shiny.Session.on_ended#

Session.on_ended(fn)#

Registers a function to be called after the client has disconnected.

Parameters

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

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("close", "Close the session"),
)

def server(input: Inputs, output: Outputs, session: Session):
    def log():
        print("Session ended at: " + datetime.now().strftime("%H:%M:%S"))

    session.on_ended(log)

    @reactive.Effect
    @reactive.event(input.close)
    async def _():
        await session.close()


app = App(app_ui, server)