dash-docs icon indicating copy to clipboard operation
dash-docs copied to clipboard

Datatable Filtering - Documentation doesn't match current Dash version

Open davidalbertonogueira opened this issue 4 years ago • 1 comments

Hi everyone,

I've been trying the code examples from https://dash.plotly.com/datatable/filtering and they don't work. For example, the code in the section 'Advanced filter usage' raises 'TypeError: Unexpected keyword argument filter_action.

I get the same type of errors with other examples. In that same page, the previous snippet raises 'TypeError: Unexpected keyword argument `filter_query'.

Currently running Dash v2.0.0. dash==2.0.0 dash-core-components==2.0.0 dash-html-components==2.0.0 dash-renderer==0.20.0 dash-table==5.0.0

davidalbertonogueira avatar Jan 05 '22 23:01 davidalbertonogueira

The examples should run on with both Dash 2 and 1. Does it work for you if you swap out the Dash 1 import statements for Dash 2? That would look like this:

import dash
from dash import Input, Output, dcc, html, dash_table
import pandas as pd
import json

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')

df['id'] = df['country']
df.set_index('id', inplace=True, drop=False)

app = dash.Dash(__name__)

app.layout = html.Div([

    dcc.RadioItems(
        id='filter-query-read-write',
        options=[
            {'label': 'Read filter_query', 'value': 'read'},
            {'label': 'Write to filter_query', 'value': 'write'}
        ],
        value='read'
    ),

    html.Br(),

    dcc.Input(id='filter-query-input', placeholder='Enter filter query'),

    html.Div(id='filter-query-output'),

    html.Hr(),

    dash_table.DataTable(
        id='datatable-advanced-filtering',
        columns=[
            {'name': i, 'id': i, 'deletable': True} for i in df.columns
            # omit the id column
            if i != 'id'
        ],
        data=df.to_dict('records'),
        editable=True,
        page_action='native',
        page_size=10,
        filter_action="native"
    ),
    html.Hr(),
    html.Div(id='datatable-query-structure', style={'whitespace': 'pre'})
])


@app.callback(
    Output('filter-query-input', 'style'),
    Output('filter-query-output', 'style'),
    Input('filter-query-read-write', 'value')
)
def query_input_output(val):
    input_style = {'width': '100%'}
    output_style = {}
    if val == 'read':
        input_style.update(display='none')
        output_style.update(display='inline-block')
    else:
        input_style.update(display='inline-block')
        output_style.update(display='none')
    return input_style, output_style


@app.callback(
    Output('datatable-advanced-filtering', 'filter_query'),
    Input('filter-query-input', 'value')
)
def write_query(query):
    if query is None:
        return ''
    return query


@app.callback(
    Output('filter-query-output', 'children'),
    Input('datatable-advanced-filtering', 'filter_query')
)
def read_query(query):
    if query is None:
        return "No filter query"
    return dcc.Markdown('`filter_query = "{}"`'.format(query))


@app.callback(
    Output('datatable-query-structure', 'children'),
    Input('datatable-advanced-filtering', 'derived_filter_query_structure')
)
def display_query(query):
    if query is None:
        return ''
    return html.Details([
        html.Summary('Derived filter query structure'),
        html.Div(dcc.Markdown('''```json
{}
```'''.format(json.dumps(query, indent=4))))
    ])


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

LiamConnors avatar Jan 11 '22 15:01 LiamConnors