Python plotly-dash中的模式匹配回调
使用plotly dash,我试图让ListGroup项目作为按钮,在单击时更新其标签。下面是按预期工作的代码,但我已经为每个按钮手动创建了一个回调Python plotly-dash中的模式匹配回调,python,plotly-dash,Python,Plotly Dash,使用plotly dash,我试图让ListGroup项目作为按钮,在单击时更新其标签。下面是按预期工作的代码,但我已经为每个按钮手动创建了一个回调 import dash import dash_bootstrap_components as dbc import dash_html_components as html from dash.dependencies import Input, Output app = dash.Dash(__name__, external_stylesh
import dash
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = (
html.Div([
dbc.ListGroup([
dbc.ListGroupItem(id="button1", n_clicks=0, action=True),
dbc.ListGroupItem(id="button2", n_clicks=0, action=True),
dbc.ListGroupItem(id="button3", n_clicks=0, action=True),
])
])
)
@app.callback(Output("button1", "children"), [Input("button1", "n_clicks")])
def clicked1(n_clicks):
return f"Button 1: clicked {n_clicks} times"
@app.callback(Output("button2", "children"), [Input("button2", "n_clicks")])
def clicked2(n_clicks):
return f"Button 2: clicked {n_clicks} times"
@app.callback(Output("button3", "children"), [Input("button3", "n_clicks")])
def clicked3(n_clicks):
return f"Button 3: clicked {n_clicks} times"
if __name__ == '__main__':
app.run_server(debug=True)
按钮会在单击时显示并更新。“调试”菜单中的回调图显示了我的预期效果。
现在,我想用一个使用模式匹配的回调函数替换这三个回调函数。我的想法是,我可以有一个任意数量的项目动态列表,它们都会按预期更新。按照中描述的方式,我将代码更改为在项和回调中使用id字典
import dash
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output, MATCH
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = (
html.Div([
dbc.ListGroup([
dbc.ListGroupItem(id={"type": "button", "index": 0}, n_clicks=0, action=True),
dbc.ListGroupItem(id={"type": "button", "index": 1}, n_clicks=0, action=True),
dbc.ListGroupItem(id={"type": "button", "index": 2}, n_clicks=0, action=True),
])
])
)
@app.callback(Output({"type": "button", "index": MATCH}, "children"), [Input({"type": "button", "index": MATCH}, "n_clicks")])
def clicked(n_clicks):
return f"Button clicked {n_clicks} times"
if __name__ == '__main__':
app.run_server(debug=True)
现在按钮什么也不显示(表明回调没有运行),它们是可点击的,但当它们被点击时什么也没有发生,回调图是一团混乱
据我所知,我已经按照指示做了。我没有收到任何错误,只是不起作用。我遗漏了什么?发现问题在于版本控制 我使用anaconda安装了dash,并且anaconda主存储库具有dash依赖项的非常旧的版本。因此,尽管我有dash版本1.16.3,回调中的模式匹配是在1.11中引入的,但模式匹配也需要dash渲染器版本1.4或更高版本,我有1.1.2 我的解决方案是从conda forge安装dash,它拥有所有dash依赖项的最新版本
conda安装-c conda forge dash
发现问题在于版本控制
我使用anaconda安装了dash,并且anaconda主存储库具有dash依赖项的非常旧的版本。因此,尽管我有dash版本1.16.3,回调中的模式匹配是在1.11中引入的,但模式匹配也需要dash渲染器版本1.4或更高版本,我有1.1.2
我的解决方案是从conda forge安装dash,它拥有所有dash依赖项的最新版本
conda安装-c conda forge dash
我很高兴您解决了问题
我只想建议对代码进行改进:
def group_item(index, click=None, action=False):
return dbc.ListGroupItem(id={"type": "button", "index": index}, n_clicks=click, action=action)
app.layout = (
html.Div([
dbc.ListGroup(
[group_item(i, click=0, action=True) for i in range(3)]
)
])
)
希望它能对你有用。我很高兴你解决了你的问题 我只想建议对代码进行改进:
def group_item(index, click=None, action=False):
return dbc.ListGroupItem(id={"type": "button", "index": index}, n_clicks=click, action=action)
app.layout = (
html.Div([
dbc.ListGroup(
[group_item(i, click=0, action=True) for i in range(3)]
)
])
)
希望它能对您有用。不确定,但有两个想法:您是否尝试过为
子项道具设置一个显式值以开始?甚至子项=''
。另外,您是否尝试过将组件拆分为按钮和文本组件?我确实尝试过先显式地设置子组件,这没有什么区别。如果回调运行,则设置子项,如果不是,则设置为。我没有尝试过拆分组件,但自从我的第一个示例起作用以来,似乎是模式匹配被破坏了,因此我也看不出这有什么帮助。不确定,但有两个想法:您是否尝试过为子项道具设置显式值以开始?甚至子项=''
。另外,您是否尝试过将组件拆分为按钮和文本组件?我确实尝试过先显式地设置子组件,这没有什么区别。如果回调运行,则设置子项,如果不是,则设置为。我没有尝试过拆分组件,但自从我的第一个示例起作用以来,似乎是模式匹配被破坏了,所以我也看不出这有什么帮助。