File size: 4,187 Bytes
e2bf898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a0a132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2bf898
3a0a132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2bf898
 
 
3a0a132
e2bf898
 
 
 
 
 
3a0a132
 
 
 
 
 
 
 
 
e2bf898
3a0a132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

# def words():
#     sentence = "A test of Gradio"
#     words = sentence.split()
#     update_show = [gr.Button.update(visible=True, value=w) for w in words]
#     update_hide = [gr.Button.update(visible=False, value="") for _ in range(10-len(words))]
#     return update_show + update_hide 


# import gradio as gr

# def words():
#     sentence = "A test of Gradio"
#     words = sentence.split()
#     update_show = [gr.Button(visible=True, value=w) for w in words]
#     update_hide = [gr.Button(visible=False, value="") for _ in range(10-len(words))]
#     return update_show + update_hide 

# btn_list = []
    
# with gr.Blocks() as demo:
#     with gr.Tab():
#         for i in range(10):
#             btn = gr.Button(visible=False)
#             btn_list.append(btn)
#     b = gr.Button("Run")
#     b.click(words, None, btn_list)
        
# demo.launch()



import gradio as gr



def add_one_btn(*btn_list,button_limit=10):
    print(f'input is {btn_list},type is {type(btn_list)},len is {len(btn_list)},type[0] is {type(btn_list[0])}')
    # global visible_btn_num
    # global button_limit
    # visible_btn_num = sum([1 for btn in btn_list if btn.visible])
    visible_btn_num = sum([1 for btn in btn_list if len(btn)>0])
    print(f'visible_btn_num: {visible_btn_num}')


    if visible_btn_num<button_limit:
        visible_btn_num += 1
    else:
        print(f'button limit {button_limit} reached')
    print(f'len(btn_list): {len(btn_list)}')
    words = btn_list[:visible_btn_num]#sentence.split()
    update_show = [gr.Textbox(visible=True, value=w,interactive=True) for w in words]
    update_hide = [gr.Textbox(visible=False, value="",interactive=True) for _ in range(button_limit-len(words))]
    # update_hide = [gr.Textbox(visible=False, value="",interactive=True) for _ in range(10-len(words))]
    rtn_list=update_show + update_hide 
    # print(f'len(rtn_list): {len(rtn_list)}')
    return rtn_list

def remove_one_btn(*btn_list,button_limit=10):
    # global visible_btn_num
    # count the visible btn in the btn_list
    # visible_btn_num = sum([1 for btn in btn_list if btn.visible])
    visible_btn_num = sum([1 for btn in btn_list if len(btn)>0])
    print(f'visible_btn_num: {visible_btn_num}')

    # global button_limit
    if visible_btn_num>0:
        visible_btn_num -= 1
    else:
        print(f'button limit {button_limit} reached')
    # print(f'len(btn_list): {len(btn_list)}')
    words = btn_list[:visible_btn_num]#sentence.split()
    update_show = [gr.Textbox(visible=True, value=w,interactive=True) for w in words]
    update_hide = [gr.Textbox(visible=False, value="",interactive=True)for _ in range(button_limit-len(words))]
    # update_hide = [gr.Textbox(visible=False, value="",interactive=True) for _ in range(10-len(words))]
    rtn_list=update_show + update_hide 
    print(f'len(rtn_list): {len(rtn_list)}')
    return rtn_list

    
def get_text_content(*btn_list):
    print(f'input is {btn_list}')
    rtn =' '.join([w for w in btn_list ])

    print(rtn)
    return  rtn




if __name__ == '__main__':
    button_limit=10
    visible_btn_num = 4
    btn_list = []
    from functools import partial
    add_one_btn_partial = partial(add_one_btn, button_limit=button_limit)#, visible_btn_num=visible_btn_num)
    remove_one_btn_partial = partial(remove_one_btn, button_limit=button_limit)#, visible_btn_num=visible_btn_num)
        
    with gr.Blocks() as demo:
        with gr.Row():
            for i in range(button_limit):
                if i<visible_btn_num:
                    btn = gr.Textbox(visible=True)
                else:
                    btn = gr.Textbox(visible=False)
                btn_list.append(btn)
        b = gr.Button("add_one_choice")
        print(f'len(btn_list): {len(btn_list)}')
        b.click(add_one_btn_partial, btn_list, btn_list)
        b = gr.Button("remove_one_choice")
        b.click(remove_one_btn_partial, btn_list, btn_list)

        # print(f'len(btn_list): {len(btn_list)}')

        print(btn_list)


        b = gr.Button("Get Text Content")
        output = gr.Textbox()
        b.click(get_text_content, btn_list, output)
            
    demo.launch(debug=True)