Hi all,
I want to filter the table by using the year dropdown filter. So e.g. if I choose ‘1957’ I only want to see the according rows. I already coded this but I have absolutely no clue how to build the dash table callback.
I would really appreciate some help after spending hours of searching for the right solution.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import dash_table as dt
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
df = df.drop_duplicates(subset=None, keep='first', inplace=False)
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Dropdown(
id='year-dropdown-table',
options=[{'label': i, 'value': i} for i in sorted(df['year'])],
value=df['year'].min()
),
dt.DataTable(
id='year-table2',
data=[{}],
columns=[{'id': c, 'name': c} for c in df.columns],
),
html.Div(id='year-table')
])
@app.callback(
Output('year-table', 'children'),
[Input('year-dropdown-table', 'value')])
def update_figure_drop(selected_year):
if selected_year is None:
tab = dt.DataTable(data=df, columns=[{'id': c, 'name': c} for c in df.columns])
else:
filtered_df = df[df.year == selected_year]
tab = dt.DataTable(data=filtered_df, columns=[{'id': c, 'name': c} for c in filtered_df.columns])
return tab
# server
if __name__ == '__main__':
app.run_server(debug=True, port=8001)
Thanks a million!
Best regards
Lucie