I’ve been trying to make a dashboard where I can populate the filters (dash core components like Dropdown, etc.) and figures after I upload a file using the Upload component.
For Sharing data between callbacks, I am using a hidden Div to store the CSV contents as json.
I’m facing a problem updating the dropdown options in a callback using the ‘children’ attribute of the hidden Div or the contents attribute of Upload component.
Here is the smallest complete snippet I created:
import base64
import datetime
import io
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input, State
import dash_table
app = dash.Dash(__name__)
app.title = 'Trial App'
app.layout = html.Div(
[
dcc.Upload(
id='file_upload',
children=[
'File Select'
]
),
html.Div(
id='uploaded_file',
style={
'display' : 'none'
}
),
dcc.Dropdown(
id='ride_drop'
)
]
)
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
return df
@app.callback(
Output('uploaded_file', 'children'),
[Input('file_upload', 'contents')],
[State('file_upload', 'filename'),
State('file_upload', 'last_modified')
]
)
def update_hidden_div(content, name, date):
#
if content is not None:
df = parse_contents(content, name, date)
jsonified_dataframe = df.to_json(
date_format='iso',
orient='split'
)
print('[INFO] Hidden Div updated')
return [
jsonified_dataframe
]
@app.callback(
Output('ride_drop', 'options'),
[Input('file_upload', 'contents')],
[State('uploaded_file', 'children')]
)
def update_ride_dropdown(content, jsonified_dataframe):
if content is not None:
df = pd.read_json(jsonified_dataframe, orient='split')
print('[INFO] Dataframe is of size: {}'.format(df.shape[0]))
rides = df['ride_num'].unique()
return [
{
'label': r,
'value' : r
} for r in rides
]
if __name__ == '__main__':
app.run_server(debug=True)
Here is the output:
The server seems to not respond (execute the print statements). And the Dropdown is not populated. The data is correct, I checked.
Any help is appreciated. Thanks!
Edit:
If I remove the second callback to update the Dropdown component, the server seems to respond.