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

Adds a gr.DownloadButton component #7518

Merged
merged 31 commits into from Feb 28, 2024
Merged

Adds a gr.DownloadButton component #7518

merged 31 commits into from Feb 28, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Feb 23, 2024

After #7528, I wanted to go through the process of creating a new component with the new approach of working with the files, i.e. setting the URLs in the backend using serve_static_file() to mimic the work that a custom component developer would have to do.

I decided to add the long-requested gr.DownloadButton component (fixes: #5426)

@abidlabs abidlabs marked this pull request as draft February 23, 2024 02:54
@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Feb 23, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detecting...

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/3a94fad9a48b48e38312c9aba9b46018079d392e/gradio-4.19.2-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@3a94fad9a48b48e38312c9aba9b46018079d392e#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Feb 23, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/app minor
@gradio/downloadbutton minor
@gradio/uploadbutton minor
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Adds a gr.DownloadButton component

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs changed the title Refactors get_fetchable_url_or_file() and adds a gr.DownloadButton component Adds a gr.DownloadButton component Feb 23, 2024
@abidlabs
Copy link
Member Author

Will probably need to be rebased once #7528 is in

@abidlabs abidlabs marked this pull request as ready for review February 27, 2024 16:58
@dawoodkhan82 dawoodkhan82 self-requested a review February 27, 2024 20:09
render: bool = True,
):
"""
Parameters:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing param for icon

Copy link
Member Author

@abidlabs abidlabs Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added here as well as in gr.UploadButton. Fixed the param description in gr.Button

@dawoodkhan82
Copy link
Collaborator

from pathlib import Path
import gradio as gr

def upload_file(filepath):
    name = Path(filepath).name
    return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True, icon="gradio-logo.png")]

def download_file():
    return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]

with gr.Blocks() as demo:
    gr.Markdown("First upload a file and and then you'll be able download it (but only once!)")
    with gr.Row():
        u = gr.UploadButton("Upload a file", file_count="single")
        d = gr.DownloadButton("Download a file", visible=False, icon="gradio-logo.png")

    u.upload(upload_file, u, [u, d])
    d.click(download_file, None, [u, d])


if __name__ == "__main__":
    demo.launch()

Tested with this code. And seems like updating the icon for the DownloadButton doesn't work. Gives error: AttributeError: 'DownloadButton' object has no attribute 'proxy_url'.

@abidlabs
Copy link
Member Author

Sooo I'm looking into this issue @dawoodkhan82 but it actually looks like a separate bug that resulted from #7528

You'll see the same error if you replace gr.DownloadButton with gr.UploadButton, e.g. in this short example:

import gradio as gr

with gr.Blocks() as demo:
    u = gr.UploadButton()
    d = gr.DownloadButton()
    
    demo.load(lambda : gr.UploadButton(icon="cheetah.jpg"), None, u)
#     demo.load(lambda : gr.DownloadButton(icon="cheetah.jpg"), None, d)
    
demo.launch()

I'm working on it, but it might be tricky and I think can potentially be a separate PR for better visibility. How does the rest of this PR look?

@abidlabs
Copy link
Member Author

Nvm the fix was easier than I thought -- I've merged it into this PR. Your example works for me now @dawoodkhan82

@dawoodkhan82 dawoodkhan82 self-requested a review February 28, 2024 19:46
Copy link
Collaborator

@dawoodkhan82 dawoodkhan82 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tested updating the icon as well. Thanks for including that fix @abidlabs

@abidlabs
Copy link
Member Author

Thanks @dawoodkhan82 for testing thoroughly!

@abidlabs abidlabs merged commit bd2c695 into main Feb 28, 2024
7 of 8 checks passed
@abidlabs abidlabs deleted the download-button branch February 28, 2024 20:07
@pngwn pngwn mentioned this pull request Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

a gr.DownloadButton that lets you download a file with a button click
3 participants