Skip to content

Commit ba4963e

Browse files
abidlabsclaude
andauthored
Run js functions in event listeners even when fn is not explicitly set to None (#13512)
* Run js functions in event listeners even when fn is not explicitly set to None Fixes #6729. Event listeners (and gr.on) default fn to the "decorator" sentinel, so calls like btn.click(js=js) silently registered nothing unless fn=None was passed explicitly. When fn is the sentinel and js is a string, register the event immediately with fn=None; if the returned Dependency is then used as a decorator, remove the js-only event and register the decorated function instead. Co-Authored-By: Claude Fable 5 <[email protected]> * Forward api_description, time_limit, stream_every in decorator wrappers The decorator re-registration paths silently dropped api_description, time_limit, and stream_every (event_trigger wrapper) and api_description (gr.on wrapper). Pre-existing gap surfaced by review. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
1 parent df59064 commit ba4963e

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎.changeset/major-jokes-occur.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": patch
3+
---
4+
5+
fix:Run `js` functions in event listeners even when `fn` is not explicitly set to `None`

‎gradio/events.py‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,14 +671,60 @@ def event_trigger(
671671
"""
672672

673673
if fn == "decorator":
674+
root_block = get_blocks_context()
675+
js_only_dep: Dependency | None = None
676+
created_fn_ids: set[int] = set()
677+
prior_fn_id = post_fn_id = 0
678+
if isinstance(js, str) and root_block is not None:
679+
# A js string can run entirely in the frontend without a backend
680+
# fn, so register the event immediately with fn=None in case this
681+
# call is not being used as a decorator, e.g. `btn.click(js=...)`.
682+
# If it is used as a decorator after all, `wrapper` removes this
683+
# js-only event before registering the decorated function.
684+
prior_fn_ids = set(root_block.fns)
685+
prior_fn_id = root_block.fn_id
686+
js_only_dep = event_trigger(
687+
block=block,
688+
fn=None,
689+
inputs=inputs,
690+
outputs=outputs,
691+
api_name=api_name,
692+
api_description=api_description,
693+
scroll_to_output=scroll_to_output,
694+
show_progress=show_progress,
695+
show_progress_on=show_progress_on,
696+
queue=queue,
697+
batch=batch,
698+
max_batch_size=max_batch_size,
699+
preprocess=preprocess,
700+
postprocess=postprocess,
701+
cancels=cancels,
702+
trigger_mode=trigger_mode,
703+
js=js,
704+
concurrency_limit=concurrency_limit,
705+
concurrency_id=concurrency_id,
706+
api_visibility=api_visibility,
707+
time_limit=time_limit,
708+
stream_every=stream_every,
709+
key=key,
710+
validator=validator,
711+
)
712+
created_fn_ids = set(root_block.fns) - prior_fn_ids
713+
post_fn_id = root_block.fn_id
674714

675715
def wrapper(func):
716+
if js_only_dep is not None and root_block is not None:
717+
for fn_id in created_fn_ids:
718+
root_block.fns.pop(fn_id, None)
719+
if root_block.fn_id == post_fn_id:
720+
root_block.fn_id = prior_fn_id
676721
event_trigger(
677722
block=block,
678723
fn=func,
679724
inputs=inputs,
680725
outputs=outputs,
681726
api_name=api_name,
727+
api_description=api_description,
682728
scroll_to_output=scroll_to_output,
683729
show_progress=show_progress,
684730
show_progress_on=show_progress_on,
@@ -693,6 +739,8 @@ def wrapper(func):
693739
concurrency_limit=concurrency_limit,
694740
concurrency_id=concurrency_id,
695741
api_visibility=api_visibility,
742+
time_limit=time_limit,
743+
stream_every=stream_every,
696744
key=key,
697745
validator=validator,
698746
)
@@ -703,6 +751,9 @@ def inner(*args, **kwargs):
703751

704752
return inner
705753

754+
if js_only_dep is not None:
755+
js_only_dep.fn = wrapper
756+
return js_only_dep
706757
return Dependency(None, {}, None, wrapper)
707758

708759
from gradio.components.base import StreamingInput
@@ -872,14 +923,60 @@ def on(
872923
inputs = [inputs] # type: ignore
873924

874925
if fn == "decorator":
926+
root_block = get_blocks_context()
927+
js_only_dep: Dependency | None = None
928+
created_fn_ids: set[int] = set()
929+
prior_fn_id = post_fn_id = 0
930+
if isinstance(js, str) and root_block is not None:
931+
# A js string can run entirely in the frontend without a backend fn, so
932+
# register the event immediately with fn=None in case this call is not
933+
# being used as a decorator, e.g. `gr.on(triggers, js=...)`. If it is
934+
# used as a decorator after all, `wrapper` removes this js-only event
935+
# before registering the decorated function.
936+
prior_fn_ids = set(root_block.fns)
937+
prior_fn_id = root_block.fn_id
938+
js_only_dep = on(
939+
triggers,
940+
fn=None,
941+
inputs=inputs,
942+
outputs=outputs,
943+
api_name=api_name,
944+
api_description=api_description,
945+
scroll_to_output=scroll_to_output,
946+
show_progress=show_progress,
947+
show_progress_on=show_progress_on,
948+
queue=queue,
949+
batch=batch,
950+
max_batch_size=max_batch_size,
951+
preprocess=preprocess,
952+
postprocess=postprocess,
953+
cancels=cancels,
954+
js=js,
955+
concurrency_limit=concurrency_limit,
956+
concurrency_id=concurrency_id,
957+
api_visibility=api_visibility,
958+
trigger_mode=trigger_mode,
959+
time_limit=time_limit,
960+
stream_every=stream_every,
961+
key=key,
962+
validator=validator,
963+
)
964+
created_fn_ids = set(root_block.fns) - prior_fn_ids
965+
post_fn_id = root_block.fn_id
875966

876967
def wrapper(func):
968+
if js_only_dep is not None and root_block is not None:
969+
for fn_id in created_fn_ids:
970+
root_block.fns.pop(fn_id, None)
971+
if root_block.fn_id == post_fn_id:
972+
root_block.fn_id = prior_fn_id
877973
on(
878974
triggers,
879975
fn=func,
880976
inputs=inputs,
881977
outputs=outputs,
882978
api_name=api_name,
979+
api_description=api_description,
883980
scroll_to_output=scroll_to_output,
884981
show_progress=show_progress,
885982
show_progress_on=show_progress_on,
@@ -906,6 +1003,9 @@ def inner(*args, **kwargs):
9061003

9071004
return inner
9081005

1006+
if js_only_dep is not None:
1007+
js_only_dep.fn = wrapper
1008+
return js_only_dep
9091009
return Dependency(None, {}, None, wrapper)
9101010

9111011
root_block = get_blocks_context()

‎test/test_events.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,60 @@ def sum(a, b, c):
116116
(0, "load"),
117117
]
118118

119+
def test_js_only_event_without_explicit_fn_none(self):
120+
js = "() => { alert('hi'); }"
121+
122+
with gr.Blocks() as demo:
123+
b1 = gr.Button("explicit fn=None")
124+
b2 = gr.Button("no fn")
125+
b3 = gr.Button("gr.on no fn")
126+
127+
b1.click(js=js, fn=None)
128+
b2.click(js=js)
129+
gr.on(triggers=[b3.click], js=js)
130+
131+
dependencies = demo.config["dependencies"]
132+
assert len(dependencies) == 3
133+
for button in (b1, b2, b3):
134+
deps = [
135+
dep
136+
for dep in dependencies
137+
if (button._id, "click") in [tuple(t) for t in dep["targets"]]
138+
]
139+
assert len(deps) == 1
140+
assert deps[0]["js"] == js
141+
assert demo.fns[deps[0]["id"]].fn is None
142+
143+
def test_decorator_with_js_registers_single_event(self):
144+
js = "() => { alert('hi'); }"
145+
146+
with gr.Blocks() as demo:
147+
textbox = gr.Textbox()
148+
button = gr.Button("decorated")
149+
on_button = gr.Button("gr.on decorated")
150+
151+
@button.click(inputs=textbox, outputs=textbox, js=js)
152+
def greet(value):
153+
return value + "!"
154+
155+
@gr.on(triggers=[on_button.click], inputs=textbox, outputs=textbox, js=js)
156+
def on_greet(value):
157+
return value + "?"
158+
159+
dependencies = demo.config["dependencies"]
160+
assert len(dependencies) == 2
161+
for component in (button, on_button):
162+
deps = [
163+
dep
164+
for dep in dependencies
165+
if (component._id, "click") in [tuple(t) for t in dep["targets"]]
166+
]
167+
assert len(deps) == 1
168+
assert deps[0]["js"] == js
169+
assert demo.fns[deps[0]["id"]].fn is not None
170+
assert greet("a") == "a!"
171+
assert on_greet("a") == "a?"
172+
119173
def test_load_chaining(self):
120174
calls = 0
121175

0 commit comments

Comments
 (0)