Hi @AIMPED ,
Thanks for the reply!
To be more specific on a few of the callback pieces:
**On the upload File button/Datable interaction (perhaps my biggest confusion)
- I’ve reduced to code to the following to only display the contents of one sheet .I am not getting any errors on the file processing portion of the callback but the Datable is not appearing within the dash app.
@app.callback(Output('output-data', 'children'),
Input('upload-data', 'contents'),
[State('upload-data', 'filename'),
State('upload-data', 'last_modified')], prevent_inital_call=True)
def update_output(contents, filename, last_modified):
if contents is not None:
# Get Taxonomy Requirements
taxonomy_df = [
parse_contents(c, n, d, sheet='MASTER SOURCE TAB', rows_to_skip=1) for c, n, d in
zip(contents, filename, last_modified)]
# Get Placements
trafficking_df = [
parse_contents(c, n, d, sheet='Social Trafficking', rows_to_skip=6) for c, n, d in
zip(contents, filename, last_modified)]
return html.Div([
dash_table.DataTable(
data=taxonomy_df,
columns=taxonomy_df.columns,
page_size=15)
])
On the upload File button/ drop down interaction:
- when the user uploads a file will that file store in the State parameter similar to a dcc.Store() where the callback can access the raw file once the submit button is triggered and perform its transformations?
Note: I’ve taken your suggestion and have copied the code below into text rather than images. (Thanks again for your help and tips, very appreciated!)
Here is the app layout:
app.layout = dbc.Container([
# Upload file
dbc.Row([dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Do not allow multiple files to be uploaded
multiple=False
)]),
# Select check to perform
dbc.Row([dbc.Select(
id='channel-QA-chosen',
placeholder = 'Select A Channel Taxonomy....',
options= [
{'label': 'Programmatic-Display', 'value': 'Programmatic-Direct'},
{'label': 'Social', 'value': 'Social'},
{'label': 'Print', 'value': 'Print', 'disabled': True}
])]),
# Button to trigger check
dbc.Row([dbc.Button(
id='submit-button', children = "Run QA", n_clicks=0)]),
# Output discrpency tables
dbc.Row([
html.Div(id='output-data')])
], fluid=True)
Here is the original Callback code:
@app.callback(Output('output-data', 'children'),
Input('submit-buttom', 'n_clicks'), # Add submit button and over arching if statement (if n is none: return no update, else do something)
[State('channel-QA-chosen', 'options'),
State('upload-data', 'contents'),
State('upload-data', 'filename'),
State('upload-data', 'last_modified')], prevent_inital_call=True)
def update_output(n_clicks, options, contents, filename, last_modified):
if n_clicks is None:
None
else:
if contents is not None and options == 'Social':
# Get Taxonomy Requirements
taxonomy_df = [
parse_contents(c, n, d, sheet='MASTER SOURCE TAB', rows_to_skip=1) for c, n, d in
zip(contents, filename, last_modified)]
# Get Placements
trafficking_df = [
parse_contents(c, n, d, sheet='Social Trafficking', rows_to_skip=6) for c, n, d in
zip(contents, filename, last_modified)]
trafficking_df = trafficking_df['Package / Placement Name'] # Extract Placement/Package name column
search_discreps = trafficking_df.str.split('_', expand = True) # split placement name on '_' delimiter
search_discreps.rename(columns=soc_dict, inplace=True) # rename columns
discrepencies_table = run_QA(taxonomy_df=taxonomy_df, workbook_df=trafficking_df, search_discreps=search_discreps, options=options)
elif contents is not None and options == 'Programmatic-Display':
# Get Taxonomy Requirements
taxonomy_df = [
parse_contents(c, n, d, sheet='MASTER SOURCE TAB', rows_to_skip=1) for c, n, d in
zip(contents, filename, last_modified)]
# Get Placements
trafficking_df = [
parse_contents(c, n, d, sheet='ProgDirect Trafficking', rows_to_skip=6) for c, n, d in
zip(contents, filename, last_modified)]
trafficking_df = trafficking_df['Package / Placement Name'] # Extract Placement/Package name column
search_discreps = trafficking_df.str.split('_', expand = True) # split placement name on '_' delimiter
search_discreps.rename(columns=disp_dict, inplace=True) # rename columns
discrepencies_table = run_QA(taxonomy_df=taxonomy_df, workbook_df=trafficking_df, search_discreps=search_discreps, options=options)
elif contents is not None and options == 'Print':
# Get Taxonomy Requirements
taxonomy_df = [
parse_contents(c, n, d, sheet='MASTER SOURCE TAB', rows_to_skip=1) for c, n, d in
zip(contents, filename, last_modified)]
# Get Placements
trafficking_df = [
parse_contents(c, n, d, sheet='Print Trafficking', rows_to_skip=6) for c, n, d in
zip(contents, filename, last_modified)]
trafficking_df = trafficking_df['Package / Placement Name'] # Extract Placement/Package name column
search_discreps = trafficking_df.str.split('_', expand = True) # split placement name on '_' delimiter
search_discreps.rename(columns=dict,inplace=True) # rename columns
discrepencies_table = run_QA(taxonomy_df=taxonomy_df, workbook_df=trafficking_df, search_discreps=search_discreps, options=options)
return html.Div([
dash_table.DataTable(
data=discrepencies_table,
columns=discrepencies_table.columns,
page_size=15)
])