python - Refresh Bokeh 0.12.6 graph with streaming data from Flask -
struggling bokeh , flask graph data streaming.
data source update scheduled , not managed bokeh (i see done every second).
how force refresh of graph (possibly not of page there multiple graphs around in final version)?
here stripped down , runnable version of faulty code. idea welcome.
# -*- coding: utf-8 -*- import numpy np import pandas pd bokeh.embed import components bokeh.plotting import figure bokeh.resources import inline bokeh.models.sources import columndatasource flask import flask, render_template_string flask_apscheduler import apscheduler apscheduler.triggers.interval import intervaltrigger html_template = """ <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> {{ js_resources | safe }} {{ css_resources | safe }} </head> <body> <div> {{ plot_div.graph_1 | safe }} </div> {{ plot_script | safe }} </body> </html> """ resources = inline js_resources = resources.render_js() css_resources = resources.render_css() app = flask(__name__) @app.route("/") def main_graph(): script, div = components({'graph_1': graph_1.figure,}) return render_template_string(html_template, js_resources=js_resources, css_resources=css_resources, plot_script=script, plot_div=div) # ------------------------------------------------------------------------------------------------------------------- class generic3lines: def __init__(self, scheduler): self.data_source = columndatasource() self.figure = figure() self.figure.axis.visible = true self.update_data() line1 = self.figure.line(y="value_1", line_color="red", x="date_time", source=self.data_source) line2 = self.figure.line(y="value_2", line_color="blue", x="date_time", source=self.data_source) line3 = self.figure.line(y="value_3", line_color="green", x="date_time", source=self.data_source) scheduler.add_job(id="scheduled_update", func=self.update_data, trigger=intervaltrigger(seconds=1)) def update_data(self): df = pd.dataframe(dict( date_time = range(180), "value_1" = np.random.random(size=180)*5+5, "value_2" = np.random.random(size=180)*5+10, "value_3" = np.random.random(size=180)*5+15, )) print(df.head(1)) self.data_source = dict(date_time=df["date_time"], value_1=df["value_1"], value_2=df["value_2"], value_3=df["value_3"], ) scheduler = apscheduler() scheduler.start() graph_1 = generic3lines(scheduler=scheduler) if __name__ == "__main__": app.run(host="0.0.0.0", port=5050)
Comments
Post a Comment