当从Python Shiny中的input_radio_buttons中选择选项时,使input_text_area反应性地出现的方法

问题描述 投票:0回答:1

我正在使用 Shiny Express for Python 开发一个 Web 应用程序。我实现了一组单选按钮,以便用户可以尝试应用程序的一些预定义输入。其中一个按钮标有“我想输入自己的输入”。当用户选择此按钮时,我希望反应性地出现一个文本框,以便他们可以在其中输入输入。

这是我到目前为止所拥有的:

# 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')

当我在浏览器中运行此程序时,会加载网页。当我选择“我想输入自己的输入”按钮时,我收到以下错误消息,并且不出现文本框:

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

我猜我没有正确嵌套东西和/或我没有正确配置反应性。

预先感谢您提供的任何帮助!

编辑:

我还尝试在

return
函数中指定
check_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:
        return ui.input_text_area('var1', 'Please enter your input here')

这也不起作用。

python interactive shiny-reactivity py-shiny
1个回答
0
投票

我找到了解决方案:

# 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")

详细信息在这里:https://shiny.posit.co/py/docs/ui-dynamic.html

© www.soinside.com 2019 - 2024. All rights reserved.