gpt4 book ai didi

python - 如何将 uirevision 直接添加到 Plotly Dash 中的图形以进行自动更新

转载 作者:行者123 更新时间:2023-12-05 05:58:33 26 4
gpt4 key购买 nike

我有一个用 Python 构建的自动更新的 Plotly 图。即使有自动更新,我也想保留仪表板缩放。 Plotly 中的文档说这可以使用布局 uirevision 字段完成,根据 this community writeup .文档将此作为返回字典的示例:

 return {
'data': data,
'layout': {
# `uirevsion` is where the magic happens
# this key is tracked internally by `dcc.Graph`,
# when it changes from one update to the next,
# it resets all of the user-driven interactions
# (like zooming, panning, clicking on legend items).
# if it remains the same, then that user-driven UI state
# doesn't change.
# it can be equal to anything, the important thing is
# to make sure that it changes when you want to reset the user
# state.
#
# in this example, we *only* want to reset the user UI state
# when the user has changed their dataset. That is:
# - if they toggle on or off reference, don't reset the UI state
# - if they change the color, then don't reset the UI state
# so, `uirevsion` needs to change when the `dataset` changes:
# this is easy to program, we'll just set `uirevision` to be the
# `dataset` value itself.
#
# if we wanted the `uirevision` to change when we add the "reference"
# line, then we could set this to be `'{}{}'.format(dataset, reference)`
'uirevision': dataset,

'legend': {'x': 0, 'y': 1}
}
}

然而,我的身材更像是这样 build 的:

import plotly.express as px

@app.callback(
Output("graph", "figure"),
[Input("interval-component", "n_intervals")])
def display_graph(n_intervals):
# Logic for obtaining data/processing is not shown
my_figure = px.line(my_data_frame, x=my_data_frame.index, y=['line_1', 'line_2'],
title='Some Title', template='plotly_dark')
return my_figure

换句话说,既然我返回的不是字典,而是直接以绘图形式表达的图形,我如何才能直接访问 uirevision 值,以便保留用户的 UI 更改?

最佳答案

可以使用图中的update_layout成员函数。

my_figure.update_layout(uirevision=<your data>)

更多信息在这里:https://plotly.com/python/creating-and-updating-figures/#updating-figure-layouts

关于python - 如何将 uirevision 直接添加到 Plotly Dash 中的图形以进行自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68460544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com