Spaces:
Running
Running
File size: 1,270 Bytes
0a6ad4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import os
import gradio as gr
def list_files(path):
try:
return os.listdir(path)
except (FileNotFoundError, PermissionError) as e:
return [f"Error: {e}"]
with gr.Blocks() as demo:
gr.Markdown(
"# Server Functions Demo\nClick 'Load Files' to list files in the directory."
)
filetree = gr.HTML(
value=os.path.dirname(__file__),
html_template="""
<div>
<p>Directory: <strong>${value}</strong></p>
<div class='tree'></div>
<button class='load-btn'>Load Files</button>
</div>
""",
js_on_load="""
const loadBtn = element.querySelector('.load-btn');
const tree = element.querySelector('.tree');
loadBtn.addEventListener('click', async () => {
const files = await server.list_files(props.value);
tree.innerHTML = '';
files.forEach(file => {
const fileEl = document.createElement('div');
fileEl.textContent = file;
tree.appendChild(fileEl);
});
});
""",
server_functions=[list_files],
)
if __name__ == "__main__":
demo.launch()
|