flask+Pyecharts+ajax实现分tab页展示多图

需求是做一个简单的网页前后台分离服务,实现获得页面输入的起始时间段,后台计算多个量化指标,完成图形后,在前台实现分页签的可视化展示。 作为演示,选定"前30名涨跌幅、换手率(正排倒排)、成交量(正排倒排)"

技术基础参考利用 Flask 动态展示 Pyecharts 图表数据的几种方法一文中"Flask 前后端分离"部分,不再赘述。 主要思路是一次查询,一次计算形成结果集(dataform),并根据dataform的对应标的代码和不同指标,形成分别涨跌幅、换手率和成交图表,在前端页对应三个页签显示。

主要难点是:

pyecharts的Tab对象没有dump_options_with_quotes()方法,所以只能利用html的tab对象,后台需要把多个图形按适当方式传递到前台,前台解析后再匹配到对应的组件。

目录

-  templates(页面目录)-mdStat2.html
                                   -404.html
- util(后台目录)-dataProcess.py (数据处理)
                   			-drawChart.py(画图)
                   			-Utility.py(函数工具)
-firstServer.py(flask启动程序)

入口页面:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>查询股票指标报告</title>
        <!-- 引入 echarts.js -->
        <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>   // jquery引入
        <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>  //Echarts引入
        <style>
            .btns input{
                width:100px;
                height: 40px;
                background-color: #ddd;
                border: 0;
            }
            .btns .current{
                background-color: gold;
            }
            .cons .active{
                display: block;
            }
            .tab1{
                width: 1000px;
                height: 300px;
            }
    
            .none {
                display: none;
            }
        </style>
        <script>  //触发tab切换
            $(function () {
                var $btn = $('.btns input');
                var $div = $('.cons div');
                $btn.click(function () {
                    $(this).addClass('current').siblings().removeClass('current');
                    $('.cons .item').eq($(this).index()).addClass('active').siblings('.item').removeClass('active');
                })
            })
        </script>
    </head>
    <body>
       // 查询form
        <form id="form1" onsubmit="return false" action="#" method="post">
            <p id="p1">起始日期:
                <input name="startDate" type="text" id="startDate" tabindex="1" size="16" value="" placeholder="起始日期"/>
            </p>
            <p id="p2">结束日期:
                <input name="endDate" type="text" id="endDate" tabindex="2" size="16" value="" placeholder="结束日期"/>
            </p>
            <p><input type="submit" value="查询" onClick="getData()"></p>
        </form>
        <div class="btns">  //tab页签对象
            <input type="button" name="" value="01" class="current">
            <input type="button" name="" value="02">
            <input type="button" name="" value="03">
        </div>
        <div class="cons">
            <div  class="clearfloat item none active">
                <div id="tab1" class="tab1"></div>
            </div>
            <div class="clearfloat item none">
                <div id="tab2" class="tab1"></div>
            </div>
            <div class="clearfloat item none">
                <div id="tab3" class="tab1"></div>
            </div>
        </div>
    <script type="text/javascript">  
        function getData() {   //查询触发的ajax提交和返回处理
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/DataStat1" ,
                data: $('#form1').serialize(),
                success: function (result) {
                   
                    // console.log(result["chart1"],result["chart2"])
                    var myChart1 = echarts.init(document.getElementById('tab1'));
                    var ch1=$.parseJSON(result["chart1"])
                    myChart1.setOption(ch1);
                    
                    var myChart2 = echarts.init(document.getElementById('tab2'));
                    var ch2=$.parseJSON(result["chart2"])
                    myChart2.setOption(ch2);
                },
                error: function() {
                    alert("错误的日期!");
                }
            });
          // alert("query!");
        }
    </script>
    </body>
</html>

dataProcess.py和Utility.py 略过(一查一大把)最后形成,结果列表

ts_code name industry incease_rate turn_over volumn

600036 招商银行 银行 ...

...

排序后即可绘图

drawChart.py

''
Created on 2023-9-3

