reactive.Effect

reactive.Effect(fn=None, *, suspended=False, priority=0, session=MISSING)

Mark a function as a reactive side effect.

A reactive effect is like a reactive calculation (calc) in that it can read reactive values and call reactive calculations, and will automatically re-execute when those dependencies change. But unlike reactive calculations, it doesn't return a result and can't be used as an input to other reactive expressions. Thus, reactive effects are only useful for their side effects (for example, performing I/O).

Another contrast between reactive calculations and effects is their execution strategy. Reactive calculations use lazy evaluation; that is, when their dependencies change, they don't re-execute right away but rather wait until they are called by someone else. Indeed, if they are not called, then they will never re-execute. In contrast, effects use eager evaluation; as soon as their dependencies change, they schedule themselves to re-execute.

Parameters

suspended : bool = False

If TRUE, start the effect in a suspended state (i.e., it will not execute until resumed and invalidated).

priority : int = 0

The new priority. A higher value means higher priority: an effect with a higher priority value will execute before all effects with lower priority values. Positive, negative, and zero values are allowed.

session : ‘MISSING_TYPE | Session | None’ = MISSING

A Session instance. If not provided, the session is inferred via get_current_session.

Returns

: Effect_ | Callable[[EffectFunction | EffectFunctionAsync], Effect_]

A decorator that marks a function as a reactive effect (Effect_).

Tip

Reactive effects are analagous to observers in Shiny for R.

See Also

Examples

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file: app.py
from shiny import App, Inputs, Outputs, Session, reactive, ui

app_ui = ui.page_fluid(ui.input_action_button("btn", "Press me!"))


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.effect
    @reactive.event(input.btn)
    def _():
        ui.insert_ui(
            ui.p("Number of clicks: ", input.btn()),
            selector="#btn",
            where="afterEnd",
        )


app = App(app_ui, server)