简体   繁体   English

带Flask服务器的d3饼图

[英]d3 pie chart with flask server

I'm using the built-in flask server and I want to show an animated pie chart, like here: http://codepen.io/tpalmer/pen/jqlFG or http://jsfiddle.net/thmain/xL48ru9k/1/ 我正在使用内置的烧瓶服务器,并且想要显示动画的饼图,例如: http : //codepen.io/tpalmer/pen/jqlFGhttp://jsfiddle.net/thmain/xL48ru9k/1 /

For simplicity, I use the latter one. 为了简单起见,我使用后一种。

The python flask server code is: python flask服务器代码为:

from flask import Flask, render_template, request, flash
from forms import ContactForm
app = Flask(__name__)
app.secret_key = 'blah'

@app.route('/', methods = ['GET', 'POST'])
def test():
   return render_template('test.html')

if __name__ == '__main__':
   app.run(debug = True)

I did copy the javascript code provided on the webpage to the file static/script2.js and the css code to static/css/style_d3.css 我确实将网页上提供的javascript代码复制到了文件static / script2.js,并将CSS代码复制到了static / css / style_d3.css

My HTML code is 我的HTML代码是

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <link  rel="stylesheet"  href="//style_d3.css" type="text/css">
  </head>

  <body>
<p>test</p>
<div class="animated-ring">
    <svg></svg>
</div>

    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="//script2.js"></script>
  </body>

</html>

I run the webpage through the flask server. 我通过烧瓶服务器运行网页。 But I get a webpage that just says "test", there's no chart. 但是我得到一个仅显示“测试”的网页,没有图表。 What do I do wrong? 我做错了什么?

I'd really appreciate if someone could help me. 如果有人可以帮助我,我将非常感激。

You only render the test.html at "/" path, the script2.js needs to be rendered too at "/script2.js". 您仅在“ /”路径处呈现test.html,script2.js也需要在“ /script2.js”处呈现。

Try this one liner at your project's root directory to host all files: 在您项目的根目录下尝试使用此衬垫来托管所有文件:

python -m SimpleHTTPServer

I think you need to set to a path your js file. 我认为您需要将js文件设置为路径。 In your app dir create a static folder then put your script2.js file and when you call this file at html use flask url_for() function 在您的应用程序目录中,创建一个静态文件夹,然后放置您的script2.js文件,并在html调用此文件时,使用flask url_for()函数

src="{{url_for('static', filename='script2.js'}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM