Skip to content

Commit b566f37

Browse files
Set default value to no value
1 parent 83f7e2e commit b566f37

7 files changed

Lines changed: 52 additions & 36 deletions

File tree

demo/reset_components/run.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@
8989
component.render()
9090
reset = gr.Button(value="Reset")
9191
hide = gr.Button(value="Hide")
92+
reveal = gr.Button(value="Reveal")
9293
reset.click(
93-
lambda: [c.update(value=gr.Keywords.VOID) for c in components],
94+
lambda: [c.update(value=None) for c in components],
9495
inputs=[],
9596
outputs=components,
9697
)
@@ -99,6 +100,11 @@
99100
inputs=[],
100101
outputs=components
101102
)
103+
reveal.click(
104+
lambda: [c.update(visible=True) for c in components],
105+
inputs=[],
106+
outputs=components
107+
)
102108

103109

104110
if __name__ == "__main__":

gradio/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
Image,
2929
Interpretation,
3030
Json,
31-
Keywords,
3231
Label,
3332
Markdown,
3433
Model3D,

gradio/blocks.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,15 @@ def postprocess_data(self, fn_index, predictions, state):
667667
prediction_value = block.__class__.update(
668668
**prediction_value
669669
)
670-
# Important to replace VOID values to None after delete_none
671-
# This way the value is only set to None if user uses gr.VOID
670+
# If the prediction is the default (NO_VALUE) enum then the user did
671+
# not specify a value for the 'value' key and we can get rid of it
672+
if (
673+
prediction_value.get("value")
674+
== components.Keywords.NO_VALUE
675+
):
676+
prediction_value.pop("value")
672677
prediction_value = delete_none(prediction_value)
673678
if "value" in prediction_value:
674-
if prediction_value["value"] == components.Keywords.VOID:
675-
prediction_value["value"] = None
676679
prediction_value["value"] = block.postprocess(
677680
prediction_value["value"]
678681
)

gradio/components.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class DataframeData(TypedDict):
6262

6363
class Keywords(Enum):
6464
# Set value in front-end to null
65-
VOID = "VOID"
65+
NO_VALUE = "NO_VALUE"
6666

6767

6868
class Component(Block):
@@ -292,7 +292,7 @@ def get_config(self):
292292

293293
@staticmethod
294294
def update(
295-
value: Optional[str] = None,
295+
value: Optional[str] = Keywords.NO_VALUE,
296296
lines: Optional[int] = None,
297297
max_lines: Optional[int] = None,
298298
placeholder: Optional[str] = None,
@@ -471,7 +471,7 @@ def get_config(self):
471471

472472
@staticmethod
473473
def update(
474-
value: Optional[float] = None,
474+
value: Optional[float] = Keywords.NO_VALUE,
475475
label: Optional[str] = None,
476476
show_label: Optional[bool] = None,
477477
interactive: Optional[bool] = None,
@@ -643,7 +643,7 @@ def get_random_value(self):
643643

644644
@staticmethod
645645
def update(
646-
value: Optional[float] = None,
646+
value: Optional[float] = Keywords.NO_VALUE,
647647
minimum: Optional[float] = None,
648648
maximum: Optional[float] = None,
649649
step: Optional[float] = None,
@@ -769,7 +769,7 @@ def get_config(self):
769769

770770
@staticmethod
771771
def update(
772-
value: Optional[bool] = None,
772+
value: Optional[bool] = Keywords.NO_VALUE,
773773
label: Optional[str] = None,
774774
show_label: Optional[bool] = None,
775775
interactive: Optional[bool] = None,
@@ -867,7 +867,7 @@ def get_config(self):
867867

868868
@staticmethod
869869
def update(
870-
value: Optional[List[str]] = None,
870+
value: Optional[List[str]] = Keywords.NO_VALUE,
871871
choices: Optional[List[str]] = None,
872872
label: Optional[str] = None,
873873
show_label: Optional[bool] = None,
@@ -1030,7 +1030,7 @@ def get_config(self):
10301030

10311031
@staticmethod
10321032
def update(
1033-
value: Optional[Any] = None,
1033+
value: Optional[Any] = Keywords.NO_VALUE,
10341034
choices: Optional[List[str]] = None,
10351035
label: Optional[str] = None,
10361036
show_label: Optional[bool] = None,
@@ -1263,7 +1263,7 @@ def get_config(self):
12631263

12641264
@staticmethod
12651265
def update(
1266-
value: Optional[Any] = None,
1266+
value: Optional[Any] = Keywords.NO_VALUE,
12671267
label: Optional[str] = None,
12681268
show_label: Optional[bool] = None,
12691269
interactive: Optional[bool] = None,
@@ -1587,7 +1587,7 @@ def get_config(self):
15871587

15881588
@staticmethod
15891589
def update(
1590-
value: Optional[Any] = None,
1590+
value: Optional[Any] = Keywords.NO_VALUE,
15911591
source: Optional[str] = None,
15921592
label: Optional[str] = None,
15931593
show_label: Optional[bool] = None,
@@ -1774,7 +1774,7 @@ def get_config(self):
17741774

17751775
@staticmethod
17761776
def update(
1777-
value: Optional[Any] = None,
1777+
value: Optional[Any] = Keywords.NO_VALUE,
17781778
source: Optional[str] = None,
17791779
label: Optional[str] = None,
17801780
show_label: Optional[bool] = None,
@@ -2052,7 +2052,7 @@ def get_config(self):
20522052

20532053
@staticmethod
20542054
def update(
2055-
value: Optional[Any] = None,
2055+
value: Optional[Any] = Keywords.NO_VALUE,
20562056
label: Optional[str] = None,
20572057
show_label: Optional[bool] = None,
20582058
interactive: Optional[bool] = None,
@@ -2288,7 +2288,7 @@ def get_config(self):
22882288

22892289
@staticmethod
22902290
def update(
2291-
value: Optional[Any] = None,
2291+
value: Optional[Any] = Keywords.NO_VALUE,
22922292
max_rows: Optional[int] = None,
22932293
max_cols: Optional[str] = None,
22942294
label: Optional[str] = None,
@@ -2500,7 +2500,7 @@ def get_config(self):
25002500

25012501
@staticmethod
25022502
def update(
2503-
value: Optional[Any] = None,
2503+
value: Optional[Any] = Keywords.NO_VALUE,
25042504
colors: Optional[List[str]] = None,
25052505
label: Optional[str] = None,
25062506
show_label: Optional[bool] = None,
@@ -2650,7 +2650,7 @@ def get_config(self):
26502650

26512651
@staticmethod
26522652
def update(
2653-
value: Optional[str] = None,
2653+
value: Optional[str] = Keywords.NO_VALUE,
26542654
variant: Optional[str] = None,
26552655
visible: Optional[bool] = None,
26562656
):
@@ -2738,7 +2738,7 @@ def get_config(self):
27382738

27392739
@staticmethod
27402740
def update(
2741-
value: Optional[str] = None,
2741+
value: Optional[str] = Keywords.NO_VALUE,
27422742
label: Optional[str] = None,
27432743
show_label: Optional[bool] = None,
27442744
visible: Optional[bool] = None,
@@ -2873,7 +2873,7 @@ def postprocess(self, y: Dict[str, float] | str | float | None) -> Dict | None:
28732873

28742874
@staticmethod
28752875
def update(
2876-
value: Optional[Dict[str, float] | str | float] = None,
2876+
value: Optional[Dict[str, float] | str | float] = Keywords.NO_VALUE,
28772877
label: Optional[str] = None,
28782878
show_label: Optional[bool] = None,
28792879
visible: Optional[bool] = None,
@@ -2963,7 +2963,9 @@ def get_config(self):
29632963

29642964
@staticmethod
29652965
def update(
2966-
value: Optional[List[Tuple[str, str | float | None]] | Dict] = None,
2966+
value: Optional[
2967+
List[Tuple[str, str | float | None]] | Dict
2968+
] = Keywords.NO_VALUE,
29672969
color_map: Optional[Dict[str, str]] = None,
29682970
show_legend: Optional[bool] = None,
29692971
label: Optional[str] = None,
@@ -3092,7 +3094,7 @@ def get_config(self):
30923094

30933095
@staticmethod
30943096
def update(
3095-
value: Optional[Any] = None,
3097+
value: Optional[Any] = Keywords.NO_VALUE,
30963098
label: Optional[str] = None,
30973099
show_label: Optional[bool] = None,
30983100
visible: Optional[bool] = None,
@@ -3177,7 +3179,7 @@ def get_config(self):
31773179

31783180
@staticmethod
31793181
def update(
3180-
value: Optional[Any] = None,
3182+
value: Optional[Any] = Keywords.NO_VALUE,
31813183
label: Optional[str] = None,
31823184
show_label: Optional[bool] = None,
31833185
visible: Optional[bool] = None,
@@ -3234,7 +3236,7 @@ def __init__(
32343236

32353237
@staticmethod
32363238
def update(
3237-
value: Optional[Any] = None,
3239+
value: Optional[Any] = Keywords.NO_VALUE,
32383240
label: Optional[str] = None,
32393241
show_label: Optional[bool] = None,
32403242
visible: Optional[bool] = None,
@@ -3368,7 +3370,7 @@ def get_config(self):
33683370

33693371
@staticmethod
33703372
def update(
3371-
value: Optional[Any] = None,
3373+
value: Optional[Any] = Keywords.NO_VALUE,
33723374
label: Optional[str] = None,
33733375
show_label: Optional[bool] = None,
33743376
visible: Optional[bool] = None,
@@ -3459,7 +3461,7 @@ def get_config(self):
34593461

34603462
@staticmethod
34613463
def update(
3462-
value: Optional[Any] = None,
3464+
value: Optional[Any] = Keywords.NO_VALUE,
34633465
color_map: Optional[Tuple[str, str]] = None,
34643466
label: Optional[str] = None,
34653467
show_label: Optional[bool] = None,
@@ -3558,7 +3560,7 @@ def get_config(self):
35583560

35593561
@staticmethod
35603562
def update(
3561-
value: Optional[Any] = None,
3563+
value: Optional[Any] = Keywords.NO_VALUE,
35623564
label: Optional[str] = None,
35633565
show_label: Optional[bool] = None,
35643566
visible: Optional[bool] = None,
@@ -3675,7 +3677,7 @@ def get_config(self):
36753677

36763678
@staticmethod
36773679
def update(
3678-
value: Optional[Any] = None,
3680+
value: Optional[Any] = Keywords.NO_VALUE,
36793681
label: Optional[str] = None,
36803682
show_label: Optional[bool] = None,
36813683
visible: Optional[bool] = None,
@@ -3763,7 +3765,7 @@ def get_config(self):
37633765

37643766
@staticmethod
37653767
def update(
3766-
value: Optional[Any] = None,
3768+
value: Optional[Any] = Keywords.NO_VALUE,
37673769
visible: Optional[bool] = None,
37683770
):
37693771
updated_config = {
@@ -3839,7 +3841,7 @@ def get_config(self):
38393841

38403842
@staticmethod
38413843
def update(
3842-
samples: Optional[Any] = None,
3844+
samples: Optional[Any] = Keywords.NO_VALUE,
38433845
visible: Optional[bool] = None,
38443846
label: Optional[str] = None,
38453847
):
@@ -3912,7 +3914,7 @@ def get_config(self):
39123914

39133915
@staticmethod
39143916
def update(
3915-
value: Optional[Any] = None,
3917+
value: Optional[Any] = Keywords.NO_VALUE,
39163918
visible: Optional[bool] = None,
39173919
):
39183920
return {
@@ -3956,7 +3958,7 @@ def get_config(self):
39563958

39573959
@staticmethod
39583960
def update(
3959-
value: Optional[Any] = None,
3961+
value: Optional[Any] = Keywords.NO_VALUE,
39603962
visible: Optional[bool] = None,
39613963
):
39623964
return {

gradio/test_data/blocks_configs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"show_label": True,
5252
"name": "image",
5353
"visible": True,
54+
"value": None,
5455
"style": {},
5556
},
5657
},
@@ -61,6 +62,7 @@
6162
"show_label": True,
6263
"name": "json",
6364
"visible": True,
65+
"value": None,
6466
"style": {},
6567
},
6668
},
@@ -98,6 +100,7 @@
98100
"name": "image",
99101
"visible": True,
100102
"style": {},
103+
"value": None,
101104
},
102105
},
103106
{
@@ -108,6 +111,7 @@
108111
"name": "json",
109112
"visible": True,
110113
"style": {},
114+
"value": None,
111115
},
112116
},
113117
{

gradio/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def delete_none(_dict):
288288
"""
289289
if isinstance(_dict, dict):
290290
for key, value in list(_dict.items()):
291+
if key == "value":
292+
continue
291293
if isinstance(value, (list, dict, tuple, set)):
292294
_dict[key] = delete_none(value)
293295
elif value is None or key is None:

test/test_blocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ def test_blocks_do_not_filter_none_values_from_updates(io_components):
253253
component.render()
254254
btn = gr.Button(value="Reset")
255255
btn.click(
256-
lambda: [gr.update(value=gr.Keywords.VOID) for _ in io_components],
256+
lambda: [gr.update(value=None) for _ in io_components],
257257
inputs=[],
258258
outputs=io_components,
259259
)
260260

261261
output = demo.postprocess_data(
262-
0, [gr.update(value=gr.Keywords.VOID) for _ in io_components], state=None
262+
0, [gr.update(value=None) for _ in io_components], state=None
263263
)
264264
assert all(
265265
[o["value"] == c.postprocess(None) for o, c in zip(output, io_components)]

0 commit comments

Comments
 (0)