问题描述
假设我有一个 dash
应用程序,我希望在页面重新加载时刷新全局数据.我正在使用一个函数来提供布局,如此处所述.但是,我确定我应该如何/在哪里定义 df
以便我可以在回调中使用它(例如在我想对 df
进行子集化的情况下)基于一些输入并将其传递给布局表).我下面的代码在页面刷新时重新加载数据,但回调无法访问 df
.
Imagine I have a dash
application where I want the global data to refresh on page reload. I'm using a function to serve the layout as described here. However, I'm note sure how/where I should define df
such that I can use it in callbacks (like in a case where I'd like to subset the df
based on some input and pass it to a layout table). My code below reloads the data on page refresh, but the callback cannot access the df
.
我对 dash
很陌生,所以提前为可能愚蠢的问题道歉.
I'm very new to dash
so apologies in advance for potentially dumb question.
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
对象中,
The most common approach for sharing data between callbacks is to save the data in a dash_core_components.Store
object,
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
参数,
You can then add the store as a State
argument for the callbacks that need access to the data,
@app.callback(..., [State("mystore", "data")])
def my_func(..., data):
df = pd.read_json(data)
这种方法的主要缺点是每次调用回调时都会在客户端和服务器之间交换数据.如果数据帧很小,这并不重要,但如果数据帧很大,则数据交换(以及与 JSON 之间的序列化)可能会导致严重的性能问题.它可以通过缓存数据框服务器端来避免,或者按照 documentation 或使用 dash-extensions
中的丰富组件.这是后者的一个小例子,
The main drawback of this approach is that the data is exchanged between the client and the server each time a callback is invoked. If the data frame is small, it doesn't really matter, but if it is large, the data exchange (and the serialization to/from JSON) might cause severe performance issues. It can be avoided by caching the data frame server side, either manually as demonstrated in the documentation or using the enriched components from dash-extensions
. Here is a small example of the latter,
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
的作者.
tested with dash-extensions==0.0.27rc1
. Disclaimer: I am the author of dash-extensions
.
这篇关于在重新加载时绘图刷新全局数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!