Oblivious to this thread I have taken a stab at the topic due to my app’s demand (one part of my app generates data via asyncio/aiohttp and another needs to visualise and update based on a DataFrame).
Here’s a partially working example:
from asyncio import get_event_loop
from aiohttp.web import AppRunner, Application, Response, TCPSite, get
from aiohttp_wsgi import WSGIHandler
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output,Input
from dash import Dash
from numpy.random import rand
def graph_figure():
print(rand(5))
return {'data': [{'x': rand(5), 'y': rand(5), 'type': 'bar', 'name': 'SF'}],
'layout': {'title': 'Test'}
}
app = Dash('Orderbook')
def layout():
return html.Div(children=[
dcc.Graph(id='example-graph',
figure=graph_figure()
),
html.Button(
'Submit',
id='button'),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
n_intervals=0
)
])
app.layout = layout
@app.callback(Output('example-graph', 'figure'), [Input('button', 'n_clicks'), Input('interval-component', 'n_intervals')])
def update_graph(n, n2):
return graph_figure()
server = Application()
runner = AppRunner(server)
async def start_webapp() -> None:
wsgi_handler = WSGIHandler(app.server)
server.router.add_route('get', '/{path_info:.*}', wsgi_handler)
await runner.setup()
await TCPSite(runner, 'localhost', 8080).start()
if __name__ == '__main__':
# app.run_server() # THIS WORKS
# THIS DOES NOT WORK:
loop = get_event_loop()
loop.run_until_complete(start_webapp())
loop.run_forever()
This generates and serves a plot, however updates via Interval and Button components do not work - the callback is not called.
Does anybody have any neat ideas how to force the callback trigger? Or should I come to terms with a static plot?