2023-11-28 18:27:45 +08:00
|
|
|
import gradio as gr
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
base_root = Path(__file__).parent.resolve()
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
2023-12-13 08:39:11 +08:00
|
|
|
with gr.Row():
|
|
|
|
dd = gr.Dropdown(label="Select File Explorer Root",
|
|
|
|
value=str(base_root / "dir1"),
|
|
|
|
choices=[str(base_root / "dir1"), str(base_root / "dir2"),
|
|
|
|
str(base_root / "dir3")])
|
|
|
|
with gr.Group():
|
2024-02-14 06:51:47 +08:00
|
|
|
txt_only_glob = gr.Checkbox(label="Show only text files", value=False)
|
|
|
|
ignore_txt_in_glob = gr.Checkbox(label="Ignore text files in glob", value=False)
|
2023-12-13 08:39:11 +08:00
|
|
|
|
2024-02-14 06:51:47 +08:00
|
|
|
fe = gr.FileExplorer(root_dir=str(base_root / "dir1"),
|
2023-12-13 08:39:11 +08:00
|
|
|
glob="**/*", interactive=True)
|
|
|
|
textbox = gr.Textbox(label="Selected Directory")
|
|
|
|
run = gr.Button("Run")
|
2024-02-17 02:03:10 +08:00
|
|
|
total_changes = gr.Number(0, elem_id="total-changes")
|
2023-12-13 08:39:11 +08:00
|
|
|
|
2024-02-14 06:51:47 +08:00
|
|
|
txt_only_glob.select(lambda s: gr.FileExplorer(glob="*.txt" if s else "*") ,
|
|
|
|
inputs=[txt_only_glob], outputs=[fe])
|
|
|
|
ignore_txt_in_glob.select(lambda s: gr.FileExplorer(ignore_glob="*.txt" if s else None),
|
|
|
|
inputs=[ignore_txt_in_glob], outputs=[fe])
|
2023-12-13 08:39:11 +08:00
|
|
|
|
2023-11-28 18:27:45 +08:00
|
|
|
dd.select(lambda s: gr.FileExplorer(root=s), inputs=[dd], outputs=[fe])
|
2023-12-13 08:39:11 +08:00
|
|
|
run.click(lambda s: ",".join(s) if isinstance(s, list) else s, inputs=[fe], outputs=[textbox])
|
2024-02-17 02:03:10 +08:00
|
|
|
fe.change(lambda num: num + 1, inputs=total_changes, outputs=total_changes)
|
2023-11-28 18:27:45 +08:00
|
|
|
|
2023-12-09 02:27:25 +08:00
|
|
|
with gr.Row():
|
|
|
|
a = gr.Textbox(elem_id="input-box")
|
|
|
|
a.change(lambda x: x, inputs=[a])
|
|
|
|
|
2023-11-28 18:27:45 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
demo.launch()
|