Skip to content

Commit

Permalink
Add show_download_button to gr.Image() (#4959)
Browse files Browse the repository at this point in the history
* add show_download_button param

* add static image story

* chlog

* fix test

* typo

* add default value

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* fix test

* fix another test

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
  • Loading branch information
hannahblair and abidlabs committed Jul 19, 2023
1 parent fdc9ca2 commit a0efc11
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,7 @@ No changes to highlight.

## New Features:

No changes to highlight.
- Add `show_download_button` param to allow the download button in static Image components to be hidden by [@hannahblair](https://github.com/hannahblair) in [PR 4959](https://github.com/gradio-app/gradio/pull/4959)

## Bug Fixes:

Expand Down
6 changes: 6 additions & 0 deletions gradio/components/image.py
Expand Up @@ -70,6 +70,7 @@ def __init__(
label: str | None = None,
every: float | None = None,
show_label: bool | None = None,
show_download_button: bool = True,
container: bool = True,
scale: int | None = None,
min_width: int = 160,
Expand Down Expand Up @@ -97,6 +98,7 @@ def __init__(
label: component name in interface.
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
show_label: if True, will display label.
show_download_button: If True, will display button to download image.
container: If True, will place the component in a container - providing some extra padding around the border.
scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
Expand Down Expand Up @@ -133,6 +135,7 @@ def __init__(
self.tool = tool
self.invert_colors = invert_colors
self.streaming = streaming
self.show_download_button = show_download_button
if streaming and source != "webcam":
raise ValueError("Image streaming only available if source is 'webcam'.")
self.select: EventListenerMethod
Expand Down Expand Up @@ -177,6 +180,7 @@ def get_config(self):
"brush_radius": self.brush_radius,
"selectable": self.selectable,
"show_share_button": self.show_share_button,
"show_download_button": self.show_download_button,
**IOComponent.get_config(self),
}

Expand All @@ -187,6 +191,7 @@ def update(
width: int | None = None,
label: str | None = None,
show_label: bool | None = None,
show_download_button: bool | None = None,
container: bool | None = None,
scale: int | None = None,
min_width: int | None = None,
Expand All @@ -200,6 +205,7 @@ def update(
"width": width,
"label": label,
"show_label": show_label,
"show_download_button": show_download_button,
"container": container,
"scale": scale,
"min_width": min_width,
Expand Down
4 changes: 4 additions & 0 deletions gradio/test_data/blocks_configs.py
Expand Up @@ -64,6 +64,7 @@
"min_width": 160,
"name": "image",
"show_share_button": False,
"show_download_button": True,
"visible": True,
},
"serializer": "ImgSerializable",
Expand Down Expand Up @@ -136,6 +137,7 @@
"min_width": 160,
"name": "image",
"show_share_button": False,
"show_download_button": True,
"visible": True,
},
"serializer": "ImgSerializable",
Expand Down Expand Up @@ -382,6 +384,7 @@
"min_width": 160,
"name": "image",
"show_share_button": False,
"show_download_button": True,
"visible": True,
},
"serializer": "ImgSerializable",
Expand Down Expand Up @@ -454,6 +457,7 @@
"min_width": 160,
"name": "image",
"show_share_button": False,
"show_download_button": True,
"visible": True,
},
"serializer": "ImgSerializable",
Expand Down
14 changes: 8 additions & 6 deletions js/app/src/components/Image/Image.svelte
Expand Up @@ -9,28 +9,29 @@
import type { LoadingStatus } from "../StatusTracker/types";
import UploadText from "../UploadText.svelte";
export let elem_id: string = "";
export let elem_classes: Array<string> = [];
export let visible: boolean = true;
export let elem_id = "";
export let elem_classes: string[] = [];
export let visible = true;
export let value: null | string = null;
export let source: "canvas" | "webcam" | "upload" = "upload";
export let tool: "editor" | "select" | "sketch" | "color-sketch" = "editor";
export let label: string;
export let show_label: boolean;
export let show_download_button: boolean;
export let streaming: boolean;
export let pending: boolean;
export let height: number | undefined;
export let width: number | undefined;
export let mirror_webcam: boolean;
export let shape: [number, number];
export let brush_radius: number;
export let selectable: boolean = false;
export let container: boolean = true;
export let selectable = false;
export let container = true;
export let scale: number | null = null;
export let min_width: number | undefined = undefined;
export let loading_status: LoadingStatus;
export let mode: "static" | "dynamic";
export let show_share_button: boolean = false;
export let show_share_button = false;
const dispatch = createEventDispatcher<{
change: undefined;
Expand Down Expand Up @@ -70,6 +71,7 @@
{value}
{label}
{show_label}
{show_download_button}
{selectable}
{show_share_button}
/>
Expand Down
50 changes: 50 additions & 0 deletions js/image/src/Image.stories.svelte
@@ -0,0 +1,50 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import StaticImage from "./StaticImage.svelte";
</script>

<Meta
title="Components/Image"
component={Image}
argTypes={{
value: {
control: "text",
description: "The image URL or file to display",
name: "value",
value: "https://i.ibb.co/6BgKdSj/groot.jpg"
},
show_download_button: {
options: [true, false],
description: "If false, the download button will not be visible",
control: { type: "boolean" },
defaultValue: true
},
}}
/>

<Template let:args>
<div class="image-container" style="width: 300px; position: relative;border-radius: var(--radius-lg);overflow: hidden;">
<StaticImage {...args} />
</div>
</Template>

<Story
name="Static Image with label and download button"
args={{
value: "https://i.ibb.co/6BgKdSj/groot.jpg",
show_label: true,
show_download_button: true
}}
/>

<Story
name="Static Image with no label or download button"
args={{
value: "https://i.ibb.co/6BgKdSj/groot.jpg",
show_label: false,
show_download_button: false
}}
/>



11 changes: 7 additions & 4 deletions js/image/src/StaticImage.svelte
Expand Up @@ -3,16 +3,17 @@
import type { SelectData } from "@gradio/utils";
import { uploadToHuggingFace } from "@gradio/utils";
import { BlockLabel, Empty, IconButton, ShareButton } from "@gradio/atoms";
import { Download, Community } from "@gradio/icons";
import { Download } from "@gradio/icons";
import { get_coordinates_of_clicked_image } from "./utils";
import { Image } from "@gradio/icons";
export let value: null | string;
export let label: string | undefined = undefined;
export let show_label: boolean;
export let selectable: boolean = false;
export let show_share_button: boolean = false;
export let show_download_button = true;
export let selectable = false;
export let show_share_button = false;
const dispatch = createEventDispatcher<{
change: string;
Expand All @@ -21,7 +22,7 @@
$: value && dispatch("change", value);
const handle_click = (evt: MouseEvent) => {
const handle_click = (evt: MouseEvent): void => {
let coordinates = get_coordinates_of_clicked_image(evt);
if (coordinates) {
dispatch("select", { index: coordinates, value: null });
Expand All @@ -34,13 +35,15 @@
<Empty unpadded_box={true} size="large"><Image /></Empty>
{:else}
<div class="icon-buttons">
{#if show_download_button}
<a
href={value}
target={window.__is_colab__ ? "_blank" : null}
download={"image"}
>
<IconButton Icon={Download} label="Download" />
</a>
{/if}
{#if show_share_button}
<ShareButton
on:share
Expand Down
1 change: 1 addition & 0 deletions test/test_components.py
Expand Up @@ -665,6 +665,7 @@ def test_component_functions(self):
"tool": "editor",
"name": "image",
"show_share_button": False,
"show_download_button": True,
"streaming": False,
"show_label": True,
"label": "Upload Your Image",
Expand Down

1 comment on commit a0efc11

@vercel
Copy link

@vercel vercel bot commented on a0efc11 Jul 19, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.