【问题标题】:Dygraph is not defined, using FlaskDygraph 未定义,使用 Flask
【发布时间】:2014-10-28 10:05:50
【问题描述】:

我正在使用烧瓶并试图让 dygraph 样本工作。这是示例代码(教程页面中的第二个示例:http://dygraphs.com/tutorial.html):

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

这是我的 Flask 代码(我正在尝试使用 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 firebug 运行它时,出现错误“Dygraph 未定义”。从线 g2 = new Dygraph(

Sample.html 在我的文件夹中工作,但是当我从 python 运行烧瓶代码后尝试从我的 url 访问它时它不起作用。我的文件夹如下所示:

FlaskStuff/main.py

FlaskStuff/templates/sample.html

FlaskStuff/templates/dygraph-combined.js(在我的文件夹中加载 sample.html)。

FlaskStuff/js/dygraph-combined.js

我是 Flask 的新手。类似的答案并没有帮助我解决这个问题。

【问题讨论】:

    标签: flask dygraphs


    【解决方案1】:

    您还必须对 temperature.csv 文件执行类似的操作。 (我已经放在静态文件夹中了)

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

    【讨论】:

      【解决方案2】:

      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>
      

      这将允许 Flask 开发服务器提供文件。您还需要向 HTTP 服务器添加一条规则,以直接提供来自 /static 的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        相关资源
        最近更新 更多