How can I prevent Dash reloader from re-running database connection on every file save?

3 days ago 1
ARTICLE AD BOX

I'm building a Dash dashboard that loads data from a remote database (Redshift) on startup. The loading takes ~2 minutes. When I use use_reloader=True, every time I save any .py file in the project (including layout files), the reloader restarts the entire process and reconnects to the database.

What I want: Edit layout files (ventas.py, filtros.py) and see the changes reflected without reconnecting to the database.

Minimal reproduction:

# main.py import os import psycopg2 import pandas as pd import dash from dash import html estado = {} def cargar_datos(): print("Connecting to DB...") # expensive — takes ~2 minutes conn = psycopg2.connect(host="...", port=5439, database="...", user="...", password="...") estado["data"] = pd.read_sql("SELECT * FROM my_table", conn) conn.close() cargar_datos() app = dash.Dash(__name__) app.layout = html.Div("hello") if __name__ == "__main__": app.run(debug=True, use_reloader=True) # ventas.py — editing and saving this file triggers a full reload from dash import html layout = html.Div("Ventas")

What happens:

Every time I save ventas.py, the terminal shows Connecting to DB... again — even though I only changed the layout.

Read Entire Article