I’m very new to Dash so this question might be pretty simple, but I’m having a hard time finding resources on using data frames in Dash.
The first thing that I want to do is update a displayed data frame based on user input into a radio item (or maybe a dropdown menu), but I’m getting an error. Here’s an example below. I’m getting a callback error updating table_data.data.
from dash import Dash, html, dash_table, dcc, Input, Output
import pandas as pd
import numpy as np
import random
df = pd.DataFrame({
'Region': ['USA', 'EU', 'ASIA']*4,
'Feature': ['a', 'b']*6,
'Amount': [random.randint(1,10) for val in range(12)]
})
app = Dash(__name__)
app.layout = html.Div([
dcc.RadioItems(
list(df['Region'].unique()),
id='regions-radio',
),
html.Div(
dash_table.DataTable(id="table_data", data = df.to_dict('records'), columns = [{"name": i, "id": i} for i in df.columns])
),
html.Hr(),
])
@app.callback(
Output('table_data', 'data'),
Input('regions-radio', 'value'))
def set_cities_options(region_val):
temp = df[df['Region']==region_val]
return temp
if __name__ == '__main__':
app.run_server(debug=True)