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

return a Dependency instance from Blocks.load event listener #4304

Merged
merged 6 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## New Features:

No changes to highlight.
* Make `Blocks.load` behave like other event listeners (allows chaining `then` off of it) [@anentropic](https://github.com/anentropic/) in [PR 4304](https://github.com/gradio-app/gradio/pull/4304)

## Bug Fixes:

Expand Down
7 changes: 5 additions & 2 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,9 @@ def get_time():
name=name, src=src, hf_token=api_key, alias=alias, **kwargs
)
else:
return self_or_cls.set_event_trigger(
from gradio.events import Dependency

dep, dep_index = self_or_cls.set_event_trigger(
event_name="load",
fn=fn,
inputs=inputs,
Expand All @@ -1498,7 +1500,8 @@ def get_time():
max_batch_size=max_batch_size,
every=every,
no_target=True,
)[0]
)
return Dependency(self_or_cls, dep, dep_index)

def clear(self):
"""Resets the layout of the Blocks object."""
Expand Down
23 changes: 23 additions & 0 deletions test/test_events.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os

import pytest
from fastapi.testclient import TestClient

import gradio as gr

os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"


class TestEvent:
def test_clear_event(self):
Expand Down Expand Up @@ -69,6 +73,25 @@ def clear():
assert not parent.config["dependencies"][2]["trigger_only_on_success"]
assert parent.config["dependencies"][3]["trigger_only_on_success"]

def test_load_chaining(self):
calls = 0

def increment():
calls += 1
return str(calls)

with gr.Blocks() as demo:
out = gr.Textbox(label="Call counter")
demo.load(increment, inputs=None, outputs=out).then(
increment, inputs=None, outputs=out
)

assert demo.config["dependencies"][0]["trigger"] == "load"
assert demo.config["dependencies"][0]["trigger_after"] is None
assert demo.config["dependencies"][1]["trigger"] == "then"
assert demo.config["dependencies"][1]["trigger_after"] == 0
demo.launch(prevent_thread_lock=True)
anentropic marked this conversation as resolved.
Show resolved Hide resolved


class TestEventErrors:
def test_event_defined_invalid_scope(self):
Expand Down