kemdeveloper
kemdeveloper

Reputation: 81

How to use a a library module in a front-end external javascript file?

Situation:

I have a server side running with express.js and there I get data from SQL database. I would like to show this data to the client side, by sending it to the client.

In the front-end, I need to take this data and make it into a chart. It is possible to do it with the tag in the HTML and include the Plotly library, which I want to use. However, I would like this operation to be done in a separate file, an external JS script for client. This avoids hard-to-read code in the HTML.

The problem is, how can I import Plotly to the external front-end file and use it?

Upvotes: 0

Views: 237

Answers (2)

Jeremy Thille
Jeremy Thille

Reputation: 26360

Well, simply load it from a CDN :) That's what CDNs are here for

<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.33.1/plotly-basic.min.js">

Upvotes: 1

SylwekFr
SylwekFr

Reputation: 328

I hope I understood your need correctly :
One simple option would be to include both of your script in your html related document with the one required by the second one first :

...
<script src="path script 1" />
<script src="path script 2 depending on script 1" />

so in your case using CDN :

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="path of your script where you use ploty"></script>

PS:
Also just a proposition but did you thing about importing Ploty in your js backend and use it in your front-end ? or with post/get or with socket ?

Upvotes: 1

Related Questions