1 Star 0 Fork 0

KG / 毕业纪念册

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

python_final_test 18网新Python期末项目

  • 项目代码Github URL

  • 项目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动作描述:

  • 上方设有导航栏,可自行选择四个页面,每个页面故事不同,第一页面和第四页面是不同的两组数据的不同图表对比,可以筛选其中某个数据查看图表。第二页面是中国地图,可放大缩小,也可以在下拉框中选中年份,点击DO it!查看不同年份的中国地图图表,同时下面的列表也会随之改变;第三页面为漏斗图,在下拉框中可以选中不同的指标,点击DO it!查看不同指标下不同年份的数据对比,同时下面的列表也会随之改变。

空文件

简介

暂无描述 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
M
1
https://gitee.com/NFUNM037/graduation_album.git
git@gitee.com:NFUNM037/graduation_album.git
NFUNM037
graduation_album
毕业纪念册
master

搜索帮助