Python 更新Dash中多个绘图的绘图
我有一个非常基本的问题,主要是了解Dash在变量更改时如何处理图形更新过程。我刚刚开始构建一个仪表板,但仍然不熟悉完整的语法 我的布局中有一个绘图、一个下拉框、回调函数和更新函数,如下所示:Python 更新Dash中多个绘图的绘图,python,plotly,dashboard,plotly-dash,Python,Plotly,Dashboard,Plotly Dash,我有一个非常基本的问题,主要是了解Dash在变量更改时如何处理图形更新过程。我刚刚开始构建一个仪表板,但仍然不熟悉完整的语法 我的布局中有一个绘图、一个下拉框、回调函数和更新函数,如下所示: @app.callback( Output(component_id='plot1', component_property='figure'), Input(component_id='drop1', component_property='value') ) def update_gra
@app.callback(
Output(component_id='plot1', component_property='figure'),
Input(component_id='drop1', component_property='value')
)
def update_graph(sel):
""" Stuff for updating the plot """
return fig
这很好,但我不完全理解“为什么”
现在,我的问题是
提前谢谢 好的,我将用一个图表解释plotly文档中的一个简单示例。一旦我们理解了一个图,那么多个图就更容易理解了
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.H6("Change the value in the text box to see callbacks in action!"),
html.Div(["Input: ",
dcc.Input(id='my-input', value='initial value', type='text')]),
html.Br(),
html.Div(id='my-output'),
])
@app.callback(
Output(component_id='my-output', component_property='children'),
Input(component_id='my-input', component_property='value')
)
def update_output_div(input_value):
return 'Output: {}'.format(input_value)
if __name__ == '__main__':
app.run_server(debug=True)
因此,首先让我们注意输入和输出组件\u id
。请注意,输入的组件\u id
与dcc.input
id匹配。匹配的id表示装饰程序@app.callback
和布局对象之间存在链接。这个链接意味着一旦输入发生变化,就会调用decorator,decorator将查看其各自的输出id。decorator随后将寻求更新输出组件,即HTML div。为此,decorator将始终查看其正下方的函数,在这种情况下,哪个是更新\u输出\u div
。已更改的值(输入)将传递到此函数
好的,现在来看多重图。在下面的代码中,我将省略app.layout
声明,只需假设下面的每个ID(square、cube-twos、threes、x^x、num-multi、dropdown)都链接到对应的ID
@app.callback(
Output('square', 'children'),
Output('cube', 'children'),
Output('twos', 'children'),
Output('threes', 'children'),
Output('x^x', 'children'),
Input('num-multi', 'value'),
Input('dropdown', 'value2'))
def callback_a(v, v2):
# update
同样,装饰程序只需查找任何输入中的更改,然后通过回调函数更新每个输出
来回答你的第一个问题。回调函数总是直接调用它自己下面的函数,而不需要隐式编码。您当然可以决定在该函数中调用其他函数。例如,如果我有多个可能不相关的输入,我可以查找实际触发的内容
@app.callback(
Output('map', 'figure'),
Input('map','clickData'),
Input('map','selectedData'))
def update_scatter_plots(
ctx = dash.callback_context
if ctx.triggered[0]['prop_id'] == 'map.clickData':
#it was a click
elif ctx.triggered[0]['prop_id'] == 'map.selectedData':
#it was a selection
这里我有一个映射图,我有两个不同的输入一个单击事件和一个选择事件,根据上下文,我可以决定如何处理传递的信息
至于你的第二个问题,请让我知道你是否想让我详细说明。我将留给其他人为你写一个简短、准确的解释。同时,阅读并应该对整个事情有所启发。太好了!我假设它是下面的函数,但仍然不是100%确定。我来自Shinny in R,那里的“回调”实际上是包装的,所以调用哪个函数是显而易见的,但这里仍然给我提出了几个问题。我会接受你的帖子作为答案,因为这是一个很好的澄清和解释,非常感谢!请随意评论您的其他问题。我可以编辑帖子来回答them@DevinBurke&鬼这变成了一个非常好的帖子!如果您有后续问题,请考虑在新岗位上询问。多次跟进和编辑可以很快将一个有价值的资源(比如你们两位在这里的医学博士)变成一团乱麻。期待着你们两位做出更多的贡献!