gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.36k stars 2.52k forks source link

Unable to save a file #9022

Closed piovis2023 closed 2 months ago

piovis2023 commented 2 months ago

Describe the bug

I just want to click save a button on the GUI. It should open windows explorer to the designated folder, where I can type and save the file. I've been working with ChatGPT, Claude 3.5 Sonnet and after 5 hours, still can't do this simple thing.

I do not want to a textbox in the GUI to create the filename, since it will create confusion. I want to create the file name via windows explorer.

My setup: Windows 11 OS Python 3.11 Conda env Latest Gradio version as of 06 Aug 2024.

Thanks you

Have you searched existing issues? πŸ”Ž

Reproduction

Various attempts (only showing this requirement) [Try 1] import gradio as gr import os from pathlib import Path import subprocess import time

Configuration

category_config_folder = Path(os.path.join(os.getcwd(), "categoryconfig"))

Ensure the directory exists

category_config_folder.mkdir(parents=True, exist_ok=True)

Function to list all files in the category config folder

def list_files(): return [f.name for f in category_config_folder.glob("*.json")]

Function to open the category config folder

def open_folder(): path_to_open = os.path.realpath(category_config_folder) subprocess.Popen(f'explorer {path_to_open}')

Function to check for new files and update the dropdown

def refresh_dropdown(): time.sleep(5) # Wait for the user to create the file return list_files()

Interface elements

def create_interface(): with gr.Blocks() as demo: with gr.Row(): file_dropdown = gr.Dropdown(choices=list_files(), label="Category Files") open_folder_button = gr.Button("πŸ“") save_file_button = gr.Button("Refresh πŸ”„")

    # Button actions
    open_folder_button.click(open_folder)
    save_file_button.click(fn=refresh_dropdown, inputs=None, outputs=file_dropdown)

return demo

Create and launch the interface

interface = create_interface() interface.launch()

[Try 2] import gradio as gr import os from pathlib import Path import subprocess import time

Configuration

category_config_folder = Path(os.path.join(os.getcwd(), "categoryconfig"))

Ensure the directory exists

category_config_folder.mkdir(parents=True, exist_ok=True)

Function to list all files in the category config folder

def list_files(): return [f.name for f in category_config_folder.glob("*.json")]

Function to open the category config folder

def open_folder(): path_to_open = os.path.realpath(category_config_folder) subprocess.Popen(f'explorer {path_to_open}')

Function to save a new file

def save_file(): path_to_open = os.path.realpath(category_config_folder) subprocess.Popen(f'explorer /select,{path_to_open}\newfile.json') time.sleep(5) # Wait for the user to save the file return gr.Dropdown.update(choices=list_files())

Interface elements

def create_interface(): with gr.Blocks() as demo: with gr.Row(): file_dropdown = gr.Dropdown(choices=list_files(), label="Category Files") open_folder_button = gr.Button("πŸ“") save_file_button = gr.Button("πŸ’Ύ")

    # Button actions
    open_folder_button.click(open_folder)
    save_file_button.click(fn=save_file, outputs=file_dropdown)

return demo

Create and launch the interface

interface = create_interface() interface.launch()

[Try 3] import gradio as gr import os from pathlib import Path import subprocess

Configuration

category_config_folder = Path(os.path.join(os.getcwd(), "categoryconfig"))

Ensure the directory exists

category_config_folder.mkdir(parents=True, exist_ok=True)

Function to list all files in the category config folder

def list_files(): return [f.name for f in category_config_folder.glob("*.json")]

Function to open the category config folder

def open_folder(): path_to_open = os.path.realpath(category_config_folder) subprocess.Popen(f'explorer {path_to_open}')

Function to save a new file and update the dropdown list

def save_file(): path_to_open = os.path.realpath(category_config_folder) subprocess.Popen(f'explorer /select,{path_to_open}\newfile.json') return gr.Dropdown.update(choices=list_files())

Interface elements

def create_interface(): with gr.Blocks() as demo: with gr.Row(): file_dropdown = gr.Dropdown(choices=list_files(), label="Category Files") open_folder_button = gr.Button("πŸ“") save_file_button = gr.Button("πŸ’Ύ")

    # Button actions
    open_folder_button.click(open_folder)
    save_file_button.click(fn=save_file, inputs=None, outputs=file_dropdown)

return demo

Create and launch the interface

interface = create_interface() interface.launch()

Screenshot

No response

Logs

No response

System Info

Operating System: Windows
gradio version: 4.40.0
gradio_client version: 1.2.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
anyio: 4.4.0
fastapi: 0.111.1
ffmpy: 0.4.0
gradio-client==1.2.0 is not installed.
httpx: 0.27.0
huggingface-hub: 0.24.5
importlib-resources: 6.4.0
jinja2: 3.1.4
markupsafe: 2.1.5
matplotlib: 3.9.1
numpy: 1.26.4
orjson: 3.10.6
packaging: 24.1
pandas: 2.2.2
pillow: 10.4.0
pydantic: 2.8.2
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.5.5
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.4
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.6.1
httpx: 0.27.0
huggingface-hub: 0.24.5
packaging: 24.1
typing-extensions: 4.12.2
websockets: 12.0

Severity

Blocking usage of gradio

abidlabs commented 2 months ago

Hi @piovis2023 apps running in a browser typically do not have access to a user's file system, so what you're describing cannot be achieved in Gradio directly. You have two options:

(1) Simply right click on the download file link and click "Save as...", which will open up the filesystem window

(2) Use gr.FileExplorer to expose the developer's file system and let users select a destination. Note that this is going to expose the developer's file system, not the user's file system, but this is an option if you are developing a Gradio app only for local useage