I have a simple example, where I have dropdown of files (or filenames) and upon selection, I want a callback to read the selected file chosen and display the info in a table (using table experiments module).
For some reason, I find my solution cumbersome and I feel I am doing it silly. Any advice?
# Import Supporting Libraries
import pandas as pd
# Import Dash Visualization Libraries
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import dash.dependencies
from dash.dependencies import Input, Output, State
import plotly
# Load datasets
US_STATES_URL = 'https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv'
US_AG_URL = 'https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv'
df_st = pd.read_csv(US_STATES_URL)
df_ag = pd.read_csv(US_AG_URL)
# Create our app layout
app = dash.Dash(__name__)
server = app.server
app.layout = html.Div([
html.H2('My Dash App'),
dcc.Dropdown(
id='dropdown',
options=[
{'label': 'States', 'value': 'STATES_URL'},
{'label': 'Exports', 'value': 'AG_URL'},
{'label': 'No Data', 'value': None}],
searchable=False
),
html.Div(id='my-table'),
], style={'width': '60%'})
@app.callback(dash.dependencies.Output('my-table','children'),
[dash.dependencies.Input('dropdown','value')])
def display_table(dropdown_value):
if dropdown_value is None:
return html.Div()
elif dropdown_value == 'STATES_URL':
df = pd.read_csv(US_STATES_URL)
elif dropdown_value == 'AG_URL':
df = pd.read_csv(US_AG_URL)
return dt.DataTable(
id='my-datatable',
rows=df.to_dict('records'),
editable=False,
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=[])
app.css.append_css({
'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'
})
if __name__ == '__main__':
app.run_server(debug=True)