-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add delete event to File
component
#8417
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9875ae5
fix param name
pngwn 28c0727
format
pngwn d922296
add delete event to File component
pngwn e6973d1
fix
pngwn 99163a0
add changeset
gradio-pr-bot fa70ed0
fix
pngwn dfda730
fix
pngwn df6f925
fix
pngwn 7a1c575
add changeset
gradio-pr-bot 43ca7d9
format
pngwn 74dde5c
add changeset
gradio-pr-bot 2634fc7
file file layout
pngwn 222124f
Merge branch 'main' into 8354-file-delete
pngwn 6b1f384
add changeset
gradio-pr-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@gradio/file": minor | ||
"@gradio/tootils": minor | ||
"@gradio/upload": minor | ||
"gradio": minor | ||
--- | ||
|
||
feat:add delete event to `File` component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: file_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": ["import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", " \n", " with gr.Row():\n", " with gr.Column():\n", " file_component = gr.File(label=\"Upload Single File\", file_count=\"single\")\n", " with gr.Column():\n", " output_file_1 = gr.File(label=\"Upload Single File Output\", file_count=\"single\")\n", " num_load_btn_1 = gr.Number(label=\"# Load Upload Single File\", value=0)\n", " file_component.upload(lambda s,n: (s, n + 1), [file_component, num_load_btn_1], [output_file_1, num_load_btn_1])\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_multiple = gr.File(label=\"Upload Multiple Files\", file_count=\"multiple\")\n", " with gr.Column():\n", " output_file_2 = gr.File(label=\"Upload Multiple Files Output\", file_count=\"multiple\")\n", " num_load_btn_2 = gr.Number(label=\"# Load Upload Multiple Files\", value=0)\n", " file_component_multiple.upload(lambda s,n: (s, n + 1), [file_component_multiple, num_load_btn_2], [output_file_2, num_load_btn_2])\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_specific = gr.File(label=\"Upload Multiple Files Image/Video\", file_count=\"multiple\", file_types=[\"image\", \"video\"])\n", " with gr.Column():\n", " output_file_3 = gr.File(label=\"Upload Multiple Files Output Image/Video\", file_count=\"multiple\")\n", " num_load_btn_3 = gr.Number(label=\"# Load Upload Multiple Files Image/Video\", value=0)\n", " file_component_specific.upload(lambda s,n: (s, n + 1), [file_component_specific, num_load_btn_3], [output_file_3, num_load_btn_3])\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_pdf = gr.File(label=\"Upload PDF File\", file_types=[\"pdf\"])\n", " with gr.Column():\n", " output_file_4 = gr.File(label=\"Upload PDF File Output\")\n", " num_load_btn_4 = gr.Number(label=\"# Load Upload PDF File\", value=0)\n", " file_component_pdf.upload(lambda s,n: (s, n + 1), [file_component_pdf, num_load_btn_4], [output_file_4, num_load_btn_4])\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_invalid = gr.File(label=\"Upload File with Invalid file_types\", file_types=[\"invalid file_type\"])\n", " with gr.Column():\n", " output_file_5 = gr.File(label=\"Upload File with Invalid file_types Output\")\n", " num_load_btn_5 = gr.Number(label=\"# Load Upload File with Invalid file_types\", value=0)\n", " file_component_invalid.upload(lambda s,n: (s, n + 1), [file_component_invalid, num_load_btn_5], [output_file_5, num_load_btn_5])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} | ||
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: file_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": ["import gradio as gr\n", "\n", "\n", "def delete_file(n: int, file: gr.DeletedFileData):\n", " return [file.file.path, n + 1]\n", "\n", "\n", "with gr.Blocks() as demo:\n", "\n", " with gr.Row():\n", " with gr.Column():\n", " file_component = gr.File(label=\"Upload Single File\", file_count=\"single\")\n", " with gr.Column():\n", " output_file_1 = gr.File(\n", " label=\"Upload Single File Output\", file_count=\"single\"\n", " )\n", " num_load_btn_1 = gr.Number(label=\"# Load Upload Single File\", value=0)\n", " file_component.upload(\n", " lambda s, n: (s, n + 1),\n", " [file_component, num_load_btn_1],\n", " [output_file_1, num_load_btn_1],\n", " )\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_multiple = gr.File(\n", " label=\"Upload Multiple Files\", file_count=\"multiple\"\n", " )\n", " with gr.Column():\n", " output_file_2 = gr.File(\n", " label=\"Upload Multiple Files Output\", file_count=\"multiple\"\n", " )\n", " num_load_btn_2 = gr.Number(label=\"# Load Upload Multiple Files\", value=0)\n", " file_component_multiple.upload(\n", " lambda s, n: (s, n + 1),\n", " [file_component_multiple, num_load_btn_2],\n", " [output_file_2, num_load_btn_2],\n", " )\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_specific = gr.File(\n", " label=\"Upload Multiple Files Image/Video\",\n", " file_count=\"multiple\",\n", " file_types=[\"image\", \"video\"],\n", " )\n", " with gr.Column():\n", " output_file_3 = gr.File(\n", " label=\"Upload Multiple Files Output Image/Video\", file_count=\"multiple\"\n", " )\n", " num_load_btn_3 = gr.Number(\n", " label=\"# Load Upload Multiple Files Image/Video\", value=0\n", " )\n", " file_component_specific.upload(\n", " lambda s, n: (s, n + 1),\n", " [file_component_specific, num_load_btn_3],\n", " [output_file_3, num_load_btn_3],\n", " )\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_pdf = gr.File(label=\"Upload PDF File\", file_types=[\"pdf\"])\n", " with gr.Column():\n", " output_file_4 = gr.File(label=\"Upload PDF File Output\")\n", " num_load_btn_4 = gr.Number(label=\"# Load Upload PDF File\", value=0)\n", " file_component_pdf.upload(\n", " lambda s, n: (s, n + 1),\n", " [file_component_pdf, num_load_btn_4],\n", " [output_file_4, num_load_btn_4],\n", " )\n", " with gr.Row():\n", " with gr.Column():\n", " file_component_invalid = gr.File(\n", " label=\"Upload File with Invalid file_types\",\n", " file_types=[\"invalid file_type\"],\n", " )\n", " with gr.Column():\n", " output_file_5 = gr.File(label=\"Upload File with Invalid file_types Output\")\n", " num_load_btn_5 = gr.Number(\n", " label=\"# Load Upload File with Invalid file_types\", value=0\n", " )\n", " file_component_invalid.upload(\n", " lambda s, n: (s, n + 1),\n", " [file_component_invalid, num_load_btn_5],\n", " [output_file_5, num_load_btn_5],\n", " )\n", " with gr.Row():\n", " with gr.Column():\n", " del_file_input = gr.File(label=\"Delete File\", file_count=\"multiple\")\n", " with gr.Column():\n", " del_file_data = gr.Textbox(label=\"Delete file data\")\n", " num_load_btn_6 = gr.Number(label=\"# Deleted File\", value=0)\n", " del_file_input.delete(\n", " delete_file,\n", " [num_load_btn_6],\n", " [del_file_data, num_load_btn_6],\n", " )\n", " # f = gr.File(label=\"Upload many File\", file_count=\"multiple\")\n", " # # f.delete(delete_file)\n", " # f.delete(delete_file, inputs=None, outputs=None)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,100 @@ | ||
import gradio as gr | ||
|
||
|
||
def delete_file(n: int, file: gr.DeletedFileData): | ||
return [file.file.path, n + 1] | ||
|
||
|
||
with gr.Blocks() as demo: | ||
|
||
with gr.Row(): | ||
with gr.Column(): | ||
file_component = gr.File(label="Upload Single File", file_count="single") | ||
with gr.Column(): | ||
output_file_1 = gr.File(label="Upload Single File Output", file_count="single") | ||
output_file_1 = gr.File( | ||
label="Upload Single File Output", file_count="single" | ||
) | ||
num_load_btn_1 = gr.Number(label="# Load Upload Single File", value=0) | ||
file_component.upload(lambda s,n: (s, n + 1), [file_component, num_load_btn_1], [output_file_1, num_load_btn_1]) | ||
file_component.upload( | ||
lambda s, n: (s, n + 1), | ||
[file_component, num_load_btn_1], | ||
[output_file_1, num_load_btn_1], | ||
) | ||
with gr.Row(): | ||
with gr.Column(): | ||
file_component_multiple = gr.File(label="Upload Multiple Files", file_count="multiple") | ||
file_component_multiple = gr.File( | ||
label="Upload Multiple Files", file_count="multiple" | ||
) | ||
with gr.Column(): | ||
output_file_2 = gr.File(label="Upload Multiple Files Output", file_count="multiple") | ||
output_file_2 = gr.File( | ||
label="Upload Multiple Files Output", file_count="multiple" | ||
) | ||
num_load_btn_2 = gr.Number(label="# Load Upload Multiple Files", value=0) | ||
file_component_multiple.upload(lambda s,n: (s, n + 1), [file_component_multiple, num_load_btn_2], [output_file_2, num_load_btn_2]) | ||
file_component_multiple.upload( | ||
lambda s, n: (s, n + 1), | ||
[file_component_multiple, num_load_btn_2], | ||
[output_file_2, num_load_btn_2], | ||
) | ||
with gr.Row(): | ||
with gr.Column(): | ||
file_component_specific = gr.File(label="Upload Multiple Files Image/Video", file_count="multiple", file_types=["image", "video"]) | ||
file_component_specific = gr.File( | ||
label="Upload Multiple Files Image/Video", | ||
file_count="multiple", | ||
file_types=["image", "video"], | ||
) | ||
with gr.Column(): | ||
output_file_3 = gr.File(label="Upload Multiple Files Output Image/Video", file_count="multiple") | ||
num_load_btn_3 = gr.Number(label="# Load Upload Multiple Files Image/Video", value=0) | ||
file_component_specific.upload(lambda s,n: (s, n + 1), [file_component_specific, num_load_btn_3], [output_file_3, num_load_btn_3]) | ||
output_file_3 = gr.File( | ||
label="Upload Multiple Files Output Image/Video", file_count="multiple" | ||
) | ||
num_load_btn_3 = gr.Number( | ||
label="# Load Upload Multiple Files Image/Video", value=0 | ||
) | ||
file_component_specific.upload( | ||
lambda s, n: (s, n + 1), | ||
[file_component_specific, num_load_btn_3], | ||
[output_file_3, num_load_btn_3], | ||
) | ||
with gr.Row(): | ||
with gr.Column(): | ||
file_component_pdf = gr.File(label="Upload PDF File", file_types=["pdf"]) | ||
with gr.Column(): | ||
output_file_4 = gr.File(label="Upload PDF File Output") | ||
num_load_btn_4 = gr.Number(label="# Load Upload PDF File", value=0) | ||
file_component_pdf.upload(lambda s,n: (s, n + 1), [file_component_pdf, num_load_btn_4], [output_file_4, num_load_btn_4]) | ||
file_component_pdf.upload( | ||
lambda s, n: (s, n + 1), | ||
[file_component_pdf, num_load_btn_4], | ||
[output_file_4, num_load_btn_4], | ||
) | ||
with gr.Row(): | ||
with gr.Column(): | ||
file_component_invalid = gr.File(label="Upload File with Invalid file_types", file_types=["invalid file_type"]) | ||
file_component_invalid = gr.File( | ||
label="Upload File with Invalid file_types", | ||
file_types=["invalid file_type"], | ||
) | ||
with gr.Column(): | ||
output_file_5 = gr.File(label="Upload File with Invalid file_types Output") | ||
num_load_btn_5 = gr.Number(label="# Load Upload File with Invalid file_types", value=0) | ||
file_component_invalid.upload(lambda s,n: (s, n + 1), [file_component_invalid, num_load_btn_5], [output_file_5, num_load_btn_5]) | ||
num_load_btn_5 = gr.Number( | ||
label="# Load Upload File with Invalid file_types", value=0 | ||
) | ||
file_component_invalid.upload( | ||
lambda s, n: (s, n + 1), | ||
[file_component_invalid, num_load_btn_5], | ||
[output_file_5, num_load_btn_5], | ||
) | ||
with gr.Row(): | ||
with gr.Column(): | ||
del_file_input = gr.File(label="Delete File", file_count="multiple") | ||
with gr.Column(): | ||
del_file_data = gr.Textbox(label="Delete file data") | ||
num_load_btn_6 = gr.Number(label="# Deleted File", value=0) | ||
del_file_input.delete( | ||
delete_file, | ||
[num_load_btn_6], | ||
[del_file_data, num_load_btn_6], | ||
) | ||
# f = gr.File(label="Upload many File", file_count="multiple") | ||
# # f.delete(delete_file) | ||
# f.delete(delete_file, inputs=None, outputs=None) | ||
|
||
if __name__ == "__main__": | ||
demo.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
from gradio_client.documentation import document | ||
from jinja2 import Template | ||
|
||
from gradio.data_classes import FileData, FileDataDict | ||
|
||
if TYPE_CHECKING: | ||
from gradio.blocks import Block, Component | ||
|
||
|
@@ -149,6 +151,15 @@ def __init__(self, target: Block | None, data: Any): | |
""" | ||
|
||
|
||
class DeletedFileData(EventData): | ||
def __init__(self, target: Block | None, data: FileDataDict): | ||
super().__init__(target, data) | ||
self.file: FileData = FileData(**data) | ||
""" | ||
The file that was deleted. | ||
""" | ||
Comment on lines
+154
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New EventData type, carries the deleted file as an instance of FileData |
||
|
||
|
||
@dataclasses.dataclass | ||
class EventListenerMethod: | ||
block: Block | None | ||
|
@@ -585,6 +596,10 @@ class Events: | |
"apply", | ||
doc="This listener is triggered when the user applies changes to the {{ component }} through an integrated UI action.", | ||
) | ||
delete = EventListener( | ||
"delete", | ||
doc="This listener is triggered when the user deletes and item from the {{ component }}. Uses event data gradio.DeletedFileData to carry `value` referring to the file that was deleted as an instance of FileData. See EventData documentation on how to use this event data", | ||
) | ||
|
||
|
||
class LikeData(EventData): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this so I could type my Event data correctly