I have successfully deployed my dash app on Heroku following the standard method:
- app.py contains all the commands, including:
app = dash.Dash(…)
server = app.server
if name == ‘main’:
- The Procfile contains just one line: “web: gunicorn app:server”.
Everything works well, except now I want to modify variables dynamically etc, so I built a class DashApp which defines the data generation, all the variables, the layout, and the callbacks separately. In a nutshell, my code now looks like this:
class DashApp: def __init__(self): #definitions of all variables etc. self.myvar = blabla #definitions of the app & layout: self.app = dash.Dash(some_options) self.app.layout = self.create_layout() self.app.callback(Outputs, Inputs)(self.update_data) # various functions that do various things, including: def create_layout(): blabla def update_data(): blabla ------------------------ if __name__ == '__main__': app = DashApp() app.app.run_server(some_options) ------------------------
I basically just put the definition of the app into a class and instantiate the class to run the app. If I run this locally directly with python, everything runs smoothly, including the callbacks. Now, if I want to deploy this on Heroku, I need to correctly define the server attribute contained in the Procfile. How does that work? I tried different approaches, but none of them work as I wish:
- If I define it as usual with “server = app.app.server” after the app instantiation (below app = DashApp(), after the if-statement), I get the error “Failed to find attribute ‘server’ in ‘app’”.
- To avoid this error, I tried to put the instantiation app = DashApp() before the if-statement (making it an attribute of the module) and added server = app.server right after. This avoids the error of point 1) and the app loads, but the callbacks stop working.
- I tried to put the server definition in the init function of the class, but that doesn’t work either (same error as 1)).
- I also tried to modify the Procfile to “web: gunicorn app.app:server” and “web: gunicorn app:app.server” but neither works.
Does anybody know a workaround for this? It would be much appreciated! I’ve been struggling the whole day