Wait for confirmation before performing an action

I wish to perform three successive measurements when a button is clicked. But the user should position a sensor before each measurement and the program should ask for confirmation before each measurement.
The dcc confirmDialog (ConfirmDialog | Dash for Python Documentation | Plotly) or confirmDialogProvider seem to be an option, but they are triggered when the user clicks a button.
Is there a way to trigger such a confirm dialog during the execution of a code?

Below is the structure of the callback executed when the button btn-start-meas is clicked where start_measure is a function that performs the desired measurement.

@app.callback(
Output(‘status’, ‘value’),
Input(‘btn-start-meas’, ‘n_clicks’),
prevent_initial_call = True
)
def make3measurements(btn):
<–Wait for confirmation–>
start_measure(1)
<–Wait for confirmation–>
start_measure(2)
<–Wait for confirmation–>
start_measure(3)

return 'Measurements done'

Hi @P67, welcome to the community!

How does the confirmation look like? Is it a user input?

Yes, the user should click a button to confirm that the code may continue.

Could you separate your process into three different callbacks?

I would try that and let the first callback trigger a popup with a confirm button, the click on this button triggers the second callback and so forth.

If you need results from the previous callback in a subsequent callback, you could use a dcc.Store() to store and retrieve the intermediate results.

Hello @P67,

@AIMPED is correct.

There is not a way to see any new information once inside of a callback.

What he is referring to here would be a chained callback. :slight_smile:

Here a basic example:

1 Like

Thanks a lot, you put me on the right track. Here is the code adapted from your suggestion :

import dash_bootstrap_components as dbc
from dash import Input, Output, State, html, dcc, ctx
import dash
import time

design of the modal

modal = html.Div(
[
dbc.Modal(
[
dbc.ModalHeader(“Position sensor confirmation”),
dbc.ModalBody( id = ‘modal-body’, children = “Ready for measurement 1 ?”),
dbc.ModalFooter(
children=[
dbc.ButtonGroup(
[
dbc.Button(
“OK”,
id=“ok”,
className=“ms-auto”,
n_clicks=0
),
dbc.Button(
“Cancel”,
id=“cancel”,
className=“ms-auto”,
n_clicks=0
)
]
)
]
),
],
id=“modal”,
is_open = False,
centered=True
),
]
)

app = dash.Dash(
name,
external_stylesheets=[dbc.themes.SLATE],
meta_tags=[
{‘name’: ‘viewport’,
‘content’: ‘width=device-width, initial-scale=1.0’
}
]
)
app.layout = html.Div(
[
dbc.Button(
“Start measurement”,
id=“startmeas-btn”,
n_clicks=0
),
dcc.Store(id = ‘nb_meas’, data = 0),
modal
]
)