shiny.Session.on_flush#

Session.on_flush(fn, once=True)#

Register a function to call before the next reactive flush.

Parameters
  • fn (Callable[[], None]) – The function to call.

  • once (bool) – Whether to call the function only once or on every flush.

Return type

A function that can be used to cancel the registration.

Example

#| standalone: true
#| layout: vertical
from datetime import datetime
from shiny import *

app_ui = ui.page_fluid(
    ui.input_action_button("flush", "Trigger flush"),
    ui.output_ui("n_clicks"),
    ui.div(id="flush_time"),
)

def server(input: Inputs, output: Outputs, session: Session):
    def log():
        msg = "A reactive flush occurred at " + datetime.now().strftime("%H:%M:%S:%f")
        print(msg)
        ui.insert_ui(
            ui.p(msg),
            selector="#flush_time",
        )

    session.on_flush(log, once=False)

    @output
    @render.ui
    def n_clicks():
        return "Number of clicks: " + str(input.flush())


app = App(app_ui, server)