@author: 13795
'''
from pyecharts.charts import Bar, Grid
from pyecharts import options as opts
from pyecharts.globals import ThemeType
#import os

def draw_report(df_result):
    grid_increase = Grid()
   
    #涨幅排序,第一个图
    df_stock_increase=df_result.sort_values(by=['increaseCloseRate'], ascending=False)
    print('df_stock_increase',df_stock_increase) 
    df_stock_increase=df_stock_increase[0:30]
    
     
    bar1=Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
    x1=df_stock_increase['ts_code'].tolist()
    y1=df_stock_increase["increaseCloseRate"].tolist()
    bar1.add_xaxis(x1)
    bar1.add_yaxis('总涨幅',y1)
    bar1.set_global_opts(title_opts=opts.TitleOpts(title="涨幅", subtitle="按涨幅排序")
                        ,xaxis_opts=opts.AxisOpts(name='股票'
                                                  ,name_textstyle_opts=opts.TextStyleOpts(font_size=13)
                                                  ,axislabel_opts=opts.LabelOpts(font_size=10,rotate=15)                                    
                                             )##坐标轴标签的格式配置
                        ,yaxis_opts=opts.AxisOpts(name = '涨幅',position='right'))
    bar1.set_series_opts(label_opts=opts.LabelOpts(position='right',color='red',font_size=8))
    bar1.reversal_axis()
    
    
    
    grid_increase.add(bar1,grid_opts=opts.GridOpts(pos_left="50%",height="100%"))
    
    
    #换手率,第二个图标 
    #降幅排序
    df_stock_decrease=df_result.sort_values(by=['increaseCloseRate'], ascending=True)
    print('df_stock_decrease',df_stock_increase) 
    df_stock_decrease=df_stock_decrease[0:30]
    
    bar2=Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
    x=df_stock_decrease['ts_code'].tolist()
    y1=df_stock_decrease["increaseCloseRate"].tolist()
    bar2.add_xaxis(x)
    bar2.add_yaxis('总跌幅',y1)
    bar2.set_global_opts(title_opts=opts.TitleOpts(title="跌幅", subtitle="按跌幅排序")
                        ,xaxis_opts=opts.AxisOpts(name_textstyle_opts=opts.TextStyleOpts(font_size=13)
                                                  ,axislabel_opts=opts.LabelOpts(font_size=10,rotate=15)                                    
                                             )##坐标轴标签的格式配置
                        ,yaxis_opts=opts.AxisOpts(name = '跌幅'))
    bar2.set_series_opts(label_opts=opts.LabelOpts(position='left',color='blue',font_size=8))
    bar2.reversal_axis()
    
    grid_increase.add(bar2,grid_opts=opts.GridOpts(pos_right="50%",height="100%"))
    
  #第二个图
    grid_turnover = Grid()     
    #换手率排序
    df_turnover_increase=df_result.sort_values(by=['turnover_mean'], ascending=False)
    print('df_turnover_increase',df_turnover_increase) 
    df_turnover_increase=df_turnover_increase[0:30]
    
    
    bar3=Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
    x=df_turnover_increase['ts_code'].tolist()
    y1=df_turnover_increase["turnover_mean"].tolist()
    bar3.add_xaxis(x)
    bar3.add_yaxis('换手率最高',y1)
    bar3.set_global_opts(title_opts=opts.TitleOpts(title="换手率", subtitle="按换手最多")
                        ,xaxis_opts=opts.AxisOpts(name_textstyle_opts=opts.TextStyleOpts(font_size=13)
                                                  ,axislabel_opts=opts.LabelOpts(font_size=10,rotate=15)                                    
                                             )##坐标轴标签的格式配置
                        ,yaxis_opts=opts.AxisOpts(name = '换手率最多',position='right'))
    bar3.set_series_opts(label_opts=opts.LabelOpts(position='right',color='red',font_size=8))
    bar3.reversal_axis()
    
    grid_turnover.add(bar3,grid_opts=opts.GridOpts(pos_top="50%",pos_left="50%",height="100%"))
    

    df_turnover_decrease=df_result.sort_values(by=['turnover_mean'], ascending=True)
    
    
    print('df_turnover_decrease',df_turnover_decrease) 
    df_turnover_decrease=df_turnover_decrease[0:30]
    
    bar4=Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
    x=df_turnover_decrease['ts_code'].tolist()
    y1=df_turnover_decrease["turnover_mean"].tolist()
    bar4.add_xaxis(x)
    bar4.add_yaxis('换手率最低',y1)
    bar4.set_global_opts(title_opts=opts.TitleOpts(title="换手率", subtitle="按换手率最低")
                        ,xaxis_opts=opts.AxisOpts(name_textstyle_opts=opts.TextStyleOpts(font_size=13)
                                                  ,axislabel_opts=opts.LabelOpts(font_size=10,rotate=15)                                    
                                             )##坐标轴标签的格式配置
                        ,yaxis_opts=opts.AxisOpts(name = '换手率最低'))
    bar4.set_series_opts(label_opts=opts.LabelOpts(position='right',color='blue',font_size=8))
    bar4.reversal_axis()
    
    grid_turnover.add(bar4,grid_opts=opts.GridOpts(pos_top="50%",pos_right="50%",height="100%"))
    return grid_increase,grid_turnover  #返回

grid_increase对应涨跌幅页面,grid_turnover对应换手率排序页面

对应的页面控制跳转及flask启动程序 firstserver.sh

#coding=gbk
'''
Created on 2023-7-2
@author: 13795
'''
from flask import Flask,render_template, request
#from pyecharts.charts import Bar
from pyecharts import options as opts
import util.Uitility as ut
import util.dataProcess as dp
import util.drawChart1 as dw
#from jinja2.utils import markupsafe
import json

app = Flask(__name__)
#def first():
#    return "<p>这是我的第一个flask程序!</p>"

@app.route('/mdStat2')
def mdStat1():
    #计算个股和板块在一段时间内基本统计信息
    data = request.args.to_dict()
    return render_template("mdStat2.html", result_json=data)

@app.route("/index2")
def index2():
    c = bar_base()
    return markupsafe.Markup(c.render_embed())
    #return render_template("index.html")

@app.route("/DataStat1", methods=['GET', 'POST'])
def get_dataStat1():
    #统计信息
    startDate=request.form.get('startDate')
    endDate=request.form.get('endDate')
    
    dp1=dp.dataProcess()

    if ut.checkDate(startDate,endDate):
        df_result=dp1.cal_report(startDate,endDate)
        #print('result',df_result)
        chart1,chart2=dw.draw_report(df_result)
        resultChart={"chart1":chart1.dump_options_with_quotes(),"chart2":chart2.dump_options_with_quotes()}
        result=json.dumps(resultChart)
        #return chart1.dump_options_with_quotes(),chart2.dump_options_with_quotes()
        return result
    
    else:
        return 'error date input' 


if __name__ == '__main__':
    app.run(host='0.0.0.0')

注意返回页面需要ajax提交跳转"/DataStat1"对应的处理函数get_dataStat1()中按json方式拼接 resultChart={"chart1":chart1.dump_options_with_quotes(),"chart2":chart2.dump_options_with_quotes()}

而在页面mdStat中 ,需要把获得JSON对象转换为javascript对象,即ch1=$.parseJSON(result["chart1"])...,否则会报错 ,说明参考
jquery each报 Uncaught TypeError: Cannot use 'in' operator to search for错误

 <script type="text/javascript">  
        function getData() {   //查询触发的ajax提交和返回处理
            $.ajax({
              ...
                success: function (result) {
                   
                    // console.log(result["chart1"],result["chart2"])
                    var myChart1 = echarts.init(document.getElementById('tab1'));
                    var ch1=$.parseJSON(result["chart1"])
                    myChart1.setOption(ch1);
                    
                    var myChart2 = echarts.init(document.getElementById('tab2'));
                    var ch2=$.parseJSON(result["chart2"])
                    myChart2.setOption(ch2);
                },
                error: function() {
                    alert("错误的日期!");
                }
            });

然后启动访问 localhost:5000/mdStat2

结果

相关推荐
SEVEN-YEARS2 分钟前
深入理解TensorFlow中的形状处理函数
人工智能·python·tensorflow
EterNity_TiMe_7 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
Suyuoa18 分钟前
附录2-pytorch yolov5目标检测
python·深度学习·yolo
好看资源平台1 小时前
网络爬虫——综合实战项目:多平台房源信息采集与分析系统
爬虫·python
进击的六角龙2 小时前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂2 小时前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python
湫ccc2 小时前
Python简介以及解释器安装(保姆级教学)
开发语言·python
孤独且没人爱的纸鹤2 小时前
【深度学习】:从人工神经网络的基础原理到循环神经网络的先进技术,跨越智能算法的关键发展阶段及其未来趋势,探索技术进步与应用挑战
人工智能·python·深度学习·机器学习·ai
羊小猪~~2 小时前
tensorflow案例7--数据增强与测试集, 训练集, 验证集的构建
人工智能·python·深度学习·机器学习·cnn·tensorflow·neo4j
lzhlizihang2 小时前
python如何使用spark操作hive
hive·python·spark