Good day,
I’m trying to build a dashboard with a dash datable and two dropdowns. The dropdowns are generated by the unique values of two fields (location and side). The first field is location which than determines the second dropdown which represent the site. For example a location can have three sites (e.g. A, B and C). Each site can have 2 or more. measurements (see image below). The link to the dataset: link to dataset
I would like to generate a dashboard that is able to select the location(s) first, which than filter the dash table meeting the selected location(s). Secondly I would like to select the side(s) which than filter the rest of the dash table meeting the selected side(s) and previous selected location(s).
This is my python script that I have so far:
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_table as dt
from dash.dependencies import Input, Output
import pandas as pd
df = pd.read_csv(‘C:\Users\jsi201\Documents\test\test.csv’)
app = dash.Dash(name)
fnameDict = {}
for i in df[‘location’].unique():
fnameDict[i] = [df[‘site’][j] for j in df[df[‘location’]==i].index]
names = list(fnameDict.keys())
nestedOptions = fnameDict[names[0]]
app.layout = html.Div(
[
html.Div([
html.Div([
dcc.Dropdown(
id=‘namedropdown’,
options=[{‘label’:name, ‘value’:name} for name in names],
value = list(fnameDict.keys())[0]
)
],
style={‘width’: ‘20%’, ‘display’: ‘inline-block’}
),
html.Div([
dcc.Dropdown(
id='optdropdown',
options=[{'label': i, 'value': i} for i in df.location.unique()]
)
],
style={'width': '20%', 'display': 'inline-block'}
)
]),
html.Div([
dt.DataTable(id='table-container', columns=[{'id': c, 'name': c} for c in df.columns.values])
])
])
@app.callback(
dash.dependencies.Output(‘optdropdown’, ‘options’),
[dash.dependencies.Input(‘namedropdown’, ‘value’)]
)
def update_date_dropdown(name):
return [{‘label’: i, ‘value’: i} for i in fnameDict[name]]
@app.callback(
dash.dependencies.Output(‘table-container’, ‘data’),
[dash.dependencies.Input(‘namedropdown’, ‘value’),
dash.dependencies.Input(‘optdropdown’, ‘value’)])
def display_table(location,mm):
dff = df[df.location==location]
dff1 = dff[dff.site==site]
#dff = df[df.location==location]
return dff1.to_dict(‘records’)
if name == ‘main’:
app.run_server()
This is the output that generates:
Any help would be very much appreciated. Kind regards Jorn Sijbertsma