My app restart with first load

Hi Dash community!

So I have an app that, when I launched it with the debug mode on, it starts normally, then it says: * Restarting with stat, restarts and then everything works smoothly.
I wonder why it restarts once or how I can debug what’s going on, since no any other file is being modified on start up.

Besides, in production mode it works good (It doesn’t watch for changed in local files).

1 Like

That is normal debug behavior, the application in a subprocess in order to reload the application on file change.

Hi. I bring up this topic since I’m struggling again to get rid of that extra launching of the app.

So, when I launch my application for first time under the development environment, it starts the app twice.
First time it seems like it starts, it shows the “Running on” statement, Debugger PIN and the bullet points list starting with the first one “Serving app”…but it stops the app straightaway. Immediately, it launches the app for second time, but this time it successfully hangs on waiting for user interaction. There’s no any message of file change detection between these two launches.

Curiously enough, this only happens in development, because in production it only launches once, as expected.

I think this is not normal behaviour as I haven’t seen this extra launching with other simpler apps also in debug mode.

As Philippe already said, and I can confirm from my usage it is normal behavior

Hi.

This behaviour is causing me troubles when I want to launch the app with python -m and I have to do relative imports in there.

So, Let’s say I want to run the dash app as a module using the python -m flag. Inside the __main__.py, I import the factory function to create the dash instance I want to launch, first time the __main__ file runs it works correctly, but with the second round that the debugger mode triggers it fails. I’ve been messing around and this is because the sys.path changes from the initial, normal, launch; to the debugger subprocess launch.

This is my wikichron/__main__.py code (summarized for clarity):

# External imports
import os
from urllib.parse import urljoin, parse_qs
import pandas as pd

import sys
print(f'This is the sys.path:{sys.path}')

# flask imports
import flask

# local imports
from .app import create_dash_app, set_up_app # ¡WITH SECOND LAUNCH, IT FAILS HERE!

# load config
from .dash_config import DevelopmentConfig
wikichron_base_pathname = DevelopmentConfig.DASH_BASE_PATHNAME;
port = DevelopmentConfig.PORT;

print(f'Starting execution of __main__')

global debug;
debug = True if os.environ.get('FLASK_ENV') == 'development' else False

def run(app):
    print('Start running the app standalone...')
    app.run_server(debug=debug, port=port)
    return


server = flask.Flask(__name__)

# load config for flask server
server.config.from_object(DevelopmentConfig)

# create and config Dash instance
app = create_dash_app(server)

# set layout, import startup js and bind callbacks
set_up_app(app)

run(app)

If I launched the app with python3 -m wikichron/, I’d get this:

Generating available metrics...
=> You are in DEBUG MODE <=
['', '/home/akronix/.pyenv/versions/3.6.7/lib/python36.zip', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6/lib-dynload', '/home/akronix/.pyenv/versions/wikichron/lib/python3.6/site-packages']
FOOOOO
Creating new Dash instance...
Binding callbacks...
Setting up layout...
¡¡¡¡ Welcome to WikiChron 2.0.0-alpha !!!!
Using version 0.39.0 of Dash.
Using version 0.20.0 of Dash renderer.
Using version 0.44.0 of Dash Core Components.
Using version 0.3.4 of Grasia Dash Components.
Using version 0.14.0 of Dash Html Components.
Start running the app standalone...
Running on http://127.0.0.1:8880/app/
Debugger PIN: 040-845-928
 * Serving Flask app "__main__" (lazy loading)
 * Environment: development
 * Debug mode: on
['/home/akronix/workspace/wikis_research/WikiChron/wikichron/dash/apps/classic', '/home/akronix/.pyenv/versions/3.6.7/lib/python36.zip', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6', '/home/akronix/.pyenv/versions/3.6.7/lib/python3.6/lib-dynload', '/home/akronix/.pyenv/versions/wikichron/lib/python3.6/site-packages']
Traceback (most recent call last):
  File "/home/akronix/workspace/wikis_research/WikiChron/wikichron/dash/apps/classic/__main__.py", line 13, in <module>
    from .app import create_dash_app, set_up_app
ModuleNotFoundError: No module named '__main__.app'; '__main__' is not a package

As you can see, the path is correct for the first launch, but incorrect for the second launch thus it fails. This is the source of the problem. Is there any way to pass the current path to the process which monitor the dash process?

If I run not in debug mode, the app launches successfull, as expected.

You can see the full code here.