Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to hide the gr.File after triggering gr.delete() #12612

@locitran

Description

@locitran

Describe the bug

Hi gradio developers,

I am writing to consult you how to hide the gr.File after I trigger gr.delete().

Because I create a upload button, where I can input some file.
After file inputing, gr.File will be visible such that I can click delete to trigger gr.delete() event.

However, after I click delete, it does not go back to original interface, which I would like to have.
Do you have any suggestions on this? Thanks.

import gradio as gr
import os

def upload_file(file):
    """Upload file and return message
    file is a gradio.utils.NamedString object
    """
    button_udt = gr.update(visible=True)
    button_file = gr.update(value=None, visible=False)
    if file is None:
        return button_udt, button_file

    filepath = str(file)
    if os.path.exists(filepath):
        button_udt = gr.update(visible=False)
        button_file = gr.update(value=filepath, visible=True)
        
    return button_udt, button_file

def on_delete_file(_):
    button_udt = gr.update(visible=True)
    button_file = gr.update(value=None, visible=False)
    return button_udt, button_file

with gr.Blocks() as demo:
    gr.Markdown("## Upload/Delete Debug Test")
    upload_btn = gr.UploadButton(label="Upload file (.txt)", file_count="single", file_types=[".txt"],)
    uploaded_file = gr.File(label="Uploaded file", visible=False,)
    upload_btn.upload(fn=upload_file, inputs=[upload_btn], outputs=[upload_btn, uploaded_file],)
    uploaded_file.delete(fn=on_delete_file,inputs=[uploaded_file], outputs=[upload_btn, uploaded_file],)

demo.launch(share=True)

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr

Screenshot

Image

Logs

System Info

gradio 6.2

Severity

I can work around it

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions