在散景中绘制多列数据帧

我已经构建了一个简单的烧瓶应用程序。前端(视图)提供了两个复选框 A 和 B,它们是数据框列。


当我选择任一列时,将绘制列值。


这是代码。


from flask import Flask, render_template, redirect, request

from bokeh.plotting import figure, show, output_file,save

import webbrowser

from threading import Timer

import pandas as pd

import numpy as np

import os



app = Flask(__name__)


@app.route('/')

def index():

    return render_template('sop1.html')


@app.route('/FlaskTutorial',  methods=['POST'])

def user_rec():

    dates = pd.date_range('20130101', periods=100)


    df1 = pd.DataFrame(np.random.randn(100, 2), index=dates, 

     columns=list('AB'))



    result = request.form 

    signal = request.form.getlist('check')

    print(signal)


    df=pd.DataFrame(df1[signal])


    path=os.getcwd()



    plot=figure(title="Time series data for "+str(df.columns[0])+" 

pair",width=1000,height=600,x_axis_type='datetime',tools='hover')   

    plot.line( source=df,x='index',y=str(df.columns[0]), line_color="blue",legend="spot rate")

output_file(str(path)+'/templates/myplot11.html')

save([plot])


return render_template('myplot11.html', result=result)


def open_browser():

      webbrowser.open_new('http://127.0.0.1:3190/') 


if __name__ == '__main__':


    Timer(1, open_browser).start();

    app.run(port=3190,debug=True)

我想要的是,当我选择两列时要绘制的两列(水平或垂直)。


这是 sop1.html


<!DOCTYPE html>

<html>

<head>



   <body>



      <form method="post" action="/FlaskTutorial">



      <div class="custom1"> 


        <p>Dataframe columns</p> 


        <input type = "checkbox" name = "check" value = "A"> A <br> </br>

        <input type = "checkbox" name = "check" value = "B"> B<br> </br>




       </div>



     <input type="submit" value="Submit" name="ok"/>    



      </form>

   </body>

</html>

请建议前进的方向。我的散景版本是 1.0.4




SMILET
浏览 168回答 1
1回答

白板的微信

侥幸,我找到了解决方案。result = request.form&nbsp;signal = request.form.getlist('check')dd=[]for i in range(len(signal)):&nbsp; &nbsp; print(i)&nbsp; &nbsp; dd.append(bokeh_plot(df1,i))save([dd[0],dd[1]])&nbsp;这是 bokeh_plot 函数def bokeh_plot(df,i):&nbsp; &nbsp; path=os.getcwd()&nbsp; &nbsp; plot=figure(title="Time series data for "+str(df.columns[i])+" pair",width=1000,height=600,x_axis_type='datetime',tools='hover')&nbsp; &nbsp;&nbsp; &nbsp; plot.line( source=df,x='index',y=str(df.columns[i]), line_color="blue",legend="spot rate")&nbsp; &nbsp; output_file(str(path)+'/templates/myplot11.html')&nbsp; &nbsp; #save([plot])&nbsp; &nbsp; return plot
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python