Daily Tips - Give your Component a Name ✍

Hi there,

It’s been a while since version 2.1 came out. As you know, this version adds support for assignment expressions. Have you tried it?

And when I use VS Code, it helps me highlight the variable by default when I move the cursor over. However, if I hadn’t made the extra settings, it wouldn’t have helped me check the strings. Em, you know, if I carelessly write two IDs, id='table' and id='tabel', I probably find it out until the process reports an error. But if they’re variables, it’s different, and I will get a very obvious underline.

from dash import html, dcc, Dash, Output, Input, State, callback_context, no_update

app = Dash(__name__)

app.layout = html.Div([my_input := dcc.Input(), my_output := html.Div()])

@app.callback(Output(my_output, 'children'), Input(my_input, 'value'))
def update(value):
    return value

if __name__ == "__main__":
    app.run_server(debug=True)

Then you might ask, where did my ID go? What if I want to use callback_context.triggered to know which component triggers the callback?

Don’t worry. Let me print it out.
print(my_btn.id)

82e2e662-f728-b4fa-4248-5e3a0a5d2f34

See, dash gave it a component ID in UUID format. Then you can use it in callbacks.

if callback_context.triggered[0]["prop_id"].split(".")[0] != my_btn.id:
    return no_update

Hope this helps you. XD

Keywords: Assignment Expression, Component ID

5 Likes