代码拉取完成,页面将自动刷新
项目pythonanywhere URL
数据传递描述: 用户在前端下拉框选择年份或想要查看的数据后,点击DO it! 后,会反馈到HTML文件中的
<select name="the_year_selected">
之后传递到python文件中的
the_year = request.form["the_year_selected"]
request会向前端请求数据后,通过
dfs = df2.query("指标=='{}'".format(the_year))
选取数据进行画图。
在项目中,我实现了:
1.HTML档:
设计导航栏,并为导航栏增添CSS样式。
其中两个页面为可交互,两个页面为固定图表;交互页面需要向前端发送请求,后再响应,非交互界面则不用。
加入了文本内容
链接了static文档中的CSS文件,让四个页面的样式相同,整体更加美观。
2.Python档:
(1)调用模块
flask
pandas
plotly
pyecharts
(2)使用pyecharts绘制图表
# 第一页面
df = pd.read_csv('D.csv',encoding='gbk')
@app.route('/',methods=['GET'])
def ge_jigou():
data_str = df.to_html()
with open("孤儿和机构.html", encoding="utf8", mode="r") as f:
plot_all = "".join(f.readlines())
return render_template('ge_jigou.html',
the_plot_all=plot_all,
the_res = data_str)
# 第四页面
@app.route('/yf-death',methods=['GET'])
def yf_death():
df1 = pd.read_csv('H.csv', encoding='gbk')
data_str = df1.to_html()
with open("孕妇死亡率.html", encoding="utf8", mode="r") as f:
plot_all = "".join(f.readlines())
return render_template('yf-death.html',
the_plot_all=plot_all,
the_res = data_str)
# 第二页面
@app.route('/ge_data',methods=['GET'])
def ge_data():
df2 = pd.read_csv('F.csv', encoding='gbk')
year_available = list(df2.年份.dropna().unique())
cf.set_config_file(offline=True, theme="ggplot")
py.offline.init_notebook_mode()
data_str = df2.to_html()
return render_template('ge_data.html',
the_res = data_str,
the_select_year=year_available)
#第二页面响应✌
@app.route('/ge_data_response',methods=['POST'])
def ge_data_response() -> 'html':
df2 = pd.read_csv('F.csv', encoding='gbk')
year_available = list(df2.年份.dropna().unique())
the_year = request.form["the_year_selected"]
df1 = pd.read_csv('F.csv', encoding='gbk')
time = list(df1['年份'].unique())
dq = list(df1['地区'].unique())
time.reverse()
dfs = df2.query("年份=='{}'".format(the_year))
def map_visualmap() -> Map:
z = (
Map()
.add("人数", list(zip(dq, dfs['孤儿数'])), "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="中国各年分省孤儿人数"),
visualmap_opts=opts.VisualMapOpts(max_=60000),
)
)
return z
map_visualmap().render()
with open("render.html", encoding="utf8", mode="r") as f:
plot_all = "".join(f.readlines())
data_str = dfs.to_html()
return render_template('ge_data.html',
the_plot_all = plot_all,
the_res = data_str,
the_select_year = year_available
)
# 第三页面
@app.route('/zm_qwm',methods=['GET'])
def zm_qwm():
df4 = pd.read_csv('G.csv', encoding='gbk')
zb_available = list(df4.指标.dropna().unique())
cf.set_config_file(offline=True, theme="ggplot")
py.offline.init_notebook_mode()
data_str = df4.to_html()
return render_template('zm_qwm.html',
the_res = data_str,
the_select_year=zb_available)
#第三页面响应✌
@app.route('/zm_qwm_response',methods=['POST'])
def zm_qwm_response() -> 'html':
df2 = pd.read_csv('G.csv', encoding='gbk')
year_available = list(df2.指标.dropna().unique())
the_year = request.form["the_year_selected"]
time = list(df2['年份'].unique())
zb = list(df2['指标'].unique())
time.reverse()
dfs = df2.query("指标=='{}'".format(the_year))
a = []
for t in time:
df4 = dfs[dfs.年份 == t]
a.append(sum(df4['人数']))
del a[1]
n = []
for i in time:
n.append(str(i))
def funnel_label_inside() -> Funnel:
c = (
Funnel()
.add(
"商品",
[list(z) for z in zip(n, a)],
label_opts=opts.LabelOpts(position="inside"),
)
.set_global_opts(title_opts=opts.TitleOpts(title="重男轻女"))
)
return c
funnel_label_inside().render()
with open("render.html", encoding="utf8", mode="r") as f:
plot_all = "".join(f.readlines())
data_str = dfs.to_html()
return render_template('zm_qwm.html',
the_plot_all = plot_all,
the_res = data_str,
the_select_year = year_available
)
(3)自定义函数,条件设置
df2 = pd.read_csv('G.csv', encoding='gbk')
year_available = list(df2.指标.dropna().unique())
the_year = request.form["the_year_selected"]
time = list(df2['年份'].unique())
zb = list(df2['指标'].unique())
time.reverse()
dfs = df2.query("指标=='{}'".format(the_year))
(4)for循环
for t in time:
df4 = dfs[dfs.年份 == t]
a.append(sum(df4['人数']))
del a[1]
n = []
for i in time:
n.append(str(i))
(5)交互实现
@app.route('/zm_qwm',methods=['GET'])
@app.route('/zm_qwm_response',methods=['POST'])
(6)嵌套及交互
time = list(df2['年份'].unique())
zb = list(df2['指标'].unique())
time.reverse()
dfs = df2.query("指标=='{}'".format(the_year))
data_str = dfs.to_html()
3.Web App动作描述:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。