Hello everyone,
I’m new to dash and plotly, and I need some help with trying to get some data to render in a dash datatable.
app.layout = html.Div([
html.Div([
html.Label('Filter Type:'),
dcc.RadioItems(
id='filter-type',
options=[
{'label': 'All', 'value': 'All'},
{'label': 'Water Rescue', 'value': 'Water'},
{'label': 'Mountain or Wilderness Rescue', 'value': 'Mountain'},
{'label': 'Disaster Rescue or Individual Tracking', 'value': 'Disaster'},
],
value='All'
)
]),
dash_table.DataTable(id='datatable-id',
data=df.to_dict('records'),
columns=[{"name": i, "id": i, "deletable": False, "selectable": True} for i in df.columns],
editable=True,
row_selectable="single",
selected_rows=[0],
filter_action="native",
sort_action="native",
page_action="native",
page_current=0,
page_size=10
),
])
])
@app.callback([Output('datatable-id','data'),
Output('datatable-id','columns')],
[Input('filter-type', 'value')])
def update_dashboard(filter_type):
breed_mappings = {
'Water': ["Labrador Retriever Mix", "Chesapeake Bay Retriever", "Newfoundland"],
'Mountain': ["German Shepherd", "Alaskan Malamute", "Old English Sheepdog", "Siberian Husky", "Rottweiler"],
'Disaster': ["Doberman Pinscher", "German Shepherd", "Golden Retriever", "Bloodhound", "Rottweiler"]
}
if filter_type == "All":
df = pd.DataFrame.from_records(db.getRecordsByQuery({}))
elif filter_type not in breed_mappings:
raise Exception("Unknown filter")
else:
target_breeds = breed_mappings[filter_type]
or_conditions = [{"breed": breed} for breed in target_breeds]
df = pd.DataFrame.from_records(db.getRecordsByQuery({
"$or": or_conditions,
"sex_upon_outcome": "Intact Male" if filter_type == "Mountain" else "Intact Female",
"age_upon_outcome_in_weeks": {"$gte": 20.0, "$lte": 300.0} if filter_type == "Disaster" else {"$gte": 26.0, "$lte": 156.0}
}))
data = df.to_dict('records')
columns = [{"name": i, "id": i, "deletable": False, "selectable": True} for i in df.columns]
return (data, columns)
I am able to successfully retrieve data from my database, but I can’t load the data into the table. I’ve uploaded the error stack here.
I’m using dash 2.0.0.
I’ve tried:
- Reducing the number of attributes on the DataTable
- Returning a single data attribute instead of a tuple
- Turning off debug mode
- Installing a different version of dash
but I’ve unable to resolve this issue. If someone could please assist me, I’d sincerely appreciate it. Thank you so much in advance!