javascript - 有没有办法将 Python 变量传递给 Plotly Dash clientside_callback?
问题描述
我正在尝试在将执行 JavaScript 脚本的 Plotly Dash 中进行客户端回调。我在脚本的其他地方定义了一个 Python 变量,现在我想将该变量传递给我有我的脚本的客户端回调。这是代码片段:
python_variable = 'string or some variable'
app.clientside_callback(
"""
function(n_clicks, user_input) {
if (n_clicks){
alert(%{user_input} + %{python_variable});
}
}
""",
Output('dummy_div', 'children'),
[Input('btn', 'n_clicks'),
Input('user_input', value)]
我不知道如何将我的 python_variable 放在 dcc.Store 中,因为我在页面加载期间加载变量(对此没有回调)。是否可以将我的 Python 变量添加到我的客户端回调函数中?
解决方案
您可以直接将变量传递给布局data
中组件的属性。dcc.Store
然后,您可以将Store
组件的data
属性作为State
.
MWE:
from dash import Dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
python_variable = "string or some variable"
app = Dash(__name__)
app.layout = html.Div(
[
dcc.Input(id="input", value="", type="text"),
html.Div(id="output"),
dcc.Store(id="store", data=python_variable),
]
)
app.clientside_callback(
"""
function(input, python_variable) {
return `${python_variable}: ${input}`
}
""",
Output("output", "children"),
Input("input", "value"),
State("store", "data"),
)
if __name__ == "__main__":
app.run_server()
推荐阅读
- express - PassportJs 身份验证允许双方(来宾和登录用户)执行的操作
- flutter - Flutter web - 在悬停时如何更改 Flatbutton TEXT 颜色
- php - 意外的异步行为
- javascript - 无法使用 blobUrl 上传视频文件
- java - ServletResponse 输出流不使用大写 .PDF 写入文件
- linux-kernel - 如何在 Linux 内核驱动程序 (stmmac) 中访问以太网帧
- vue.js - Vue 生命周期是如何工作的?并将其用于骨架和加载
- python - 我无法理解我在 Kaggle 比赛中的训练和测试数据做错了什么
- facebook-graph-api - Facebook 设备登录
- php - 如何为 WooCommerce 创建多个结帐页面?