-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
SQL querying and updating a global variable in callback function isn't working - Broken pipe #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
+1. I described a similar issue here |
Thanks for reporting @eloiup and @wtluke ! I just tried to replicate this but was unable to. Here is a simple, self-contained SQL example that uses SQLite (it generates the SQLite database on start) import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from sqlalchemy import create_engine
# Create a simple database
engine = create_engine('sqlite:///sample.db')
df = pd.DataFrame({
'a': [1, 2, 3, 4, 5, 6],
'b': ['x', 'y', 'x', 'x', 'z', 'y']
})
df.to_sql('dataframe', engine, if_exists='replace')
# Dash
def generate_table(dataframe, max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
app = dash.Dash()
app.layout = html.Div([
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in df.b.unique()],
value='x',
clearable=False
),
html.Div(id='table-container')
])
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('dropdown', 'value')])
def sql(value):
dff = pd.read_sql_query(
'SELECT a, b FROM dataframe WHERE b = "{}"'.format(value),
engine
)
return generate_table(dff)
if __name__ == '__main__':
app.run_server(debug=True) |
It would be helpful to get a little bit more info:
Thanks for the help! This is a weird issue for sure |
hi! 1.i am on windows. its kind of different from the example you gave here. since my query takes a while to run (1-2 minutes), and it only needs to be run every 1-2 days, what i was trying to achieve was to update a global variable (dataframe) every X interval instead. hence the issue might be related to the different approach. |
Ahhh, thanks for clarifying @eloiup ! Yes, global variables aren't safe in Dash as not all of the processes share the same memory. As a rule of thumb, it's not safe to change any variables outside of the scope of a callback function. You can read from global variables, but you can't write to them. In the meantime, I recommend using Let me know if that works! |
yeah i am running/killing the script periodically to update the data, not the optimal solutions but works. tried reading the plugins link (thanks) but i am not much of a dev myself (data scientist / BI analyst). |
Even I tried to update a global variable from callback but it is failing consistently; I will try out flask caching |
Is anyone still seeing this? I'm going to close due to inactivity, but feel free to reopen |
Hi,
I am having an issue with dash when trying to do live updates
The example at https://plot.ly/dash/live-updates updates simple text components and the graph directly.
What i would like to do is to update a dataframe, on which i produce the graphs/texts through other callbacks such as radio buttons and dropdowns.
The live update component works perfectly well for simple updates (changing text/components), just as in the example.
But when I try to set up a new dataframe, through retrieving some data from a sql database periodically, it crashes. They way I do it is the following:
Make a callback:
Within the function that feeds on the callback:
note: the connector/cursor code is fine, it works perfectly outside the function
note2: the dash works perfectly without the connector/cursor/dataframe update
error message:
any hints/advices? is what i am trying to do feasable?
or shld i have a graph update function/callback instead of trying to update the dataframe?
ty
The text was updated successfully, but these errors were encountered: