FreshBench / gradio_samples /add_components.py
jijivski
wait for soft plotly
3a0a132
# 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)