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

Allow empty value in gr.Number #8974

Closed
wants to merge 12 commits into from
6 changes: 6 additions & 0 deletions .changeset/deep-teams-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/number": patch
"gradio": patch
---

fix:Allow empty value in gr.Number
4 changes: 3 additions & 1 deletion gradio/components/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ def preprocess(self, payload: float | None) -> float | int | None:
"""
if payload is None:
return None
elif self.minimum is not None and payload < self.minimum:

if self.minimum is not None and payload < self.minimum:
raise Error(f"Value {payload} is less than minimum value {self.minimum}.")
elif self.maximum is not None and payload > self.maximum:
raise Error(
f"Value {payload} is greater than maximum value {self.maximum}."
)

Comment on lines +124 to +131
Copy link
Member

Choose a reason for hiding this comment

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

IIUC this change should have no effect since the first branch terminates in a return statement, am I misunderstanding?

return self._round_to_precision(payload, self.precision)

def postprocess(self, value: float | int | None) -> float | int | None:
Expand Down
20 changes: 13 additions & 7 deletions js/number/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Block, BlockTitle } from "@gradio/atoms";
import { StatusTracker } from "@gradio/statustracker";
import type { LoadingStatus } from "@gradio/statustracker";
import { afterUpdate, tick } from "svelte";
import { afterUpdate, tick, onMount } from "svelte";

export let gradio: Gradio<{
change: never;
Expand All @@ -21,7 +21,7 @@
export let container = true;
export let scale: number | null = null;
export let min_width: number | undefined = undefined;
export let value = 0;
export let value: number | null = 0;
export let show_label: boolean;
export let minimum: number | undefined = undefined;
export let maximum: number | undefined = undefined;
Expand All @@ -31,13 +31,17 @@
export let interactive: boolean;

function handle_change(): void {
if (!isNaN(value) && value !== null) {
if (!isNaN(parseInt(input_value)) && value !== null) {
value = parseInt(input_value);
gradio.dispatch("change");
if (!value_is_output) {
gradio.dispatch("input");
}
}
}

$: input_value = value === null ? "" : value.toString();

afterUpdate(() => {
value_is_output = false;
});
Expand All @@ -46,11 +50,11 @@
await tick();
if (e.key === "Enter") {
e.preventDefault();
handle_change();
gradio.dispatch("submit");
}
}

$: value, handle_change();
$: disabled = !interactive;
</script>

Expand All @@ -73,11 +77,13 @@
<BlockTitle {show_label} {info}>{label}</BlockTitle>
<input
aria-label={label}
type="number"
bind:value
type="text"
bind:value={input_value}
min={minimum}
max={maximum}
{step}
on:change={handle_change}
on:input={handle_change}
on:keypress={handle_keypress}
on:blur={() => gradio.dispatch("blur")}
on:focus={() => gradio.dispatch("focus")}
Expand All @@ -96,7 +102,7 @@
border: var(--input-border-width) solid var(--input-border-color);
border-radius: var(--input-radius);
}
input[type="number"] {
input[type="text"] {
Copy link
Member

Choose a reason for hiding this comment

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

This means that browsers won't treat this field as numeric. On Desktop it means we'll lose these:

image

On mobile, I think it will lead to a ux degradation since the numeric keyboard won't pop up anymore

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this change should be needed since at least according to the original issue, it is in theory possible to set a numeric field blank:

Currently, I can set the value to Callable, where Callable returns None. I have tested this, and the frontend displays it as blank, which aligns with my requirements.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This means that browsers won't treat this field as numeric. On Desktop it means we'll lose these:

Thats a good point, we don't want to lose that :/

display: block;
position: relative;
outline: none !important;
Expand Down
7 changes: 7 additions & 0 deletions js/number/Number.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
}}
/>

<Story
name="Number with empty value"
args={{
value: null
}}
/>

<Story
name="Number with step of 10"
args={{
Expand Down