Hello all,
i’m trying to update my data which is an output from an SQL query but while the query run the dash gets frozen until the import is complete, does anyone have an idea on how to make the output of data available for the app until the moment in which the data frame refresh is over?
In the example below you can see i introduced an time.sleep(3) to simulate the delay caused by the query, notice how the RadioItems are unresponsive (its callback are no longer reachable) while time.sleep is running.
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
from datetime import datetime
import numpy as np
import pandas as pd
import time
app = dash.Dash(__name__)
def compute_expensive_data():
t=datetime.now()
d = {'time' : pd.Series(np.array([t.minute, t.second]), index=['minute', 'second'])}
dat = pd.DataFrame(d).to_json()
time.sleep(3)
return dat
dat = compute_expensive_data()
print(dat)
app.layout = html.Div([
html.H3('Original Time: Minute = ' + str(pd.read_json(dat)['time']['minute']) + ': Second = ' + str(pd.read_json(dat)['time']['second'])),
html.Div(id='title-line-children'),
dcc.RadioItems(
id='time-dropdown',
options=[
{'label': 'Minute', 'value': 'minute'}, {'label': 'Second', 'value': 'second'},
],
value='minute'
),
# Hidden div inside the app that stores the intermediate value
html.Div(id='intermediate-value', style={'display': 'none'}, children = dat),
dcc.Interval(
id='interval-component',
interval=10*1000, # 20 seconds in milliseconds
n_intervals=0
)
])
@app.callback(
Output('title-line-children', 'children'),
[Input('time-dropdown', 'value'), Input('intermediate-value', 'children')])
def render(value,dat1):
if value == 'minute':
printStr = str(pd.read_json(dat1)['time']['minute'])
outStr = 'Minute = ' + printStr
elif value == 'second':
printStr = str(pd.read_json(dat1)['time']['second'])
outStr = 'Second = ' + printStr
return outStr
@app.callback(Output('intermediate-value', 'children'),
[Input('interval-component', 'n_intervals')])
def update_global_var(n):
return compute_expensive_data()
if __name__ == '__main__':
app.run_server(debug=False)