I hope someone can help me. I’m developing an app with Dash, in which one can upload some files at runtime using
dcc.Upload. Then, I need to create some callbacks, depending on the uploaded file. Unfortunately, I can’t define the callbacks before running the server (
app.run_server(debug=True)), since they depend on the uploaded data.
My idea was to “restart” the whole app in a callback, e.g. create a new app with all the callbacks, and then run the new app by
app.run_server(debug=True). Unfortunately, I always get a
ValueError: signal only works in main thread.
The following callback is triggered when I selected the files and hit an “Upload Button”.
@app.callback(Output("dummy-div", "children"), [Input("upload-button", "n_clicks")]) def update(upload_button): # A new layout is calculated from the uploaded data layout = setup_new_dash_layout() # A new app is created in my dash_app_handler dash_app_handler.create_new_app() # The layout of this new app will be set dash_app_handler.set_layout(layout) # The callbacks depending on the new data will be created add_callbacks(dash_app_handler) # The new app should be restarted again dash_app_handler.run_server()
This is how
dash_app_handler looks like:
class DashAppHandler(): def __init__(self): self.app = dash.Dash(name=__name__, external_stylesheets=external_stylesheets, ) self._add_empty_layout() def run_server(self, debug=True): self.app.run_server( debug=debug, ) def set_layout(self, layout): self.app.layout.children = layout def create_new_app(self): self.app = dash.Dash(name=__name__, external_stylesheets=external_stylesheets) self._add_empty_layout() def _add_empty_layout(self): self.app.layout = html.Div(children=, id='app_layout')
The easiest solution I think would be to add callbacks to the existing running app. But I haven’t found a way to modify the callbacks in a running server (app.run_server).
Any ideas how to get it work? Or any other ideas how to solve my problem?
Any help is highly appreciated! Thanks in advance!