Skip to content

Commit

Permalink
Handle the case where examples is null for all components (#7192)
Browse files Browse the repository at this point in the history
* handle null examples

* add changeset

* add changeset

* lint

* merge conflict

* fixes

* add changeset

* stories

* feedback

* examples

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
abidlabs and gradio-pr-bot authored Jan 30, 2024
1 parent e3217b4 commit 8dd6f4b
Show file tree
Hide file tree
Showing 25 changed files with 380 additions and 205 deletions.
23 changes: 23 additions & 0 deletions .changeset/warm-jokes-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"@gradio/audio": patch
"@gradio/checkbox": patch
"@gradio/code": patch
"@gradio/colorpicker": patch
"@gradio/dataset": patch
"@gradio/dropdown": patch
"@gradio/file": patch
"@gradio/fileexplorer": patch
"@gradio/image": patch
"@gradio/markdown": patch
"@gradio/model3d": patch
"@gradio/number": patch
"@gradio/radio": patch
"@gradio/simpledropdown": patch
"@gradio/simpleimage": patch
"@gradio/simpletextbox": patch
"@gradio/textbox": patch
"@gradio/video": patch
"gradio": patch
---

fix:Handle the case where examples is `null` for all components
24 changes: 24 additions & 0 deletions js/audio/AudioExample.stories.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import Audio from "./Example.svelte";
</script>

<Meta title="Components/Audio/Example" component={Audio} />

<Template let:args>
<Audio {...args} />
</Template>

<Story
name="Audio file"
args={{
value: "cantina.mp3"
}}
/>

<Story
name="Null"
args={{
value: null
}}
/>
4 changes: 2 additions & 2 deletions js/audio/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
</script>
Expand All @@ -9,7 +9,7 @@
class:gallery={type === "gallery"}
class:selected
>
{value}
{value ? value : ""}
</div>

<style>
Expand Down
4 changes: 2 additions & 2 deletions js/checkbox/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
export let value: boolean;
export let value: boolean | null;
export let type: "gallery" | "table";
export let selected = false;
</script>
Expand All @@ -9,7 +9,7 @@
class:gallery={type === "gallery"}
class:selected
>
{value.toLocaleString()}
{value !== null ? value.toLocaleString() : ""}
</div>

<style>
Expand Down
4 changes: 2 additions & 2 deletions js/code/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts">
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
</script>

<pre
class:table={type === "table"}
class:gallery={type === "gallery"}
class:selected>{value}</pre>
class:selected>{value ? value : ""}</pre>

<style>
pre {
Expand Down
24 changes: 24 additions & 0 deletions js/colorpicker/ColorPickerExample.stories.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import ColorPicker from "./Example.svelte";
</script>

<Meta title="Components/Color Picker/Example" component={ColorPicker} />

<Template let:args>
<ColorPicker {...args} />
</Template>

<Story
name="Color value"
args={{
value: "#ff0000"
}}
/>

<Story
name="Null"
args={{
value: null
}}
/>
4 changes: 2 additions & 2 deletions js/colorpicker/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script lang="ts">
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
</script>

<div
style="background-color: {value}"
style="background-color: {value ? value : 'black'}"
class:table={type === "table"}
class:gallery={type === "gallery"}
class:selected
Expand Down
46 changes: 24 additions & 22 deletions js/dataset/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,30 @@
{#if gallery}
<div class="gallery">
{#each selected_samples as sample_row, i}
<button
class="gallery-item"
on:click={() => {
value = i + page * samples_per_page;
gradio.dispatch("click", value);
gradio.dispatch("select", { index: value, value: sample_row });
}}
on:mouseenter={() => handle_mouseenter(i)}
on:mouseleave={() => handle_mouseleave()}
>
{#if component_meta.length && component_map.get(components[0])}
<svelte:component
this={component_meta[0][0].component}
{...component_props[0]}
value={sample_row[0]}
{samples_dir}
type="gallery"
selected={current_hover === i}
index={i}
/>
{/if}
</button>
{#if sample_row[0]}
<button
class="gallery-item"
on:click={() => {
value = i + page * samples_per_page;
gradio.dispatch("click", value);
gradio.dispatch("select", { index: value, value: sample_row });
}}
on:mouseenter={() => handle_mouseenter(i)}
on:mouseleave={() => handle_mouseleave()}
>
{#if component_meta.length && component_map.get(components[0])}
<svelte:component
this={component_meta[0][0].component}
{...component_props[0]}
value={sample_row[0]}
{samples_dir}
type="gallery"
selected={current_hover === i}
index={i}
/>
{/if}
</button>
{/if}
{/each}
</div>
{:else}
Expand Down
4 changes: 2 additions & 2 deletions js/dropdown/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
export let value: string | string[];
export let value: string | string[] | null;
export let type: "gallery" | "table";
export let selected = false;
export let choices: [string, string | number][];
let value_array = Array.isArray(value) ? value : [value];
let value_array = value ? (Array.isArray(value) ? value : [value]) : [];
let names = value_array
.map(
(val) =>
Expand Down
4 changes: 2 additions & 2 deletions js/file/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import type { FileData } from "@gradio/client";
export let value: FileData;
export let value: FileData | null;
export let type: "gallery" | "table";
export let selected = false;
</script>
Expand All @@ -11,7 +11,7 @@
class:gallery={type === "gallery"}
class:selected
>
{Array.isArray(value) ? value.join(", ") : value}
{value ? (Array.isArray(value) ? value.join(", ") : value) : ""}
</div>

<style>
Expand Down
16 changes: 8 additions & 8 deletions js/fileexplorer/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
import type { FileData } from "@gradio/client";
export let value: string[] | string;
export let value: string[] | string | null;
export let type: "gallery" | "table";
export let selected = false;
</script>
Expand All @@ -11,11 +9,13 @@
class:gallery={type === "gallery"}
class:selected
>
{#each Array.isArray(value) ? value.slice(0, 3) : [value] as path}
<li><code>./{path}</code></li>
{/each}
{#if Array.isArray(value) && value.length > 3}
<li class="extra">...</li>
{#if value}
{#each Array.isArray(value) ? value.slice(0, 3) : [value] as path}
<li><code>./{path}</code></li>
{/each}
{#if Array.isArray(value) && value.length > 3}
<li class="extra">...</li>
{/if}
{/if}
</ul>

Expand Down
9 changes: 7 additions & 2 deletions js/image/Example.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
class:table={type === "table"}
class:gallery={type === "gallery"}
class:selected
class:border={value}
>
<Image src={samples_dir + value?.path} alt="" />
{#if value}
<Image src={samples_dir + value.path} alt="" />
{/if}
</div>

<style>
Expand All @@ -26,10 +29,12 @@
.container.selected {
border-color: var(--border-color-accent);
}
.border.table {
border: 2px solid var(--border-color-primary);
}
.container.table {
margin: 0 auto;
border: 2px solid var(--border-color-primary);
border-radius: var(--radius-lg);
overflow: hidden;
width: var(--size-20);
Expand Down
29 changes: 29 additions & 0 deletions js/image/ImageExample.stories.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import Image from "./Example.svelte";
</script>

<Meta title="Components/Image/Example" component={Image} />

<Template let:args>
<Image {...args} />
</Template>

<Story
name="Image file"
args={{
samples_dir: "",
value: {
path: "https://gradio-builds.s3.amazonaws.com/demo-files/ghepardo-primo-piano.jpg",
url: "https://gradio-builds.s3.amazonaws.com/demo-files/ghepardo-primo-piano.jpg",
orig_name: "cheetah.jpg"
}
}}
/>

<Story
name="Null"
args={{
value: null
}}
/>
6 changes: 6 additions & 0 deletions js/image/shared/Image.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@

<!-- svelte-ignore a11y-missing-attribute -->
<img src={resolved_src} {...$$restProps} />

<style>
img {
object-fit: cover;
}
</style>
4 changes: 2 additions & 2 deletions js/markdown/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import MarkdownCode from "./shared/MarkdownCode.svelte";
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
export let sanitize_html: boolean;
Expand All @@ -20,7 +20,7 @@
class="prose"
>
<MarkdownCode
message={value}
message={value ? value : ""}
{latex_delimiters}
{sanitize_html}
{line_breaks}
Expand Down
4 changes: 2 additions & 2 deletions js/model3D/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
</script>
Expand All @@ -9,7 +9,7 @@
class:gallery={type === "gallery"}
class:selected
>
{value}
{value ? value : ""}
</div>

<style>
Expand Down
4 changes: 2 additions & 2 deletions js/number/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
</script>
Expand All @@ -9,7 +9,7 @@
class:gallery={type === "gallery"}
class:selected
>
{value}
{value ? value : ""}
</div>

<style>
Expand Down
12 changes: 9 additions & 3 deletions js/radio/Example.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<script lang="ts">
export let value: string;
export let value: string | null;
export let type: "gallery" | "table";
export let selected = false;
export let choices: [string, string | number][];
let name = choices.find((pair) => pair[1] === value);
let name_string = name ? name[0] : "";
let name_string: string;
if (value === null) {
name_string = "";
} else {
let name = choices.find((pair) => pair[1] === value);
name_string = name ? name[0] : "";
}
</script>

<div
Expand Down
Loading

0 comments on commit 8dd6f4b

Please sign in to comment.