英文:
Taipy layout annotation text not updating itself when callback is triggered
问题
我正在尝试创建一个类似于这个(https://plotly.com/python/network-graphs/)的网络图。
一切都正常工作,直到我尝试在网络图中的链接旁边添加一些注释文本。
问题在于,当触发 on_change 函数时,注释文本不会自动更新自身。它只在刷新网页时更新。
我已经尝试在回调触发时打印文本的状态。我确实看到回调中的布局文本值已更新,只是在可视化中没有更新(除非刷新网页)。
英文:
I am trying to create a network graph like this (https://plotly.com/python/network-graphs/).
It all worked fine until I try to add some annotation texts next to the links in the network graph.
The issue is that the annotation text is not automatically updating itself when on_change function is triggered. It only gets updated when the webpage is refreshed.
I have tried to print the text in the state when the callback is triggered. I do see the layout text values are updated in the callback, it just doesn't update in the visuals (unless the web is refreshed).
答案1
得分: 1
我们在图表上有一个名为rebuild
的属性,用于在更改其配置时刷新图表。然而,注释仍然需要考虑此属性。
您现在可以使用局部部分来实现具有正确注释的图形。
下面的代码来自注释文档,其中包含实现注释更改的内容:
- 图表放在名为p的局部部分中;
- 页面上的按钮调用
change_annotation(state)
以更新注释; - 此函数使用新的注释更改layout并在最后更新局部部分。
from taipy.gui import Gui
def f(x):
return x*x*x/3-x
x = [(x-10)/4.5 for x in range(0, 21)]
data = {
"x": x,
"y": [f(x) for x in x]
}
layout = {
# 图表标题
"title": "局部极值点",
"annotations": [
# 局部最大值的注释 (x = -1)
{
"text": "局部 <b>最大值</b>",
"font": {
"size": 20
},
"x": -1,
"y": f(-1)
},
# 局部最小值的注释 (x = 1)
{
"text": "局部 <b>最小值</b>",
"font": {
"size": 20
},
"x": 1,
"y": f(1),
"xanchor": "left"
}
]
}
def change_annotation(state):
layout["annotations"][0]["text"] = "一般 <b>最大值</b>"
layout["annotations"][1]["text"] = "一般 <b>最小值</b>"
state.layout = layout
state.p.update_content(state, md_for_chart)
md_for_chart = "|{data}|chart|x=x|y=y|layout={layout}|"
md = """
<|part|partial={p}|
<|change annotation|button|on_action=change_annotation|
"""
gui = Gui(md)
p = gui.add_partial(md_for_chart)
gui.run()
英文:
We have a rebuild
property on charts to refresh the chart when its configuration is changed. However, annotations still need to be concerned by this property.
You can now use a partial to actualize your graph with the correct annotations.
The code below comes from the documentation on annotations with the changes to actualize annotations.
- The chart is put in a partial called p;
- A button on the page calls
change_annotation(state)
to update the annotations; - This function changes the layout with the new annotations and updates the partial at the end.
from taipy.gui import Gui
def f(x):
return x*x*x/3-x
x = [(x-10)/4.5 for x in range(0, 21)]
data = {
"x": x,
"y": [f(x) for x in x]
}
layout = {
# Chart title
"title": "Local extrema",
"annotations": [
# Annotation for local maximum (x = -1)
{
"text": "Local <b>max</b>",
"font": {
"size": 20
},
"x": -1,
"y": f(-1)
},
# Annotation for local minimum (x = 1)
{
"text": "Local <b>min</b>",
"font": {
"size": 20
},
"x": 1,
"y": f(1),
"xanchor": "left"
}
]
}
def change_annotation(state):
layout["annotations"][0]["text"] = "General <b>max</b>"
layout["annotations"][1]["text"] = "General <b>min</b>"
state.layout = layout
state.p.update_content(state, md_for_chart)
md_for_chart = "<|{data}|chart|x=x|y=y|layout={layout}|>"
md = """
<|part|partial={p}|>
<|change annotation|button|on_action=change_annotation|>
"""
gui = Gui(md)
p = gui.add_partial(md_for_chart)
gui.run()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论