【发布时间】:2019-07-06 09:46:52
【问题描述】:
我有一个简单的 Dash 应用程序,当单击“更新”按钮时,该应用程序会更新一个绘图。我在这张图上添加了一个可编辑的注释。我希望能够访问用户在注释中键入的任何内容,因此当图形更新时,注释保持不变。我主要想知道是否有办法访问他们如何编辑它。
我一直在尝试将当前注释保存到创建图形时访问的存储组件中。我尝试制作一个“保存”按钮,将存储数据更改为当前注释文本我唯一的猜测是,当注释被编辑时,新文本不会存储在默认文本所在的位置。那个,或者什么东西只是在我脑海里浮现,我没有意识到。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, State, Output
import random
app = dash.Dash(__name__)
app.config['suppress_callback_exceptions'] = True
app.layout = html.Div(
id = 'top_level',
children=[
html.Div(
id='plot_div',
children=[],
),
html.Button(
id = 'update',
children="Update",
),
dcc.Store(
id='annotation_storage',
data='Editable Annotation',
)
]
)
@app.callback(
Output('plot_div', 'children'),
[Input('update', 'n_clicks')],
[State('annotation_storage', 'data')]
)
def plot_update(clicked, annotation):
if clicked:
x = random.sample(range(1,101), 20)
y = random.sample(range(1,101), 20)
figure = {'data': [{'x': x, 'y': y,
'type': 'scatter', 'mode': 'markers'}],
'layout': {
'xaxis': {
'fixedrange': True,
'zeroline': False,
},
'yaxis': {
'fixedrange': True,
'zeroline': False,
},
'annotations': [{
'x': -0.05,
'y': 0.5,
'showarrow':False,
'text':annotation,
'xref':'paper',
'yref':'paper',
}],
}
}
return [dcc.Graph(
id = 'plot_output',
figure = figure,
config = {
'editable': True,
'edits': {
'axisTitleText': False,
'titleText': False,
},
'displayModeBar': False,
},
),
html.Button(
id = 'save_annotation',
children='Save',
),
]
@app.callback(
Output('annotation_storage', 'data'),
[Input('save_annotation', 'n_clicks')],
[State('plot_output', 'figure')]
)
def save_annotation(clicked, figure):
if clicked:
annotation = figure['layout']['annotations'][0]['text']
return annotation
if __name__ == '__main__':
app.run_server(debug=True, port=1000)
当前,即使用户对其进行了编辑,当图表更新时,注释也会恢复为默认文本“可编辑注释”。即使图表更新,我也希望注释保持不变。任何见解将不胜感激!谢谢!
【问题讨论】:
标签: python plotly-dash