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

Handle the case where examples is null for all components #7192

Merged
merged 14 commits into from Jan 30, 2024
23 changes: 23 additions & 0 deletions .changeset/warm-jokes-fetch.md
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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
@@ -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
@@ -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
@@ -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
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);
abidlabs marked this conversation as resolved.
Show resolved Hide resolved
border-radius: var(--radius-lg);
overflow: hidden;
width: var(--size-20);
Expand Down
29 changes: 29 additions & 0 deletions js/image/ImageExample.stories.svelte
@@ -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
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
@@ -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
@@ -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
@@ -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
@@ -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