2

I need to create an array in an HTML file of the form:

<script>
   var markers = [
         [1, 2],
         [3, 4]
         ];
</script>

I can get this array from either one of two sources: python via pandas or sql. I know how to get the list into a flat file or json, but do not know how to get that list into the javascript array.

What's a good way to do this?

5
  • plz post your list, as an assumption you can result = json.dumps(list) from python end and JSON.parse(result) from javascript end. Commented Oct 5, 2018 at 1:31
  • 1
    I don't follow. if you can save the data to a flat file, what's the problem? Also, is this running via HTTP or locally? Commented Oct 5, 2018 at 1:35
  • Sorry, that is the problem. I can dump a flat file to a local server but then I don't know how to get the contents into the javascript array. Running locally. Commented Oct 5, 2018 at 1:39
  • json is something like this: {"a":{"0":-64.08,"1":-64.09},"b":{"0":42.13,"1":42.12}} Commented Oct 5, 2018 at 1:42
  • 1
    @racket99 kind of similar, don't know full match your case or not? have a look stackoverflow.com/questions/23038710/… Commented Oct 5, 2018 at 1:42

1 Answer 1

3

In Django example, you do this:

view.py

data = df.to_dict(orient='records')
return render(request, 'XXX.html',{'data':data})`

in template:

data = {{ data|safe }};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.