0

I would like to display a drop down selector and underneath a data frame displayed as a table and filtered on the drop down selection.

Example code:

import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

# example df
df = pd.DataFrame({'numbers': [1, 2, 3], 'letters': ['A', 'B', 'C']})

# App layout
app = dash.Dash(__name__, prevent_initial_callbacks=True, suppress_callback_exceptions=True)
app.layout = html.Div([
    dcc.Dropdown(
        id='demo-dropdown',
        options=[
            {'label': 'A', 'value': 1},
            {'label': 'B', 'value': 2},
            {'label': 'C', 'value': 3}
        ],
        value=1
    ),
    html.Div(id='dd-output-container'),

    # want this df to print out underneath the filter
    dash_table.DataTable(id='df')
])

@app.callback(
    dash.dependencies.Output('dd-output-container', 'children'),
    [dash.dependencies.Input('demo-dropdown', 'value')])
def update_output(value):
    return df[df['letters']=='what do I put here to print out the filtered df?']

if __name__ == '__main__':
    app.run_server(debug=True)

I'm struggling in two areas related to my goal:

  1. Is the gist of line dash_table.DataTable(id='df') correct if I want to display a dataframe here? I tried making it static to just show anything but failed.
  2. The line return df[df['letters']=='what do I put here to print out the filtered df?'] hopefully illustrates what I'm trying to do?

Using this example, how can I display a drop down menu and a table where the table is filtered based on the drop down selection?

When I try the above block I get error:

dash.exceptions.InvalidCallbackReturnValue: The callback for <Output dd-output-container.children> returned a value having type DataFrame which is not JSON serializable.

The value in question is either the only value returned, or is in the top level of the returned list,

and has string representation Empty DataFrame Columns: [numbers, letters] Index: []

In general, Dash properties can only be dash components, strings, dictionaries, numbers, None, or lists of those.

1 Answer 1

3

The return value of the drop-down is an integer, so it will be the 'number' column of the data frame. Also, the table in Dash is in dict format, so it needs to be converted.

import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

# example df
df = pd.DataFrame({'numbers': [1, 2, 3], 'letters': ['A', 'B', 'C']})

# App layout
app = dash.Dash(__name__, prevent_initial_callbacks=True, suppress_callback_exceptions=True)

app.layout = html.Div(children=[
    dcc.Dropdown(
        id='demo-dropdown',
        options=[
            {'label': 'A', 'value': 1},
            {'label': 'B', 'value': 2},
            {'label': 'C', 'value': 3}
        ],
        value=1
    ),
    dash_table.DataTable(id='dd-output-container',
                         data=df.to_dict('records'),
                         columns=[{'id': c, 'name': c} for c in df.columns.values]) #  
])
@app.callback(
    dash.dependencies.Output('dd-output-container', 'data'),
    [dash.dependencies.Input('demo-dropdown', 'value')])
def update_output(value):
    dfs = df.loc[df['numbers'] == value]
    return dfs.to_dict('records') 

if __name__ == '__main__':
    app.run_server(debug=True)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.