Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76ae98d

Browse files
committed
Python Web Flask pyecharts
1 parent 0b94450 commit 76ae98d

22 files changed

+714
-0
lines changed

‎day-017/template.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from flask import Flask # 引入Flask模块
2+
from flask import render_template # 引入Jinja2模板
3+
4+
app = Flask(__name__)
5+
6+
@app.route('/')
7+
def index():
8+
return render_template('hello.html', name=(1,2,3,4))
9+
10+
@app.route('/template/')
11+
def template():
12+
name = 'Jinja2 模板引擎'
13+
myindex = 1
14+
mylist = [1,2,3,4]
15+
mydict = {
16+
'key': 'age',
17+
'value': '25'
18+
}
19+
mytuple = (1,2,3,4)
20+
return render_template('template.html', name=name, myindex=myindex, mylist=mylist, mydict=mydict, mytuple=mytuple)
21+
22+
23+
if __name__ == "__main__":
24+
app.run()

‎day-017/templates/hello.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello {{ name[0] }}</h1>

‎day-017/templates/template.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p> A value form a string: {{ name }}.</p>
2+
<p> A value form a int: {{ myindex }}.</p>
3+
<p> A value form a list: {{ mylist[3] }}.</p>
4+
<p> A value form a list, with a variable index: {{ mylist[myindex] }}.</p>
5+
<p> A value form a dictionary: {{ mydict['key'] }}.</p>
6+
<p> A value form a tuple: {{ mytuple }}.</p>
7+
<p> A value form a tuple by index: {{ mytuple[myindex] }}.</p>

‎day-017/templates/新建文本文档.txt

Whitespace-only changes.

‎flask_pyecharts/app.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from flask import Flask, render_template
2+
from jinja2 import Markup, Environment, FileSystemLoader
3+
from pyecharts.globals import CurrentConfig
4+
5+
# 关于 CurrentConfig,可参考 [基本使用-全局变量]
6+
CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates/pyecharts"))
7+
8+
from pyecharts import options as opts
9+
from pyecharts.charts import Bar, WordCloud, Line
10+
11+
12+
app = Flask(__name__)
13+
14+
15+
def bar_base() -> Bar: # -> 表示要返回的是类型
16+
c = (
17+
Bar()
18+
.add_js_funcs("""
19+
alert("Hahaha")
20+
""")
21+
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
22+
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
23+
.add_yaxis("商家B", [15, 25, 16, 55, 48, 8])
24+
.add_yaxis("商家C", [33, 23, 12, 53, 23, 10])
25+
.set_global_opts(
26+
title_opts=opts.TitleOpts(
27+
title="Bar-基本示例",
28+
title_link='https://baidu.com',
29+
subtitle="我是副标题",
30+
pos_left= "center",
31+
pos_top="top"),
32+
legend_opts=opts.LegendOpts(
33+
pos_top="60"
34+
))
35+
)
36+
c = (
37+
Bar()
38+
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
39+
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
40+
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
41+
.reversal_axis()
42+
# .set_series_opts(label_opts=opts.LabelOpts(position="right"))
43+
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-测试渲染图片"))
44+
)
45+
return c
46+
47+
# 词云
48+
def wordcloud_base() -> WordCloud:
49+
w = (
50+
WordCloud()
51+
.add('',[('python', 23),('word',10),('cloud',5),
52+
('java', 23),('C',10),('C++',5),('设计模式',20),('重构',12),('架构','20')], shape='circle')
53+
)
54+
return w
55+
# 模板渲染
56+
@app.route("/")
57+
def index():
58+
c = bar_base()
59+
return Markup(c.render_embed())
60+
61+
@app.route("/wordcloud")
62+
def wordcloud():
63+
wordcloud = wordcloud_base()
64+
return Markup(wordcloud.render_embed())
65+
66+
# 前后分离
67+
@app.route("/bar")
68+
def bar():
69+
return render_template("index.html")
70+
71+
@app.route("/barChart")
72+
def get_bar_chart():
73+
c = bar_base()
74+
return c.dump_options_with_quotes()
75+
76+
# 定时增量更新
77+
from random import randrange
78+
from flask.json import jsonify
79+
80+
def line_base() -> Line:
81+
line = (
82+
Line()
83+
.add_xaxis(["{}".format(i) for i in range(10)])
84+
.add_yaxis(
85+
series_name="",
86+
y_axis=[randrange(50, 80) for _ in range(10)],
87+
is_smooth=True,
88+
label_opts=opts.LabelOpts(is_show=False),
89+
)
90+
.set_global_opts(
91+
title_opts=opts.TitleOpts(title="动态数据"),
92+
xaxis_opts=opts.AxisOpts(type_="value"),
93+
yaxis_opts=opts.AxisOpts(type_="value"),
94+
)
95+
)
96+
return line
97+
98+
@app.route("/line")
99+
def line():
100+
return render_template("index2.html")
101+
102+
@app.route("/lineChart")
103+
def get_line_chart():
104+
c = line_base()
105+
return c.dump_options_with_quotes()
106+
107+
idx = 9
108+
109+
@app.route("/lineDynamicData")
110+
def update_line_data():
111+
global idx
112+
idx = idx + 1
113+
return jsonify({"name": idx, "value": randrange(50, 80)})
114+
115+
if __name__ == "__main__":
116+
print("ok")
117+
app.run()

‎flask_pyecharts/static/echarts.min.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flask_pyecharts/static/jquery.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flask_pyecharts/templates/index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Awesome-pyecharts</title>
7+
<script src='{{ url_for("static",filename="jquery.min.js") }}'></script>
8+
<script type="text/javascript" src='{{ url_for("static",filename="echarts.min.js") }}'></script>
9+
</head>
10+
11+
<body>
12+
<div id="bar" style="width:1000px; height:600px;"></div>
13+
<script>
14+
$(
15+
function () {
16+
var chart = echarts.init(document.getElementById('bar'), 'white', { renderer: 'canvas' });
17+
$.ajax({
18+
type: "GET",
19+
url: "/barChart",
20+
dataType: 'json',
21+
success: function (result) {
22+
chart.setOption(result);
23+
}
24+
});
25+
}
26+
)
27+
</script>
28+
</body>
29+
30+
</html>

‎flask_pyecharts/templates/index2.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>定时增量更新</title>
6+
<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
7+
<script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script>
8+
9+
</head>
10+
<body>
11+
<div id="bar" style="width:1000px; height:600px;"></div>
12+
<script>
13+
var chart = echarts.init(document.getElementById('bar'), 'white', {renderer: 'canvas'});
14+
var old_data = [];
15+
$(
16+
function () {
17+
fetchData(chart);
18+
setInterval(getDynamicData, 2000);
19+
}
20+
);
21+
22+
function fetchData() {
23+
$.ajax({
24+
type: "GET",
25+
url: "/lineChart",
26+
dataType: "json",
27+
success: function (result) {
28+
chart.setOption(result);
29+
old_data = chart.getOption().series[0].data;
30+
}
31+
});
32+
}
33+
34+
function getDynamicData() {
35+
$.ajax({
36+
type: "GET",
37+
url: "/lineDynamicData",
38+
dataType: "json",
39+
success: function (result) {
40+
old_data.push([result.name, result.value]);
41+
chart.setOption({
42+
series: [{data: old_data}]
43+
});
44+
}
45+
});
46+
}
47+
48+
</script>
49+
</body>
50+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% import 'macro' as macro %}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>{{ chart.page_title }}</title>
7+
</head>
8+
<body>
9+
10+
{{ macro.gen_components_content(chart) }}
11+
12+
</body>
13+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /