Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python for shiny, How to access input id that gernerated by iteration?

I am new in python for shiny.I have create text input ui from iteration that number of iteration come from user input(in example code it is 5 times).But I can not access their ID because in shiny it must call 'input.<ID>()' and deny by python input() method.Anyone can solve this.

from shiny import App, ui, reactive, render

def input_txt(id):
    return (
        ui.input_numeric(id=id, label=id, value=1, min=1, max=20)
    
    )

my_ui = ui.page_fluid(
    ui.input_action_button("button1", "Click me", class_="btn-primary"),
    ui.hr(),
    ui.output_ui('output1'),
    ui.input_action_button("button2", "Submit", class_="btn-primary"),
)

def server(input, output, session):
    @reactive.Calc()
    def input_block():
        list = []
        for i in range(5):
            list.append(input_txt('input_'+str(i+1))) # <-- I names input ID here
        return list

    @output
    @render.ui
    @reactive.event(input.button1)
    def output1():
        txt = input_block()
        return txt

    ''' How to access input id, input value of input text ui which created by iteration
    if number of iteration is not fix with 5, but it is depend on user input ?'''

    @reactive.Effect()
    @reactive.event(input.button2)
    def get_text():
        values = []
        for i in range(5):
            values.append(input.input_1()) # <-- I am stucking here. How to access all input id
        
        print(*values)

app = App(ui=my_ui, server=server)

I run this in console it's ok except cannot access ID.

% shiny run --reload app1.py
like image 853
Suzanoo''' Avatar asked Feb 24 '26 07:02

Suzanoo'''


1 Answers

The shiny team provided an answer:

Use input\['input_1'\]() instead of input.input_1() and it's fine.

like image 67
Suzanoo''' Avatar answered Feb 25 '26 20:02

Suzanoo'''



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!