gradio组件的选项更新

我有两个下拉栏,第二个下拉栏需要根据第一个选项去更新。

踩的坑:

1、更新操作是使用gr.Dropdown.update(choices=[xxxxxx]),不能直接给choices赋值。

2、必须要给Dropdown对象建立.change()操作,否则下拉栏无法选择选项。

例子:

def upload_ori(test_folder_dropdown):
    test_folder_path = "./"
    return gr.Dropdown.update(choices=os.listdir(os.path.join(test_folder_path,test_folder_dropdown,"image")))

def upload_specific(test_file_dropdown):
    print(test_file_dropdown)
    return test_file_dropdown

with gr.Blocks(theme="freddyaboulton/dracula_revamped") as demo:
    with gr.Row():

        test_folder_dropdown = gr.inputs.Dropdown(choices=subfolders, label="Choose a folder")
        test_file_dropdown = gr.inputs.Dropdown(choices=["请先选择文件夹"],label="you can Choose a file or write a file name")


    test_folder_dropdown.change(fn=upload_ori,inputs=[test_folder_dropdown],outputs=[ test_file_dropdown]) 
    test_file_dropdown.change(fn=upload_specific,inputs=[test_file_dropdown],outputs=[test_file_dropdown])
    

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0",server_port=6666,debug=True,share=False)

你可能感兴趣的:(python,前端,gradio)