Guest User

Untitled

a guest
Nov 15th, 2022
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. from flask import Flask, jsonify
  2. from jinja2 import Template
  3. import numpy as np
  4. from bokeh.plotting import figure
  5. from bokeh.models import AjaxDataSource, Slider
  6. from bokeh.models.callbacks import CustomJS
  7. from bokeh.embed import components
  8. from bokeh.layouts import column
  9. from bokeh.resources import INLINE
  10. app = Flask(__name__)
  11. a, b = 1, 0
  12. @app.route("/data", methods=['POST'])
  13. def get_x():
  14.     global a, b
  15.     a += 1      # in real use case, I would like to get the value of the "slider1"
  16.     b += 1      #                                                    ... "slider2"
  17.     x = np.arange(100)
  18.     y = a * np.sin(x) + b
  19.     print(y)
  20.     return jsonify(x=x.tolist(), y=y.tolist())
  21. template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Streaming Example</title> {{ js_resources }} {{ css_resources }} </head> <body> {{ plot_div }} {{ plot_script }} </body> </html> ''')
  22. @app.route("/")
  23. def simple():
  24.     source = AjaxDataSource(data_url="http://127.0.0.1:5000/data", polling_interval=None, mode='replace')
  25.     fig = figure(title="Streaming Example")
  26.     fig.line('x', 'y', source=source)
  27.     callback = CustomJS(args=dict(source=source), code="""source.get_data(source.mode);""")
  28.     slider1 = Slider(start=1, end=5, value=1, step=1, title="a")
  29.     slider1.js_on_change('value', callback)
  30.     slider2 = Slider(start=0, end=20, value=0, step=1, title="b")
  31.     slider2.js_on_change('value', callback)
  32.     layout = column(fig, slider1, slider2)
  33.     script, div = components(layout, INLINE)
  34.     html = template.render(plot_script=script, plot_div=div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
  35.     return html
  36. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment