pythoninteractiveshiny-reactivitypy-shiny

Way to make input_text_area appear reactively when option is selected from input_radio_buttons in Python Shiny


I'm developing a web app in Shiny express for Python. I've implemented a group of radio buttons so that users can try out some pre-defined input for the app. One of the buttons is labelled "I want to enter my own input". When a user selects this button, I'd like a text box to appear reactively so they can type their input there.

This is what I have so far:

# imports
from shiny.express import input, render, ui # interactivity
from shiny import reactive

# make radio buttons with options, assign user's choice to var1
ui.input_radio_buttons(
                'var1',
                'Please select some input',
                {'A': 'Option 1', 'B': 'Option 2', None: 'I want to enter my own input'}
)
# make sure the following events occur after the user has interacted with the radio buttons above
@reactive.effect
@reactive.event(input.var1)
def check_var1():
# if user has selected 'I want to enter my own input' above (so var1 == None)
# assign var1 to the input they put in the text box
    if input.var1 is None:
        ui.input_text_area('var1', 'Please enter your input here')

When I run this in a browser, the web page loads. When I select the "I want to enter my own input" button, I get the following error messages, and the text box doesn't appear:

Traceback (most recent call last):output_obs
    value = await renderer.render()

I'm guessing that I'm not nesting things properly and/or I haven't configured reactivity properly.

Thanks in advance for any help you can provide!

EDIT:

I've also tried specifying return within the check_var1 function like so:

def check_var1():
# if user has selected 'I want to enter my own input' above (so var1 == None)
# assign var1 to the input they put in the text box
    if input.var1 is None:
        return ui.input_text_area('var1', 'Please enter your input here')

This isn't working either.


Solution

  • I found a solution:

    # imports
    from shiny.express import input, render, ui # interactivity
    from shiny import reactive
    
    # make radio buttons with options, assign user's choice to var1
    ui.input_radio_buttons(
                    'var1',
                    'Please select some input',
                    {'A': 'Option 1', 'B': 'Option 2', '1': 'I want to enter my own input'}
    )
    # ui.panel_conditional makes a ui component appear based on a condition
    # for some reason you can't check against None, so you have to assign a different value
    # also, the variable assigned here has to be DIFFERENT than the variable assigned 
    # above (not var1)
    with ui.panel_conditional("input.var1 === '1'"):
        ui.input_text_area("var2", "Please enter your own input")
    

    Details here: https://shiny.posit.co/py/docs/ui-dynamic.html