[Python] What is the purpose of the output parameter in the server function of a shiny app?

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    In R, the output is used to render outputs by assigning the result of a render function to some element of the output object. It does not seem to work this way in python.

    Some example python code:

    def server(input: Inputs, output: Outputs, session: Session) -> None:
    @render.plot
    def hist() -> None:
    hue = "species" if input.species() else None
    sns.kdeplot(df, x=input.var(), hue=hue)
    if input.show_rug():
    sns.rugplot(df, x=input.var(), hue=hue, color="black", alpha=0.25)


    In python, rendering is controlled with decorators and having a function name matching the name of a component defined in the UI. So when is the output parameter used for anything?

    Full working sample


    import seaborn as sns

    # Import data from shared.py
    from shared import df
    from shiny import App, render, ui

    app_ui = ui.page_sidebar(
    ui.sidebar(
    ui.input_select(
    "var", "Select variable", choices=["bill_length_mm", "body_mass_g"]
    ),
    ui.input_switch("species", "Group by species", value=True),
    ui.input_switch("show_rug", "Show Rug", value=True),
    ),
    ui.output_plot("hist"),
    title="Hello sidebar!",
    )


    def server(input, output, session):
    @render.plot
    def hist():
    hue = "species" if input.species() else None
    sns.kdeplot(df, x=input.var(), hue=hue)
    if input.show_rug():
    sns.rugplot(df, x=input.var(), hue=hue, color="black", alpha=0.25)


    app = App(app_ui, server)

    Continue reading...

Compartilhe esta Página