Plotly dash 在重新加载时刷新全局数据
Plotly dash refreshing global data on reload
假设我有一个 dash
应用程序,我希望在其中刷新页面重新加载时的全局数据。我正在使用一个函数来提供布局,如 here 所述。但是,我注意到 how/where 我应该定义 df
以便我可以在回调中使用它(比如在我想根据某些输入对 df
进行子集化的情况下并将其传递给布局 table)。我下面的代码在页面刷新时重新加载数据,但回调无法访问 df
.
我是 dash
的新手,所以提前为可能愚蠢的问题道歉。
def serve_layout():
df = # Fetch data from DB
return # Layout
app.layout = serve_layout
@app.callback()
def my_func:
# Here I want to reference df
在回调之间共享数据的最常见方法是将数据保存在 dash_core_components.Store
对象中,
def serve_layout():
df = # Fetch data from DB
store = Store(id="mystore", data=df.to_json()) # The store must be added to the layout
return # Layout
然后您可以将商店添加为需要访问数据的回调的 State
参数,
@app.callback(..., [State("mystore", "data")])
def my_func(..., data):
df = pd.read_json(data)
这种方法的主要缺点是数据在客户端和服务器之间交换每次调用回调。如果数据帧很小,这并不重要,但如果它很大,数据交换(和序列化 to/from JSON)可能会导致严重的性能问题。可以通过缓存数据框服务器端来避免这种情况,如 documentation 中所示手动或使用 dash-extensions
中的丰富组件。下面是后者的一个小例子,
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import pandas as pd
from dash_extensions.enrich import Dash, ServersideOutput, Output, Input, Trigger
app = Dash()
app.layout = html.Div([dcc.Store(id="store"), # this is the store that holds the data
html.Div(id="onload"), # this div is used to trigger the query_df function on page load
html.Div(id="log")])
@app.callback(ServersideOutput("store", "data"), Trigger("onload", "children"))
def query_df():
return pd.DataFrame(data=np.random.rand(int(10)), columns=["rnd"]) # some random example data
@app.callback(Output("log", "children"), Input("store", "data"))
def print_df(df):
return df.to_json() # do something with the data
if __name__ == '__main__':
app.run_server()
使用 dash-extensions==0.0.27rc1
进行了测试。免责声明:我是 dash-extensions
.
的作者
假设我有一个 dash
应用程序,我希望在其中刷新页面重新加载时的全局数据。我正在使用一个函数来提供布局,如 here 所述。但是,我注意到 how/where 我应该定义 df
以便我可以在回调中使用它(比如在我想根据某些输入对 df
进行子集化的情况下并将其传递给布局 table)。我下面的代码在页面刷新时重新加载数据,但回调无法访问 df
.
我是 dash
的新手,所以提前为可能愚蠢的问题道歉。
def serve_layout():
df = # Fetch data from DB
return # Layout
app.layout = serve_layout
@app.callback()
def my_func:
# Here I want to reference df
在回调之间共享数据的最常见方法是将数据保存在 dash_core_components.Store
对象中,
def serve_layout():
df = # Fetch data from DB
store = Store(id="mystore", data=df.to_json()) # The store must be added to the layout
return # Layout
然后您可以将商店添加为需要访问数据的回调的 State
参数,
@app.callback(..., [State("mystore", "data")])
def my_func(..., data):
df = pd.read_json(data)
这种方法的主要缺点是数据在客户端和服务器之间交换每次调用回调。如果数据帧很小,这并不重要,但如果它很大,数据交换(和序列化 to/from JSON)可能会导致严重的性能问题。可以通过缓存数据框服务器端来避免这种情况,如 documentation 中所示手动或使用 dash-extensions
中的丰富组件。下面是后者的一个小例子,
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import pandas as pd
from dash_extensions.enrich import Dash, ServersideOutput, Output, Input, Trigger
app = Dash()
app.layout = html.Div([dcc.Store(id="store"), # this is the store that holds the data
html.Div(id="onload"), # this div is used to trigger the query_df function on page load
html.Div(id="log")])
@app.callback(ServersideOutput("store", "data"), Trigger("onload", "children"))
def query_df():
return pd.DataFrame(data=np.random.rand(int(10)), columns=["rnd"]) # some random example data
@app.callback(Output("log", "children"), Input("store", "data"))
def print_df(df):
return df.to_json() # do something with the data
if __name__ == '__main__':
app.run_server()
使用 dash-extensions==0.0.27rc1
进行了测试。免责声明:我是 dash-extensions
.