[Suggestion] Mention that app.layout must not be placed inside `if name == main:`

I suggest that we add something like the following line in the Dash User Guide page on Layouts:
“The instantiation of app.layout must not be placed inside of the conventional if __name__ == __main__ otherwise the layout will not be detected when the app is deployed.”

This happened to me as I was making my first Dash app. I instantiated my app.layout inside of
if __name__ == __main__
The sneaky thing about this particular issue, is that the app works when you run it locally, but results in an Internal Server Error when deployed (I was deploying to Heroku).

The Officiall Dash User Guide should be something that a first-time Dash learner can completely follow, resulting in his/her first successful Dash App without going through a troubleshooting headache. I believe this will help ensure that first time Dash learners who are learning from the User Guide will avoid the incident I had endured.

Technically, the User Guide never tells me to put app.layout inside of if __name__ == __main__ but the reason I tried to put it in there was that in some python code styles and conventions, all code that is not meant to run when a python file is imported is placed inside of if __name__ == __main__. Well, I thought that wasn’t the case for instantiating app.layout, but it turns out it is.

1 Like

I will add a suggestion on top of yours regarding deployment. There should have a place in the docs stating that the WSGI compatible object in Dash is app.server and not app itself. In many services (like Google App Engine) the default object used to start the gunicorn server is app, which I imagine it can be confusing for beginners as well.

2 Likes

Thank you for your feedback, @jlfsjunior and @human42.
Our Docs team will take a look at this and see what updates to the docs could be made based on their suggestion.

1 Like