In Plotly Dash, I am trying to
What I tried:
import dash
app = dash.Dash(__name__)
if app.server.debug is False:
print("Not in Debug mode")
# app.logger.addHandler(mail_handler)
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=True)
print(f"app.server.debug is {app.server.debug}") # This code only executes after the server is shut down
I tried app.server.debug
(and app.server.config["DEBUG"]
) but both always return False. So I am not able to determine if the app is actually in debug mode or not.
This is my console output:
Not in Debug mode Dash is running on http://127.0.0.1:8050/ * Serving Flask app 'example_code' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on Not in Debug mode
I assume this confusion is because the FLASK_DEBUG
environment variable is not set, but even so, it does say * Debug mode: on
so how to identify this during runtime?
And finally where do I add this debug mode check and change the handler - the debug is set in app.run_server()
but adding any code immediately after that only executes after the server is shut down.
Maybe this approach will work for you? Just set a callback to run only once per program execution. (sorry for using globals, couldn't think of a cleaner way).
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import logging
app = dash.Dash(__name__)
app.layout = html.Div([dcc.Store(id='dummy_store')])
logger = logging.getLogger(__name__)
LOGGER_CONFIGURED = False
@app.callback(
Output('dummy_store', 'data'),
Input('dummy_store', 'data'),
)
def configure_logger(dummy_store):
global LOGGER_CONFIGURED
if LOGGER_CONFIGURED is False:
print(f'configured logger with debug mode set to {app.server.debug}')
# do something with the logger here
LOGGER_CONFIGURED = True
if __name__ == '__main__':
app.run_server(debug=True, use_reloader=True)