mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-30 11:00:11 +08:00
Fix component update bug (#6368)
* Add code * Add comment * add changeset * add changeset --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
parent
4d3aad33a0
commit
8a3f45c261
5
.changeset/early-geese-relate.md
Normal file
5
.changeset/early-geese-relate.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
fix:Fix component update bug
|
@ -132,7 +132,13 @@ def updateable(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
fn_args = inspect.getfullargspec(fn).args
|
||||
self = args[0]
|
||||
if not hasattr(self, "_constructor_args"):
|
||||
|
||||
# We need to ensure __init__ is always called at least once
|
||||
# so that the component has all the variables in self defined
|
||||
# test_blocks.py::test_async_iterator_update_with_new_component
|
||||
# checks this
|
||||
initialized_before = hasattr(self, "_constructor_args")
|
||||
if not initialized_before:
|
||||
self._constructor_args = []
|
||||
for i, arg in enumerate(args):
|
||||
if i == 0 or i >= len(fn_args): # skip self, *args
|
||||
@ -140,7 +146,7 @@ def updateable(fn):
|
||||
arg_name = fn_args[i]
|
||||
kwargs[arg_name] = arg
|
||||
self._constructor_args.append(kwargs)
|
||||
if in_event_listener():
|
||||
if in_event_listener() and initialized_before:
|
||||
return None
|
||||
else:
|
||||
return fn(self, **kwargs)
|
||||
|
@ -1575,3 +1575,19 @@ def test_postprocess_update_dict():
|
||||
("New Country B", "New Country B"),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def test_async_iterator_update_with_new_component(connect):
|
||||
async def get_number_stream():
|
||||
for i in range(10):
|
||||
yield gr.Number(value=i, label="Number (updates every second)")
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
demo = gr.Interface(fn=get_number_stream, inputs=None, outputs=["number"])
|
||||
demo.queue()
|
||||
|
||||
with connect(demo) as client:
|
||||
job = client.submit(api_name="/predict")
|
||||
job.result()
|
||||
assert [r["value"] for r in job.outputs()] == list(range(10))
|
||||
|
Loading…
Reference in New Issue
Block a user