2014-09-03 21 views
0

我正在使用燒瓶並試圖讓dygraph樣本工作。下面是示例代碼(從教程頁面示例2:http://dygraphs.com/tutorial.html):Dygraph沒有定義,使用Flask

<html> 
<head> 
<script type="text/javascript" 
    src="dygraph-combined.js"></script> 
</head> 
<body> 
<div id="graphdiv2" 
    style="width:500px; height:300px;"></div> 
<script type="text/javascript"> 
    g2 = new Dygraph(
    document.getElementById("graphdiv2"), 
    "temperatures.csv", // path to CSV file 
    {}   // options 
); 
</script> 
</body> 
</html> 

這裏是我的瓶代碼(我試圖使用render_template()):

from flask import Flask, render_template 
app = Flask(__name__) 
@app.route('/') 
def render_plot(): 
    return render_template('sample.html') 
if __name__ == '__main__': 
    app.run() 

當我運行這通過python螢火蟲給我錯誤「Dygraph沒有定義。」從線G2 =新Dygraph(

Sample.html作品在我的文件夾中,但是當我試圖從Python的運行我的燒瓶代碼之後,從我的網址訪問它,它不工作我的文件夾是這樣的:

FlaskStuff/main.py

FlaskStuff /模板/ sample.html

FlaskStuff /模板/ dygraph-combined.js(要我的文件夾中加載sample.html)。

FlaskStuff/JS/dygraph-combined.js

我是新來的燒瓶。類似的回答並沒有幫助我解決這個問題。

回答

1

dygraph-combined.js位於何處?它需要在某個地方可以提供服務。您最有可能將其放置在您的static文件夾中。在static(例如,cssjs)內對類似文件進行分組是相當普遍的做法。

採用這種結構

static/ 
    js/ 
     dygraph-combined.js 

你要更新sample.html如下

<script type="text/javascript" src="{{ url_for('static', filename='js/dygraph-combined.js') }}"></script> 

這將允許瓶開發服務器提供服務的文件。您還需要將規則添加到HTTP服務器,以便直接從/static提供內容。

0

您還必須爲temperature.csv文件做類似的事情。 (我已將它放在靜態文件夾中)

"{{ url_for('static', filename='temperatures.csv') }}", // path to CSV file