I am trying to display a data frame with Dash. The data frame that I have is https://www.kaggle.com/timoboz/superbowl-history-1967-2020. My goal is to show the data frame on the webpage with one search button that dynamically searches all the columns and filters the data frame.
So far, I have the following code that displays the dataframe.
import pandas as pd
import dash
import dash_table
from dash.dependencies import Input, Output
df = pd.read_csv('./Data/superbowl.csv')
PAGE_SIZE = 10
app = dash.Dash(__name__)
app.layout = dash_table.DataTable(
id='datatable-paging',
columns=[
{"name": i, "id": i} for i in df.columns #sorted(df.columns)
],
page_current=0,
page_size=PAGE_SIZE,
page_action='custom',
sort_action='custom',
sort_mode='single',
sort_by=[]
)
@app.callback(
Output('datatable-paging', 'data'),
[Input('datatable-paging', "page_current"),
Input('datatable-paging', "page_size"),
Input('datatable-paging', 'sort_by')])
def update_table(page_current,page_size,sort_by):
if len(sort_by):
dff = df.sort_values(
sort_by[0]['column_id'],
ascending=sort_by[0]['direction'] == 'asc',
inplace=False
)
else:
# No sort is applied
dff = df
return dff.iloc[
page_current * page_size:(page_current + 1) * page_size
].to_dict('records')
if __name__ == '__main__':
app.run_server(debug=True)
After reading through the documentation https://dash.plot.ly/datatable/callbacks especially 'Backend Paging with Filtering', I couldn't find a way to have like a single textbox that would search all the columns and filter the data frame for the matched entries.
filter_action='native'as an argument withindash_table.DataTable