Schedule a callback at a user defined time

Hi all,

I wanted to let the user define a time, on which a certain callback should be triggered and came up with the following concept. I am pretty sure there is room for improvement, so I am looking for feedback :slight_smile:

from dash import Dash, Input, Output, State, html, dcc
import dash_mantine_components as dmc
import datetime

app = Dash(__name__)

app.layout = html.Div(
    [
        html.H4('Enter start time for the callback'),
        dmc.TimeInput(
            id='time_in',
            style={"width": 150},
            withSeconds=True,
        ),
        html.Br(),
        html.Button(
            'submit',
            id='btn'
        ),
        dcc.Interval(
            id='interval',
            interval=1000,
            n_intervals=0,
            max_intervals=0
        ),
        html.Br(),
        html.Div(id='dump'),
    ]
)


@app.callback(
    Output('interval', 'max_intervals'),
    Output('interval', 'interval'),
    Input('btn', 'n_clicks'),
    State('time_in', 'value'),
    prevent_initial_call=True
)
def update(_, value):
    # convert into datetime object
    start_time = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')

    # get current system time
    now = datetime.datetime.now()

    # how long until the callback should get triggered (in milliseconds)?
    difference = (start_time - now).total_seconds() * 1000

    return 1, difference


@app.callback(
    Output('dump', 'children'),
    Output('interval', 'n_intervals'),
    Input('interval', 'n_intervals'),
    State('interval', 'interval'),
    prevent_initial_call=True
)
def out(_, wait):
    return html.H4(
        [
            f'You had to wait {wait} milliseconds',
            html.Br(),
            f'The callback finished {datetime.datetime.now()}'
        ]
    ), 1


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

Hey @AIMPED, what kind of use case are you thinking of for this? The main issue I see with a solution like this is that the user’s tab need to stay open until the specified time in order for the callback to be triggered.

For scheduled tasks I would probably rely on services like Google Cloud Tasks or the equivalent in other Cloud providers.

1 Like

Hi @RenaudLN, thanks for your answer which intrigues me.

I was wondering, if it is possible to trigger callbacks from within the app at a certain time and I started to play around. I did not have any use case in mind but, one might be a reminder modal or something for the user at a certain time.

How would you use Google cloud tasks for triggering a callback? Or am I getting it wrong and you are suggesting something different? It would be nice if you could point me into the right direction.

If the use case is purely frontend like a user reminder on the UI, then your solution would work.

I was more thinking of a scheduled task where something needs to happen on the server, say send an email reminder, and you want the task to happen whether or not the user is still using the app.

You may be able to trigger a callback from a cloud task by using a websocket. Haven’t tried it though ^^

Even for scheduling a reminder, using something that is solely interval based on the clientside would only work if the browser was up the whole time. Otherwise it would reload and never trigger. XD

Now, I could see using it for something where the user only wants to refresh a datatable every so often. But for this, you would need to allow for them to adjust it and store it in a server so it is applicable upon load for their site. :slight_smile:

In the above instance, the table’s info would load immediately and then also at their selected interval.