Hi guys, im fairly new to Dash and im trying to grasp as much as i can in this short period of time. There aren’t much references out there that addresses my problem.
Below is my code and im having trouble with updating the new datatable with the means of a groupby from the previous datatable.
please ignore the additional dropdown for now. I would like to fix the aforementioned issue…
import pandas as pd
import numpy as np
import dash
import dash_table as dt
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
# set display options
pd.set_option('display.width', None)
pd.set_option('display.float_format', lambda x: '%.3f' % x)
# import data
path = '/Users/jun/Downloads/Data Analyst Prework Dataset.csv'
df = pd.read_csv(path)
# pre-processing data
# define function to clean brand names
def rm_space(item):
if ' ' in item:
# Only prints the string without trailing white spaces
return item[:item.find(' ')]
else:
return item
def convert(x):
if x != 0:
return 100
else:
return 0
df['Transaction Date'] = pd.to_datetime(df['Transaction Date']).dt.strftime("%Y-%m")
df['Customer Id'] = df['Customer Id'].astype('str')
df = df.rename(columns = {'Customer Id': 'Customer ID'})
df['Brand'] = df['Brand'].apply(rm_space).str.upper()
# measuring the performances of the brands in terms of date, customer and sales agent
pivot1 = pd.crosstab([df['Transaction Date'], df['Customer ID'], df['Sales Agent ID']], df['Brand'])
pivot_reset = pivot1.applymap(convert).reset_index()
pivot2 = pivot_reset.groupby('Transaction Date').mean().T
pivot_reset2 = pivot2.reset_index()
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Supplier LTD Dataset', style={'text-align': 'center'}
),
dcc.Dropdown(
id='date_dropdown',
options=[{'label': i, 'value': i} for i in pivot_reset['Transaction Date'].unique()],
),
dcc.Dropdown(
id='customer_dropdown',
options=[{'label': i, 'value': i} for i in pivot_reset['Customer ID'].unique()],
),
html.Br(),
html.Label('Main Dataframe', style={'font-weight': 'bold'}),
dt.DataTable(
columns=[{'name': i, 'id': i} for i in pivot_reset.columns],
id='main_table',
data=pivot_reset.to_dict('records'), # the contents of the table
editable=False, # allow editing of data inside all cells
filter_action='none', # allow filtering of data by user ('native') or not ('none')
sort_action="native", # enables data to be sorted per-column by user or not ('none')
sort_mode="multi", # sort across 'multi' or 'single' columns
row_deletable=False, # choose if user can delete a row (True) or not (False)
selected_columns=[], # ids of columns that user selects
selected_rows=[], # indices of rows that user selects
page_action="native", # all data is passed to the table up-front or not ('none')
page_size=10, # number of rows visible per page
fixed_columns={'headers': True, 'data': 1},
style_table={'minWidth': '100%'},
style_cell={ # ensure adequate header width when text is shorter than cell's text
'minWidth': 100, 'maxWidth': 100, 'width': 100
},
style_data={ # overflow cells' content into multiple lines
'whiteSpace': 'normal',
'height': 'auto'
}
),
html.Hr(),
html.Label('Main Dataframe #2', style={'font-weight': 'bold'}),
dt.DataTable(
columns=[{'name': i, 'id': i} for i in pivot_reset2.columns],
id='brand_active_monthly',
data=pivot_reset2.to_dict('records'), # the contents of the table
editable=False, # allow editing of data inside all cells
filter_action='none', # allow filtering of data by user ('native') or not ('none')
sort_action="native", # enables data to be sorted per-column by user or not ('none')
sort_mode="multi", # sort across 'multi' or 'single' columns
row_deletable=False, # choose if user can delete a row (True) or not (False)
selected_columns=[], # ids of columns that user selects
selected_rows=[], # indices of rows that user selects
page_action="native", # all data is passed to the table up-front or not ('none')
page_current=0, # page number that user is on
page_size=50, # number of rows visible per page
fixed_columns={'headers': True, 'data': 1},
style_table={'minWidth': '100%'},
style_cell={ # ensure adequate header width when text is shorter than cell's text
'minWidth': 95, 'maxWidth': 95, 'width': 95
},
style_data={ # overflow cells' content into multiple lines
'whiteSpace': 'normal',
'height': 'auto'
}
)
])
@app.callback(
Output('main_table', 'data'),
[Input('customer_dropdown', 'value')]
)
def update_table(value):
dff = pivot_reset[pivot_reset['Customer ID'] == value]
return dff.to_dict('records')
# Callback to re-calculate means after filtering is
@app.callback(
Output('brand_active_monthly', 'data'),
[Input('main_table', 'data')]
)
def update_means(data):
# Calculate means from data currently stored in top datatable
dff_2 = pd.DataFrame.from_dict(data)
df_floats_means = dff_2.groupby(['Transaction Date']).mean(numeric_only=True)
df_t = df_floats_means.T
# Return means to means datatable
#THIS NOW RETURNS DICT INSTEAD OF LIST
T_means = df_t('records')
return T_means
if __name__ == '__main__':
app.run_server(debug=True)
Hopefully there are some kind souls out there who can shed some light.
Regards,
Jun