I'm trying to create a Data table which shows so aggregate result from the core table (df). For that I am using Pandas, Dash and Plotly as follows:
import pandas as pd
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash_html_components import Div
from dash_table import DataTable
from dash.dependencies import Input, Output
df = pd.DataFrame([['1', '2021-01-31', 'category_1', 20],
['1', '2021-01-31', 'category_3', 12],
['1', '2021-02-28', 'category_1', 35],
['1', '2021-02-28', 'category_2', 17],
['1', '2021-02-28', 'category_3', 35],
['1','2021-03-31', 'category_1', 12],
['1','2021-03-31', 'category_2', 58],
['1','2021-03-31', 'category_3', 23],
['2', '2021-01-31', 'category_1', 29],
['2', '2021-01-31', 'category_2', 66],
['2', '2021-01-31', 'category_3', 22],
['2', '2021-02-28', 'category_1', 53],
['2', '2021-02-28', 'category_2', 71],
['2', '2021-02-28', 'category_3', 32],
['2','2021-03-31', 'category_1', 19],
['2','2021-03-31', 'category_2', 2],
['2','2021-03-31', 'category_3', 99],
['3', '2021-02-28', 'category_1', 53],
['3', '2021-02-28', 'category_2', 71],
['3','2021-03-31', 'category_1', 19],
['3','2021-03-31', 'category_2', 2],
['3','2021-03-31', 'category_3', 99],
['3','2021-03-31', 'category_4', 39]],
columns=['Account', 'Date', 'category', 'Amount'])
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Input(id="account", type="string", placeholder="Enter Account"),
DataTable(id='figure_1',
style_cell_conditional=[{'if': {'column_id': c}, 'textAlign': 'center'} for c in [0, 1, 2, 3, 4]],
style_as_list_view=True,
fill_width=True,
style_cell={'font-size': '12px'},
style_header={'display': 'none'},
style_table={'height': '395px', 'overflowY': 'auto'})])
@app.callback(dash.dependencies.Output('figure_1', 'data'),
[dash.dependencies.Input('account', 'value')])
def update_figura_2(account):
df_query = df[df['Account'] == account].copy()
df_query = df_query.groupby(['Account', 'Date', 'category']).agg({'Amount': 'sum'}).reset_index().pivot(values='Amount', columns='Date')
df_query.index = df.loc[df['Account'] == account, 'category'].copy()
df_query.fillna(0, inplace=True)
return df_query.to_dict(orient='records')
if __name__ == '__main__':
app.run_server(debug=False)
So the output i'd like to get:

However, when I run the code I get an empty Dataframe:

I suspect it has to be something with the callback, but I don't know what!
Should I define the columns parameter in the DataTable? If so, how could I get the columns of the callback?
