testmode.snapshot_preprocess_input

testmode.snapshot_preprocess_input(id, fn)

Set a function for preprocessing an input value in test-mode snapshots.

Uses the current reactive session; equivalent to session.input.set_snapshot_preprocess(id, fn). See shiny.session.Inputs.set_snapshot_preprocess for details. Inside a module, id is resolved in the module's namespace.

Parameters

id : str

The ID of the input value.

fn : Callable[[Any], Any] | Callable[[Any], Awaitable[Any]]

A function (synchronous or asynchronous) that takes the input value and returns the value to write to the test snapshot.

Raises

: RuntimeError

If there is no active session.

See Also

Examples

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

## file: app.py
from shiny import App, Inputs, Outputs, Session, render, ui
from shiny.testmode import snapshot_preprocess_input

app_ui = ui.page_fluid(
    ui.input_text("secret", "Secret", value="hunter2"),
    ui.output_text_verbatim("shout"),
)


def server(input: Inputs, output: Outputs, session: Session):
    # Scrub the sensitive value from test-mode snapshots; the live input value
    # is untouched. Has no effect unless test mode is enabled
    # (`App(test_mode=True)` or `SHINY_TESTMODE=1`), so the call can be left in
    # production code.
    snapshot_preprocess_input("secret", lambda value: "<redacted>")

    @render.text
    def shout() -> str:
        return str(input.secret()).upper()


app = App(app_ui, server)