0

I have built a simple flask application. The front end (view) offers two checkboxes A and B that are dataframe columns.

When I select either of the columns, the column values are plotted.

Here is the code.

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)

What I want is, both the columns to be plotted (either horizontally or vertically) when I select both the columns.

Here is 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>

Please suggest the way forward. My bokeh version is 1.0.4

1 Answer 1

0

By fluke, I found the solution.

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

dd=[]
for i in range(len(signal)):
    print(i)
    dd.append(bokeh_plot(df1,i))
save([dd[0],dd[1]]) 

And here is the bokeh_plot function

def bokeh_plot(df,i):
    path=os.getcwd()
    plot=figure(title="Time series data for "+str(df.columns[i])+" pair",width=1000,height=600,x_axis_type='datetime',tools='hover')   
    plot.line( source=df,x='index',y=str(df.columns[i]), line_color="blue",legend="spot rate")
    output_file(str(path)+'/templates/myplot11.html')
    #save([plot])
    return plot
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.