Skip to content

Conversation

@wch
Copy link
Collaborator

@wch wch commented Feb 23, 2023

This adds a method for Inputs.__contains__, so that the user can write code like this:

@Effect
def foo():
    if "x" in input:
        ...

When in is used, Shiny creates a reactive dependency. If input.x starts out nonexistent and then later gains a value, then foo will be invalidated and re-executed. After that point, input.x() changes value, then this will not cause foo to invalidate -- the invalidation only occurs when

When x is not yet set from the client, the interface here makes it look like x is not in the Input object (when input.x() and "x" in input are called). The underlying implementation actually does add x to the Inputs's internal _map object, but that is hidden from the user.

Test application:

from shiny import App, Inputs, Outputs, Session, render, ui

app_ui = ui.page_fluid(
    ui.input_checkbox("show_input_txt", "Show text input"),
    ui.output_text_verbatim("txt_status"),
    ui.output_ui("dyn_ui"),
)


def server(input: Inputs, output: Outputs, session: Session):
    @output
    @render.ui
    def dyn_ui():
        if input.show_input_txt():
            return ui.input_text("txt", "Text input:")

    n_exec = 0

    @output
    @render.text
    def txt_status():
        nonlocal n_exec
        n_exec += 1
        return f'"txt" in input: {"txt" in input}\nThis output has executed {n_exec} times.'

app = App(app_ui, server)

@wch wch requested a review from jcheng5 February 23, 2023 04:50
@wch wch merged commit 7626a74 into main Feb 23, 2023
@wch wch deleted the input-contains branch February 23, 2023 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants