Skip to content

Commit

Permalink
ui tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
pngwn committed Apr 19, 2024
1 parent a3d5678 commit 099c152
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 17 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ This component can be used in several ways:


## Installation
```bash

```bash
pip install gradio_imageslider
```

Expand All @@ -33,7 +33,7 @@ def fn(im):

with gr.Blocks() as demo:
with gr.Group():
img1 = ImageSlider(label="Blur image", type="pil")
img1 = ImageSlider(label="Blur image", type="pil", slider_color="pink")
img1.upload(fn, inputs=img1, outputs=img1)

if __name__ == "__main__":
Expand Down Expand Up @@ -291,6 +291,19 @@ bool | None
<td align="left"><code>None</code></td>
<td align="left">If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.</td>
</tr>

<tr>
<td align="left"><code>slider_color</code></td>
<td align="left" style="width: 25%;">

```python
str | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">The color of the slider separator.</td>
</tr>
</tbody></table>


Expand All @@ -307,8 +320,8 @@ bool | None

The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).

- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.

The code snippet below is accurate in cases where the component is used as both an input and an output.

Expand Down
4 changes: 4 additions & 0 deletions backend/gradio_imageslider/imageslider.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
show_share_button: bool | None = None,
slider_color: str | None = None,
**kwargs,
):
"""
Expand All @@ -91,6 +92,7 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
slider_color: The color of the slider separator.
"""

valid_types = ["numpy", "pil", "filepath"]
Expand All @@ -109,6 +111,8 @@ def __init__(
)
self.position = position
self.upload_count = upload_count
self.slider_color = slider_color

