I created an Upload component that writes the uploaded file to the filesystem. Because this will generally overwrite anything that’s already there, I want to place the upload component inside a confirm dialog.
Ideally, this dialog would only show up if the file is already in the filesystem, but for now I’m just trying to get it to work.
This is my code so far:
import base64 import io import dash from dash.dependencies import Input, Output, State import dash_core_components as dcc import dash_html_components as html import dash_table import pandas as pd external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.layout = html.Div([ dcc.ConfirmDialogProvider( children=[ dcc.Upload(children=html.Button('Upload File'), id='upload'), html.Div(children=html.Div(['Upload file']), id='uploadconfirmation')], id='uploadconfirmprovider', message='Existing files will be overwritten. Are you sure you want to proceed?' ), ]) # callback for file upload def parse_contents(contents, filename): try: if contents == None: return 'Upload file' else: content_type, content_string = contents.split(',') decoded = base64.b64decode(content_string) df = pd.read_excel(io.BytesIO(decoded), ['tables', 'populations']) writer = pd.ExcelWriter('views/123.xlsx') for sheet_name in df.keys(): df[sheet_name].to_excel(writer, sheet_name=sheet_name, index=False) writer.save() return html.Div(['Successfully uploaded new data file']) except: return html.Div(['There was an error processing this file.']) @app.callback(Output('uploadconfirmation', 'children'), [Input('uploadconfirmprovider', 'submit_n_clicks')], [State('upload', 'contents'), State('upload', 'filename'), ]) def update_output(submit_n_clicks, contents, filename): if not submit_n_clicks: return 'Upload file' children = parse_contents(contents, filename) return children if __name__ == '__main__': app.run_server()
Unfortunately, it doesn’t trigger the confirmdialog and it breaks the actual fileupload (which works fine without the confirmation dialog.
Edit: Removed some non-essential code from the example.