The server has a Python script (Bottle) that generates about 10-15 variables containing lists of values. How would you pass them on to Java Script so that it builds graphics?

The most desperate way is to write all the values ​​in HTML through hidden fields, but somehow clumsy ...

The second way: pack in JSON and write it into a JS-file, which will be imported by the page to the main script. But what will happen if several users simultaneously open / refresh a page?

The third way is to do it right and here I am asking for your help.

  • one
    generate json and paste it inside the page. If several users request a page, then everyone will get their own (do support sessions, if you need a little more complex logic). - KoVadim

2 answers 2

Imagine how client-server works.

server one, it generates content that runs on the client side.

one hundred clients connected - a hundred barrels of content appeared on the client side.

each client in a barrel lesh unique js and all.

those. you just render the js block on the server with the variables that suit your client. on the server, relatively speaking:

print ("<script language='text/javascript'>alert("+ random.randint(1, 10) + ")</script>") 

on the client, for example

 <script language='text/javascript'>alert(6)</script> 

if you write in hidden form fields, this is actually the same as what you write in js. and js and html - everything flies to the client and the client already interprets this data as he wants.

    Maybe through ajax?

    A page on the client side when it is ready (for example, it can be done immediately at the time of its download) sends an ajax request to the server, receives the values ​​of the necessary variables in response, and builds anything on them.

    Although the way to write directly in html is also nothing bad. You can not in hidden fields, but make the script tag and inside it initialize the necessary variables with the necessary values.

    • via ajax rules when uploading new data. it is better to give the first result directly to the script, however, yes, it’s also an option not to mess around - strangeqargo
    • ajax - better when you need to hide data, cache is allowed by server means, you want to reduce the size of the html file. - Igor