Uh oh!
There was an error while loading. Please reload this page.
475 Pattern Matching Callbacks (codenamed wildcards) - #1103
Conversation
- new _validate.py to centralize error checking - dash exceptions auto-dedent - clean up usage and cruft
christianwengert
commented
Jan 31, 2020
Dear @alexcjohnson |
alexcjohnson
commented
Jan 31, 2020
@christianwengert Absolutely! Thank you for your patience with this. I will try to post some usage examples here later today. |
christianwengert
commented
Jan 31, 2020
via email
Yes, an usage example would be great for getting started … On 31 Jan 2020, at 15:44, alexcjohnson ***@***.***> wrote:
@christianwengert Absolutely! Thank you for your patience with this. I will try to post some usage examples here later today.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
To-Do list exampleThe importdashimportdash_core_componentsasdccimportdash_html_componentsashtmlfromdash.dependenciesimportInput, Output, State, MATCH, ALLapp=dash.Dash(__name__)
app.layout=html.Div([
html.Div('Dash To-Do list'),
dcc.Input(id="new-item"),
html.Button("Add", id="add"),
html.Button("Clear Done", id="clear-done"),
html.Div(id="list-container"),
html.Hr(),
html.Div(id="totals")
])
style_todo= {"display": "inline", "margin": "10px"}
style_done= {"textDecoration": "line-through", "color": "#888"}
style_done.update(style_todo)
@app.callback( [Output("list-container", "children"),Output("new-item", "value") ], [Input("add", "n_clicks"),Input("new-item", "n_submit"),Input("clear-done", "n_clicks") ], [State("new-item", "value"),State({"item": ALL}, "children"),State({"item": ALL, "action": "done"}, "value") ])defedit_list(add, add2, clear, new_item, items, items_done):
triggered= [t["prop_id"] fortindash.callback_context.triggered]
adding=len([1foriintriggeredifiin ("add.n_clicks", "new-item.n_submit")])
clearing=len([1foriintriggeredifi=="clear-done.n_clicks"])
new_spec= [
(text, done) fortext, doneinzip(items, items_done)
ifnot (clearinganddone)
]
ifadding:
new_spec.append((new_item, []))
new_list= [
html.Div([
dcc.Checklist(
id={"item": i, "action": "done"},
options=[{"label": "", "value": "done"}],
value=done,
style={"display": "inline"}
),
html.Div(text, id={"item": i}, style=style_doneifdoneelsestyle_todo)
], style={"clear": "both"})
fori, (text, done) inenumerate(new_spec)
]
return [new_list, ""ifaddingelsenew_item]
@app.callback(Output({"item": MATCH}, "style"), [Input({"item": MATCH, "action": "done"}, "value")])defmark_done(done):
returnstyle_doneifdoneelsestyle_todo@app.callback(Output("totals", "children"), [Input({"item": ALL, "action": "done"}, "value")])defshow_totals(done):
count_all=len(done)
count_done=len([dfordindoneifd])
result="{} of {} items completed".format(count_done, count_all)
ifcount_all:
result+=" - {}%".format(int(100*count_done/count_all))
returnresultif__name__=="__main__":
app.run_server(debug=True) |
alexcjohnson
commented
Feb 2, 2020
I noticed a problem with the To-Do app: if you delete all the items, the totals callback will not fire to say "0 of 0 items completed" as it should. This is because we currently only fire callbacks based on inputs that exist when they're fired, but for Working on a fix for this... |
alexcjohnson
commented
Feb 2, 2020
At a meeting where I demoed this feature, the consensus was We didn't discuss Give me a 👍 reaction (not a comment please) if the names |
nicolaskruchten
commented
Feb 2, 2020
|
alexcjohnson
commented
Apr 7, 2020
The issue @chriddyp mentioned #1103 (comment) from the docs, that the title never goes back from "Updating...", is still present. Investigating... |
alexcjohnson
commented
Apr 7, 2020
Ah got it - we're creating and throwing some errors that I forgot to trap, so I could clear the associated callback from the queue and surface the error in the devtools. The specific error we're hitting in the docs is (in case someone wants to fix it):
|
so we can clear the pendingCallbacks queue and show errors in the devtools
alexcjohnson
commented
Apr 7, 2020
After 7c47b05 (running docs with devtools on so we see the errors... with devtools off the title is still correct and the errors only appear in the js console) |
MM-Lehmann
commented
May 5, 2020
whats that rationale behind the restriction that I cannot combine |
alexcjohnson
commented
May 5, 2020
That’s essentially it - the value to return into that single output would be ill-defined, as it would be trying to get a separate value from each matched input. I see how specifically with In this case I would suggest either making a separate |
gmountee
commented
Sep 8, 2020
When I used two outputs, one of which uses MATCH and the other has nothing to do with 'index': it raised an error as follows: In the callback for output(s): Why is it necessary to require all the outputs with MATCH, and how can I solve this problem? |
alexcjohnson
commented
Sep 8, 2020
@lhsusan1016 as described in #1103 (comment) if there were two different elements that satisfy your You can solve this by either switching to an I happened to see your comment, but old closed PRs are not great places to start conversations. This kind of question is better at https://community.plotly.com |


Pattern Matching Callbacks! (Codenamed, Wildcards!)
➡️ See official documentation here: https://dash.plotly.com/pattern-matching-callbacks
Closes#475 - using the API discussed there (I'll post some examples soon, but for now here's the description):
id={"type": "cat", "age": 4, "purrs": True}MATCH,ALL, andALLSMALLER- and can be imported fromdash.dependencies. There are various rules about how these can combine, driven by the need to resolve these to a unique callback invocation for each set of outputs and identify uniquely the inputs required for this invocation...To do before this is ready:
Contributor Checklist
CHANGELOG.mdCloses#832 (among other callback bugs - the others all have commits that close them with matching tests, but #832 is a bit heavy for a test of its own)
Closes#1146