Drfrink
Drfrink

Reputation: 404

running highcharts with flaskr and jinja2

I can't seem to get a working demo of highcharts with flaskr and jinja2. I get these three errors when I open the page.I intend to use javascript to call the json object my python code will provide to render the chart.

Uncaught TypeError: undefined is not a function highstock.js:287
Uncaught TypeError: Cannot read property 'prototype' of undefined exporting.js:9
Uncaught TypeError: Object [object Object] has no method 'highcharts' 127.0.0.1:32

Here is my python code that renders the template

@app.route('/')
def route():
    return render_template('login.html')

I put the script src for highcharts as well as jquery in a block in the layout.html and the function that should create the chart in login.html.

This is the layout.html

<title>Flaskr</title>
{% block head %}
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
{% endblock %}
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Flaskr</h1>
<div class=metanav>

</div>
{% for message in get_flashed_messages() %}
<div class=flash>{{ message }}</div>
{% endfor %}
{% block body %}{% endblock %}
</div>

This is my login.html where I'm trying to render the code

{% extends "layout.html" %}
{% block body %}
<h2>Login</h2>
{% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
<form action="" method=post>
<dl>
  <dt>Username:
  <dd><input type=text name=username>
  <dt>Password:
  <dd><input type=password name=password>
  <dd><input type=submit value=Login>
</dl>
</form>
<script type="text/javascript">
$(document).ready(function() {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlc.json&callback=?', function(data) {

    // create the chart
    $('#container').highcharts('StockChart', {


        rangeSelector : {
            selected : 2
        },

        title : {
            text : 'AAPL Stock Price'
        },

        series : [{
            type : 'ohlc',
            name : 'AAPL Stock Price',
            data : data,
            dataGrouping : {
                units : [[
                    'week', // unit name
                    [1] // allowed multiples
                ], [
                    'month', 
                    [1, 2, 3, 4, 6]
                ]]
            }
        }]
    });
});
});
</script>
<div id="container" style="height: 500px; min-width: 500px"></div>
{% endblock %}

What am I doing wrong? Also how can I fix this to get a working demo?

Upvotes: 1

Views: 2488

Answers (1)

Paweł Fus
Paweł Fus

Reputation: 45079

First load jQuery, then Highstock, see:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>

Upvotes: 4

Related Questions