【发布时间】:2022-09-24 09:17:52
【问题描述】:
如何在 plotly 中制作交互式按钮,这将更改图表并将它们的交互发送到 streamlit?
我已经尝试在 plotly https://plotly.com/python/custom-buttons/#relayout-button 中使用内置按钮
这适用于更改图表,但无法将行为作为点击事件的交互发送到此处https://plotly.com/python/click-events/
目前在 streamlit 中只有一个库来获取 plotly 图表的交互,据我所知,在引擎盖下它使用 plotly 事件 https://github.com/null-jones/streamlit-plotly-events
所以我能想出的唯一解决方案是创建第二个图表作为子图并将其样式设置为看起来像一个按钮。这是一个重大的黑客攻击,并且出现了许多危险信号,但我想不出另一种方法来做到这一点。
这是我到目前为止所拥有的:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2, column_widths=[0.1, 0.7])
fig.add_trace(
go.Bar(x=[1, 1], y=[\'foo\', \'bar\'], orientation=\'h\', width=.4, hoverinfo=\'skip\'),
row=1, col=1)
fig.add_trace(
go.Bar(x=[1, 1], y=[\'foo\', \'bar\'], orientation=\'h\', width=.4, hoverinfo=\'skip\'),
row=1, col=1)
fig.add_trace(
go.Bar(
x=[1, 2],
y=[\'foo\', \'bar\'],
orientation=\'h\',
name=\'revenue\',
width=.4
),
row=1, col=2)
fig.add_trace(
go.Bar(
x=[2, 4],
y=[\'foo\', \'bar\'],
orientation=\'h\',
name=\'potential\',
width=.4
),
row=1, col=2)
fig.update_yaxes(matches=\'y\')
fig.update_layout(barmode=\'stack\',
title_text=\"Multiple Subplots with Shared Y-Axes\")
fig.show()
我错过了什么吗?
标签: plotly mouseclick-event streamlit