0

I am following the instructions from http://www.highcharts.com/docs/getting-started/your-first-chart to create a sample chart. I have saved the main chunk of javascript locally, and am add the <script src="/chart.js"></script> tag in my html to reference it.

On my side, I am using python flask to render a html template containing the script.

@app.route('/view', methods=['POST', 'GET'])
def show_graph_view():
    query= request.form['query']
    data = get_current_data(query)
    return render_template('graph.html', data=data)

I have a function to prepare some custom and current data I want to plot instead and I want the data to be available once the client brower loads. How do I add this data into the charts?

4
  • can you give us some code? Commented Aug 2, 2016 at 1:58
  • @Seekheart added a code, my problem is with passing the data parameters to a js file outside the html Commented Aug 2, 2016 at 2:04
  • so your flask app is your server, to access the data you need to perform a GET from your client side js. Commented Aug 2, 2016 at 2:07
  • @Seekheart is there no way I can get the data without a GET request? I think this can be done if I put the javascript in the html, but it gets ugly Commented Aug 2, 2016 at 2:11

2 Answers 2

1

Assuming a globally accessible function, just call it in the module with the data converted to json on the server with the tojson and safe filters.

<script type=text/javascript> doSomethingWith({{ data|tojson|safe }}); </script>

It's a bit hard to follow the logic when you mix together server side templating and client side scripting like this. But sometimes you gotta do it.

Sign up to request clarification or add additional context in comments.

Comments

0

Is stumbled on this old answer in 2021 and I just wanted to add another option..

Injecting Python seems to mess with your Javascript, but you can actually do this:

<script>
    var names = []
    var i = 0
    // {% for i in range(0, names_len) %}      
        names[i++] = "{{names[i]}}";
    // {% endfor %}  
</script>

You can "comment-out" the injected Pyhton code, so your Javascript remains correct :)

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.