-2

I went through the following articles to get clarity.

Flask isn't getting the checkbox value

Send data from a textbox into Flask?

Though they are informative, the html code in those questions are straight forward.

This is my code.

from flask import Flask, render_template, request, jsonify
import webbrowser
from threading import Timer
import pandas as pd
import warnings
from bokeh.io import output_file
from bokeh.models.widgets import CheckboxGroup
from bokeh.plotting import save

flat_list=['S&P 500',
 'Nasdaq 100',
 'Russell 2000',
 'MSCI Europe',
 'STOXX Europe 600',
 'Nikkei 225',
 'MSCI Japan',
 'MSCI EM',
 'MSCI US REIT',
 'Commodities',
 'US Corp HY',
 'US Corp IG',
 'LT US Treasuries (20+)',
 'LT US Treasuries(10-30)',
 'Gold',
 'Barclays Agg. Bond Index',
 'CPA(7-10YR Treasury)',
 'Rf']

output_file("dropdown_list.html")
dropdown = CheckboxGroup(labels=flat_list)
save(dropdown)

app = Flask(__name__)

@app.route('/process',methods=["GET", "POST"])
def process():
    if request.method == 'POST':
        data = request.json
        return jsonify(data)
    #return render_template("form.html")
    return render_template("dropdown_list.html")

def open_browser():
      webbrowser.open_new('http://127.0.0.1:2000/process') 

if __name__ == '__main__':

    Timer(1, open_browser).start();
    app.run(port=2000)

The code opens dropdown_list html. How do I get the checked boxes back in flask?

Abhishek Kulkarni
  • 654
  • 1
  • 8
  • 20

1 Answers1

0

Your best bet would be to configure the Bokeh checkbox with a CustomJS callback that makes a POST of its own back to your flask app, e.g. do something like this in the code that actually created the Bokeh content:

callback = CustomJS(args=dict(widget=dropdown), code="""
    // JS code to post widget.active back to you app goes here 
""")

dropdown.js_on_change('active', callback)
bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • I edited the code to simplify my question. Please let me know. You answer is helpful but not giving me idea – Abhishek Kulkarni Jun 26 '19 at 17:23
  • This is all I can offer. The value you want will be available in the JS variable `widget.active` in the code block. Only you know and control what the end points of your flask app are, that this value should be posted to. – bigreddot Jun 26 '19 at 17:31
  • Thanks for your valuable time. I have updated with the flask working code – Abhishek Kulkarni Jun 26 '19 at 17:46
  • I don't really understand what you are looking for. Are you asking how to make an Ajax POST from JavaScript in general? Use [`XMLHttpRequest.send`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send) (or [`$.ajax`](https://api.jquery.com/jQuery.ajax/) if you are using JQuery) – bigreddot Jun 26 '19 at 18:08
  • Not an ajax post. If I select S&P500, then in flask it should print S&P500,1 else S&P500,0 – Abhishek Kulkarni Jun 26 '19 at 18:33
  • The Bokeh widget is in the browser. As far as I know, the only way to get its value to your Flask app is to make an ajax call (I point out exactly where to do this, the snippet above). What the flask app does with the value once it receives it (prints it or whatever) is up to you, but also not really related to the question above. – bigreddot Jun 26 '19 at 18:45