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 show_download_button to gr.Image() #4959

Merged
merged 14 commits into from Jul 19, 2023
Merged
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,
hannahblair marked this conversation as resolved.
Show resolved Hide resolved
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