freddyaboulton HF Staff commited on
Commit
0a6ad4c
·
verified ·
1 Parent(s): fd15ba0

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +7 -7
  2. run.ipynb +1 -0
  3. run.py +44 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Html Server Functions
3
- emoji: 👁
4
- colorFrom: green
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 6.7.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: html_server_functions
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 6.7.0
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: html_server_functions"]}, {"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": ["import os\n", "\n", "import gradio as gr\n", "\n", "\n", "def list_files(path):\n", " try:\n", " return os.listdir(path)\n", " except (FileNotFoundError, PermissionError) as e:\n", " return [f\"Error: {e}\"]\n", "\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\n", " \"# Server Functions Demo\\nClick 'Load Files' to list files in the directory.\"\n", " )\n", " filetree = gr.HTML(\n", " value=os.path.abspath(''),\n", " html_template=\"\"\"\n", " <div>\n", " <p>Directory: <strong>${value}</strong></p>\n", " <div class='tree'></div>\n", " <button class='load-btn'>Load Files</button>\n", " </div>\n", " \"\"\",\n", " js_on_load=\"\"\"\n", " const loadBtn = element.querySelector('.load-btn');\n", " const tree = element.querySelector('.tree');\n", " loadBtn.addEventListener('click', async () => {\n", " const files = await server.list_files(props.value);\n", " tree.innerHTML = '';\n", " files.forEach(file => {\n", " const fileEl = document.createElement('div');\n", " fileEl.textContent = file;\n", " tree.appendChild(fileEl);\n", " });\n", " });\n", " \"\"\",\n", " server_functions=[list_files],\n", " )\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+
5
+
6
+ def list_files(path):
7
+ try:
8
+ return os.listdir(path)
9
+ except (FileNotFoundError, PermissionError) as e:
10
+ return [f"Error: {e}"]
11
+
12
+
13
+ with gr.Blocks() as demo:
14
+ gr.Markdown(
15
+ "# Server Functions Demo\nClick 'Load Files' to list files in the directory."
16
+ )
17
+ filetree = gr.HTML(
18
+ value=os.path.dirname(__file__),
19
+ html_template="""
20
+ <div>
21
+ <p>Directory: <strong>${value}</strong></p>
22
+ <div class='tree'></div>
23
+ <button class='load-btn'>Load Files</button>
24
+ </div>
25
+ """,
26
+ js_on_load="""
27
+ const loadBtn = element.querySelector('.load-btn');
28
+ const tree = element.querySelector('.tree');
29
+ loadBtn.addEventListener('click', async () => {
30
+ const files = await server.list_files(props.value);
31
+ tree.innerHTML = '';
32
+ files.forEach(file => {
33
+ const fileEl = document.createElement('div');
34
+ fileEl.textContent = file;
35
+ tree.appendChild(fileEl);
36
+ });
37
+ });
38
+ """,
39
+ server_functions=[list_files],
40
+ )
41
+
42
+
43
+ if __name__ == "__main__":
44
+ demo.launch()