用flaskr和jinja2运行highcharts

时间:2013-03-25 03:59:55

标签: javascript python highcharts flask jinja2

我似乎无法使用flaskr和jinja2获得高级图表的工作演示。当我打开页面时,我得到了这三个错误。我打算使用javascript来调用我的python代码将提供的json对象来呈现图表。

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

这是我渲染模板的python代码

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

我将脚本src用于highcharts以及jquery放在layout.html中的一个块中,以及应该在login.html中创建图表的函数。

这是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>

这是我的login.html,我正在尝试呈现代码

{% 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 %}

我做错了什么?另外,我如何解决此问题以获得有效的演示?

1 个答案:

答案 0 :(得分:4)

首先加载jQuery,然后加载Highstock,参见:

<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>