super().__init__(
label=label,
every=every,
Expand Down
2 changes: 1 addition & 1 deletion demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def fn(im):

with gr.Blocks() as demo:
with gr.Group():
img1 = ImageSlider(label="Blur image", type="pil")
img1 = ImageSlider(label="Blur image", type="pil", slider_color="pink")
img1.upload(fn, inputs=img1, outputs=img1)

if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions demo/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from app import demo as app
import os

_docs = {'ImageSlider': {'description': 'Creates an image slider component that can be used to compare two images', 'members': {'__init__': {'value': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'default': 'None', 'description': 'A PIL Image, numpy array, path or URL for the default value that Image component is going to take. If callable, the function will be called whenever the app loads to set the initial value of the component.'}, 'position': {'type': 'int', 'default': '0.5', 'description': 'The position of the slider, between 0 and 1.'}, 'upload_count': {'type': 'int', 'default': '1', 'description': 'The number of images that can be uploaded to the component. 1 or 2.'}, 'height': {'type': 'int | None', 'default': 'None', 'description': 'Height of the displayed image in pixels.'}, 'width': {'type': 'int | None', 'default': 'None', 'description': 'Width of the displayed image in pixels.'}, 'type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted to before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'component name in interface.'}, 'every': {'type': 'float | None', 'default': 'None', 'description': "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': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will display button to download image.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': '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': {'type': 'int', 'default': '160', 'description': '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.'}, 'interactive': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will allow users to upload and edit an image; if False, can only be used to display images. If not provided, this is inferred based on whether the component is used as an input or output.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}}, 'postprocess': {'y': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'description': 'image as a numpy array, PIL Image, string/Path filepath, or string URL'}, 'value': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'description': 'image as a numpy array, PIL Image, string/Path filepath, or string URL'}}, 'preprocess': {'return': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'description': 'tuple of images in the requested format.'}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the ImageSlider changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the ImageSlider.'}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'ImageSlider': []}}}
_docs = {'ImageSlider': {'description': 'Creates an image slider component that can be used to compare two images', 'members': {'__init__': {'value': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'default': 'None', 'description': 'A PIL Image, numpy array, path or URL for the default value that Image component is going to take. If callable, the function will be called whenever the app loads to set the initial value of the component.'}, 'position': {'type': 'int', 'default': '0.5', 'description': 'The position of the slider, between 0 and 1.'}, 'upload_count': {'type': 'int', 'default': '1', 'description': 'The number of images that can be uploaded to the component. 1 or 2.'}, 'height': {'type': 'int | None', 'default': 'None', 'description': 'Height of the displayed image in pixels.'}, 'width': {'type': 'int | None', 'default': 'None', 'description': 'Width of the displayed image in pixels.'}, 'type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted to before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'component name in interface.'}, 'every': {'type': 'float | None', 'default': 'None', 'description': "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': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will display button to download image.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': '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': {'type': 'int', 'default': '160', 'description': '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.'}, 'interactive': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will allow users to upload and edit an image; if False, can only be used to display images. If not provided, this is inferred based on whether the component is used as an input or output.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'slider_color': {'type': 'str | None', 'default': 'None', 'description': 'The color of the slider separator.'}}, 'postprocess': {'y': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'description': 'image as a numpy array, PIL Image, string/Path filepath, or string URL'}, 'value': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'description': 'image as a numpy array, PIL Image, string/Path filepath, or string URL'}}, 'preprocess': {'return': {'type': 'tuple[str, str]\n | tuple[PIL.Image.Image, PIL.Image.Image]\n | tuple[numpy.ndarray, numpy.ndarray]\n | None', 'description': 'tuple of images in the requested format.'}, 'value': None}}, 'events': {'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the ImageSlider changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the ImageSlider.'}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'ImageSlider': []}}}

abs_path = os.path.join(os.path.dirname(__file__), "css.css")

with gr.Blocks(
Expand Down Expand Up @@ -58,7 +58,7 @@ def fn(im):
with gr.Blocks() as demo:
with gr.Group():
img1 = ImageSlider(label="Blur image", type="pil")
img1 = ImageSlider(label="Blur image", type="pil", slider_color="pink")
img1.upload(fn, inputs=img1, outputs=img1)
if __name__ == "__main__":
Expand Down Expand Up @@ -89,8 +89,8 @@ def fn(im):
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
Expand Down Expand Up @@ -132,7 +132,7 @@ def predict(
})
}
})
Object.entries(refs).forEach(([key, refs]) => {
if (refs.length > 0) {
const el = document.querySelector(`.${key}`);
Expand Down
6 changes: 6 additions & 0 deletions frontend/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
export let interactive: boolean;
export let position: number;
export let upload_count: number = 2;
export let slider_color = "var(--border-color-primary)"
$: console.log($$props)
export let gradio: Gradio<{
change: never;
Expand All @@ -39,6 +43,7 @@

{#if interactive}
<InteractiveImage
{slider_color}
{elem_id}
{elem_classes}
{visible}
Expand All @@ -55,6 +60,7 @@
/>
{:else}
<StaticImage
{slider_color}
i18n={gradio.i18n}
{elem_id}
{elem_classes}
Expand Down
1 change: 1 addition & 0 deletions frontend/shared/ArrowIcon.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24"><path fill="currentColor" d="M16 22L6 12L16 2l1.775 1.775L9.55 12l8.225 8.225z"/></svg>
5 changes: 3 additions & 2 deletions frontend/shared/Image.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
export let position: number;
export let upload_count: number = 2;
export let show_download_button = true;
export let slider_color: string;
let value_: [FileData | null, FileData | null] = value || [null, null];
Expand Down Expand Up @@ -88,7 +89,7 @@
{/if}
</div>
{/if}
<Slider bind:position disabled={upload_count == 2 || !value?.[0]}>
<Slider bind:position disabled={upload_count == 2 || !value?.[0]} {slider_color}>
<div
class="upload-wrap"
style:display={upload_count === 2 ? "flex" : "block"}
Expand Down Expand Up @@ -179,7 +180,7 @@
top: 0;
left: 0;
background-color: rgba(var(--anim-block-background-fill), 0.8);
z-index: 4;
z-index: 0;
}
@media (prefers-color-scheme: dark) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/shared/ImagePreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
export let position: number;
export let layer_images = true;
export let show_single = false;
export let slider_color: string;
$: value = normalise_file(value, root, null) as [FileData, FileData];
Expand All @@ -38,7 +39,7 @@
{/if}
</div>
<div class="slider-wrap" bind:clientWidth={el_width}>
<Slider bind:position>
<Slider bind:position {slider_color}>
<img src={value?.[0]?.url} alt="" loading="lazy" />
<img
class:fixed={layer_images}
Expand Down
2 changes: 2 additions & 0 deletions frontend/shared/InteractiveImage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
export let root: string;
export let position: number;
export let upload_count: number = 2;
export let slider_color: string;
export let gradio: Gradio<{
change: never;
Expand Down Expand Up @@ -81,6 +82,7 @@
/>

<Image
{slider_color}
{position}
bind:value
{root}
Expand Down
32 changes: 30 additions & 2 deletions frontend/shared/Slider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import { drag } from "d3-drag";
import { select } from "d3-selection";
import { clamp } from "./utils";
import Arrow from './ArrowIcon.svelte'
export let position = 0.5;
export let disabled = false;
export let slider_color = "var(--border-color-primary)";
let active = false;
let el;
Expand Down Expand Up @@ -65,7 +68,9 @@
role="none"
style="transform: translateX({px}px)"
>
<div class="inner"></div>
<span class="icon-wrap" class:active class:disabled><Arrow /></span>
<div class="inner" style:--color={slider_color}></div>
<span class="icon-wrap right" class:active class:disabled><Arrow /></span>
</div>
</div>

Expand All @@ -74,8 +79,31 @@
position: relative;
width: 100%;
height: 100%;
z-index: 100;
}
.icon-wrap {
position: absolute;
top: 50%;
transform: translateY(-50%);
left: -40px;
width: 32px;
transition: 0.2s;
color: var(--body-text-color);
}
.icon-wrap.right {
left: 60px;
transform: translateY(-50%) translateX(-100%) rotate(180deg);
}
.icon-wrap.active {
opacity: 0;
}
.icon-wrap.disabled {
opacity: 0;
}
.outer {
width: 20px;
height: 100%;
Expand All @@ -90,7 +118,7 @@
box-shadow: -1px 0px 6px 1px rgba(0, 0, 0, 0.2);
width: 1px;
height: 100%;
background: #555;
background: var(--color);
position: absolute;
left: calc((100% - 2px) / 2);
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/shared/StaticImage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
export let width: number | undefined;
export let layer_images = true;
export let i18n: I18nFormatter;
export let slider_color: string;
export let container = true;
export let scale: number | null = null;
Expand Down Expand Up @@ -95,6 +96,7 @@
i18n={gradio.i18n}
{layer_images}
show_single={is_half}
{slider_color}
/>
</Block>

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ artifacts = [
"*.pyi",
"backend/gradio_imageslider/templates",
"backend/gradio_imageslider/templates",
"backend/gradio_imageslider/templates",
]

[tool.hatch.build.targets.wheel]
Expand Down

0 comments on commit 099c152

Please sign in to comment.