I’ve got a dash app in production which is backed by a snowflake (AWS hosted SQL service) instance. Snowflake has a 4 hour timeout on any of their connections, so I need to architect this in a way to keep the connection open. Let’s assume for the time being that I can’t edit the 4 hour timeout. My thought is there are two ways around this…
-
establish the connection (in app.py) on the client side, so that a new conn is established when a user hits the dash application. The four hours shouldn’t present an issue in this scenario. Am I correct in understanding that establishing the connection object in app.py (and then using that connection object on subsequent queries) is happening on the server side in dash’s current architecture?
-
I recently setup dcc.Interval() to run a dummy query every hour to keep the connection open, but reading the forums it looks like dcc.Interval() is run on the client side (and I understand why that would be, but it sounds like it doesn’t help me out here).
So given the above, is there a current methodology to keep my connection purely client-side or run some queries server side that keep the connection alive? App layout and pseudo-code below.
repo
- app.py
- index.py
- apps
- app1.py
- app2.py
app.py
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP,'.assets/custom.css'])
conn = utils.conn_fcn()
server = app.server
app.config.suppress_callback_exceptions = True
index.py
def layout():
return html.Div(id = 'first-div',
children=[
####### RANDOM QUERY TO RUN AND KEEP THE CONN ALIVE
dcc.Interval(
id='interval-component',
interval=60*60*1000, # in milliseconds
n_intervals=0
),
html.Div(id='i-dont-care',style={"display":"none"}),
dcc.Location(id='url',refresh=False),
html.Div(id='outermost-white-div',
children=[row,body]
)
]
)
#this runs a dummy query every hour to keep the connection open
@app.callback(Output('i-dont-care', 'children'),
[Input('interval-component', 'n_intervals')])
def run_dummy(n):
print("i ran the timing query")
df = pd.read_sql('''SELECT * FROM MY_TABLE LIMIT 1''',conn)
return df.to_json()
app1.py
def return_charts():
df = pd.read_sql('''SELECT * FROM DATA_TABLES''',conn)
x = []
for i in df['i']:
x.append(build_chart(df,i))
return x
def layout():
body = html.Div(
dbc.Row(html.Div(id='charts', children=return_charts()))
)
return body```