Skip to content
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 .download() event to gr.File #9887

Merged
merged 8 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/giant-baboons-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/file": minor
"gradio": minor
---

feat:Add `.download()` event to `gr.File`
1 change: 1 addition & 0 deletions gradio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from gradio.data_classes import FileData
from gradio.events import (
DeletedFileData,
DownloadData,
EventData,
KeyUpData,
LikeData,
Expand Down
9 changes: 8 additions & 1 deletion gradio/components/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ class File(Component):
Demo: zip_files, zip_to_json
"""

EVENTS = [Events.change, Events.select, Events.clear, Events.upload, Events.delete]
EVENTS = [
Events.change,
Events.select,
Events.clear,
Events.upload,
Events.delete,
Events.download,
]

def __init__(
self,
Expand Down
29 changes: 29 additions & 0 deletions gradio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,31 @@ def __init__(self, target: Block | None, data: Any):
"""


@document()
class DownloadData(EventData):
"""
The gr.DownloadData class is a subclass of gr.EventData that specifically carries information about the `.download()` event. When gr.DownloadData
is added as a type hint to an argument of an event listener method, a gr.DownloadData object will automatically be passed as the value of that argument.
The attributes of this object contains information about the event that triggered the listener.
Example:
import gradio as gr
def on_download(download_data: gr.DownloadData):
return f"Downloaded file: {download_data.file.path}"
with gr.Blocks() as demo:
files = gr.File()
textbox = gr.Textbox()
files.download(on_download, None, textbox)
demo.launch()
"""

def __init__(self, target: Block | None, data: FileDataDict):
super().__init__(target, data)
self.file: FileData = FileData(**data)
"""
The file that was downloaded, as a FileData object.
"""


@dataclasses.dataclass
class EventListenerMethod:
block: Block | None
Expand Down Expand Up @@ -931,3 +956,7 @@ class Events:
"collapse",
doc="This listener is triggered when the {{ component }} is collapsed.",
)
download = EventListener(
"download",
doc="This listener is triggered when the user downloads a file from the {{ component }}. Uses event data gradio.DownloadData to carry information about the downloaded file as a FileData object. See EventData documentation on how to use this event data",
)
2 changes: 2 additions & 0 deletions js/file/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
select: SelectData;
clear_status: LoadingStatus;
delete: FileData;
download: FileData;
}>;
export let file_count: "single" | "multiple" | "directory";
export let file_types: string[] = ["file"];
Expand Down Expand Up @@ -82,6 +83,7 @@
{#if !interactive}
<File
on:select={({ detail }) => gradio.dispatch("select", detail)}
on:download={({ detail }) => gradio.dispatch("download", detail)}
selectable={_selectable}
{value}
{label}
Expand Down
2 changes: 1 addition & 1 deletion js/file/shared/File.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/>

{#if value && (Array.isArray(value) ? value.length > 0 : true)}
<FilePreview {i18n} {selectable} on:select {value} {height} />
<FilePreview {i18n} {selectable} on:select on:download {value} {height} />
{:else}
<Empty unpadded_box={true} size="large"><File /></Empty>
{/if}
6 changes: 6 additions & 0 deletions js/file/shared/FilePreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
select: SelectData;
change: FileData[] | FileData;
delete: FileData;
download: FileData;
}>();
export let value: FileData | FileData[];
export let selectable = false;
Expand Down Expand Up @@ -56,6 +57,10 @@
dispatch("change", normalized_files);
}

function handle_download(file: FileData): void {
dispatch("download", file);
}

const is_browser = typeof window !== "undefined";
</script>

Expand All @@ -82,6 +87,7 @@
{#if file.url}
<DownloadLink
href={file.url}
on:click={() => handle_download(file)}
download={is_browser && window.__is_colab__
? null
: file.orig_name}
Expand Down
Loading