Commit 2: Add 50 file(s)
Browse files
demos/file_explorer_component_events/run.ipynb
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: file_explorer_component_events"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('dir1')\n", "!wget -q -O dir1/bar.txt https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir1/bar.txt\n", "!wget -q -O dir1/foo.txt https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir1/foo.txt\n", "os.mkdir('dir2')\n", "!wget -q -O dir2/baz.png https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir2/baz.png\n", "!wget -q -O dir2/foo.png https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir2/foo.png\n", "os.mkdir('dir3')\n", "!wget -q -O dir3/dir3_bar.log https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir3/dir3_bar.log\n", "!wget -q -O dir3/dir4 https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir3/dir4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from pathlib import Path\n", "\n", "base_root = Path(__file__).parent.resolve()\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " dd = gr.Dropdown(label=\"Select File Explorer Root\",\n", " value=str(base_root),\n", " choices=[str(base_root),\n", " str(base_root / \"dir1\"),\n", " str(base_root / \"dir2\"),\n", " str(base_root / \"dir3\")])\n", " with gr.Group():\n", " txt_only_glob = gr.Checkbox(label=\"Show only text files\", value=False)\n", " ignore_txt_in_glob = gr.Checkbox(label=\"Ignore text files in glob\", value=False)\n", "\n", "
|
|
|
|
| 1 |
+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: file_explorer_component_events"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('dir1')\n", "!wget -q -O dir1/bar.txt https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir1/bar.txt\n", "!wget -q -O dir1/foo.txt https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir1/foo.txt\n", "os.mkdir('dir2')\n", "!wget -q -O dir2/baz.png https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir2/baz.png\n", "!wget -q -O dir2/foo.png https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir2/foo.png\n", "os.mkdir('dir3')\n", "!wget -q -O dir3/dir3_bar.log https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir3/dir3_bar.log\n", "!wget -q -O dir3/dir4 https://github.com/gradio-app/gradio/raw/main/demo/file_explorer_component_events/dir3/dir4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from pathlib import Path\n", "\n", "base_root = Path(__file__).parent.resolve()\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " dd = gr.Dropdown(label=\"Select File Explorer Root\",\n", " value=str(base_root),\n", " choices=[str(base_root),\n", " str(base_root / \"dir1\"),\n", " str(base_root / \"dir2\"),\n", " str(base_root / \"dir3\")])\n", " with gr.Group():\n", " txt_only_glob = gr.Checkbox(label=\"Show only text files\", value=False)\n", " ignore_txt_in_glob = gr.Checkbox(label=\"Ignore text files in glob\", value=False)\n", "\n", " fe = gr.FileExplorer(root_dir=str(base_root),\n", " glob=\"**/*\", interactive=True)\n", " textbox = gr.Textbox(label=\"Selected Directory\")\n", " selected_file = gr.Textbox(label=\"Last Selected (via .select())\")\n", " run = gr.Button(\"Run\")\n", " total_changes = gr.Number(0, elem_id=\"total-changes\", label=\"# of Change Events\")\n", " total_inputs = gr.Number(0, elem_id=\"total-inputs\", label=\"# of Input Events\")\n", " total_selects = gr.Number(0, elem_id=\"total-selects\", label=\"# of Select Events\")\n", "\n", " txt_only_glob.select(lambda s: gr.FileExplorer(glob=\"*.txt\" if s else \"*\") ,\n", " inputs=[txt_only_glob], outputs=[fe])\n", " ignore_txt_in_glob.select(lambda s: gr.FileExplorer(ignore_glob=\"*.txt\" if s else None),\n", " inputs=[ignore_txt_in_glob], outputs=[fe])\n", "\n", " dd.select(lambda s: gr.FileExplorer(root_dir=s), inputs=[dd], outputs=[fe])\n", " run.click(lambda s: \",\".join(s) if isinstance(s, list) else s, inputs=[fe], outputs=[textbox])\n", " fe.change(lambda num: num + 1, inputs=total_changes, outputs=total_changes)\n", " fe.input(lambda num: num + 1, inputs=total_inputs, outputs=total_inputs)\n", " def on_select(evt: gr.SelectData, num: int):\n", " return f\"Index: {evt.index}, Value: {evt.value}\", num + 1\n", " fe.select(on_select, inputs=total_selects, outputs=[selected_file, total_selects])\n", "\n", " with gr.Row():\n", " a = gr.Textbox(elem_id=\"input-box\")\n", " a.change(lambda x: x, inputs=[a])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
demos/file_explorer_component_events/run.py
CHANGED
|
@@ -15,10 +15,9 @@ with gr.Blocks() as demo:
|
|
| 15 |
txt_only_glob = gr.Checkbox(label="Show only text files", value=False)
|
| 16 |
ignore_txt_in_glob = gr.Checkbox(label="Ignore text files in glob", value=False)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
fe = gr.FileExplorer(root_dir=str(base_root), value=None,
|
| 20 |
glob="**/*", interactive=True)
|
| 21 |
-
|
| 22 |
selected_file = gr.Textbox(label="Last Selected (via .select())")
|
| 23 |
run = gr.Button("Run")
|
| 24 |
total_changes = gr.Number(0, elem_id="total-changes", label="# of Change Events")
|
|
@@ -31,6 +30,7 @@ with gr.Blocks() as demo:
|
|
| 31 |
inputs=[ignore_txt_in_glob], outputs=[fe])
|
| 32 |
|
| 33 |
dd.select(lambda s: gr.FileExplorer(root_dir=s), inputs=[dd], outputs=[fe])
|
|
|
|
| 34 |
fe.change(lambda num: num + 1, inputs=total_changes, outputs=total_changes)
|
| 35 |
fe.input(lambda num: num + 1, inputs=total_inputs, outputs=total_inputs)
|
| 36 |
def on_select(evt: gr.SelectData, num: int):
|
|
|
|
| 15 |
txt_only_glob = gr.Checkbox(label="Show only text files", value=False)
|
| 16 |
ignore_txt_in_glob = gr.Checkbox(label="Ignore text files in glob", value=False)
|
| 17 |
|
| 18 |
+
fe = gr.FileExplorer(root_dir=str(base_root),
|
|
|
|
| 19 |
glob="**/*", interactive=True)
|
| 20 |
+
textbox = gr.Textbox(label="Selected Directory")
|
| 21 |
selected_file = gr.Textbox(label="Last Selected (via .select())")
|
| 22 |
run = gr.Button("Run")
|
| 23 |
total_changes = gr.Number(0, elem_id="total-changes", label="# of Change Events")
|
|
|
|
| 30 |
inputs=[ignore_txt_in_glob], outputs=[fe])
|
| 31 |
|
| 32 |
dd.select(lambda s: gr.FileExplorer(root_dir=s), inputs=[dd], outputs=[fe])
|
| 33 |
+
run.click(lambda s: ",".join(s) if isinstance(s, list) else s, inputs=[fe], outputs=[textbox])
|
| 34 |
fe.change(lambda num: num + 1, inputs=total_changes, outputs=total_changes)
|
| 35 |
fe.input(lambda num: num + 1, inputs=total_inputs, outputs=total_inputs)
|
| 36 |
def on_select(evt: gr.SelectData, num: int):
|