Skip to content

Commit 1ff6270

Browse files
authored
Merge pull request #844 from gradio-app/Blocks-Backend-Events
Blocks-Backend-Events
2 parents e8e8439 + 95aaf2d commit 1ff6270

2 files changed

Lines changed: 380 additions & 57 deletions

File tree

gradio/blocks.py

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,33 @@ def __init__(self):
1919
Context.root_block.blocks[self._id] = self
2020
self.events = []
2121

22-
def click(self, fn, inputs, outputs):
23-
if not isinstance(inputs, list):
24-
inputs = [inputs]
25-
if not isinstance(outputs, list):
26-
outputs = [outputs]
27-
Context.root_block.fns.append(fn)
28-
Context.root_block.dependencies.append(
29-
{
30-
"id": len(Context.root_block.dependencies),
31-
"targets": [self._id],
32-
"trigger": "click",
33-
"inputs": [block._id for block in inputs],
34-
"outputs": [block._id for block in outputs],
35-
}
36-
)
37-
38-
def change(
39-
self, fn: str, inputs: List["Component"], outputs: List["Component"]
22+
def set_event_trigger(
23+
self,
24+
event_name: str,
25+
fn: Callable,
26+
inputs: List[Component],
27+
outputs: List[Component],
4028
) -> None:
4129
"""
42-
Adds change event to the component's dependencies.
43-
44-
Whenever the component changes the function is triggered.
45-
30+
Adds an event to the component's dependencies.
4631
Parameters:
47-
fn: function name
32+
event_name: event name
33+
fn: Callable function
4834
inputs: input list
4935
outputs: output list
50-
5136
Returns: None
52-
5337
"""
38+
# Support for singular parameter
5439
if not isinstance(inputs, list):
5540
inputs = [inputs]
5641
if not isinstance(outputs, list):
5742
outputs = [outputs]
58-
Context.root_block.fns.append(fn)
59-
Context.root_block.dependencies.append(
60-
{
61-
"targets": [self._id],
62-
"trigger": "change",
63-
"inputs": [block._id for block in inputs],
64-
"outputs": [block._id for block in outputs],
65-
}
66-
)
6743

68-
def save(
69-
self, fn: str, inputs: List["Component"], outputs: List["Component"]
70-
) -> None:
71-
"""
72-
Adds save event to the component's dependencies.
73-
74-
Whenever the component is saved the function is triggered.
75-
76-
Parameters:
77-
fn: function name
78-
inputs: input list
79-
outputs: output list
80-
81-
Returns: None
82-
83-
"""
84-
if not isinstance(inputs, list):
85-
inputs = [inputs]
86-
if not isinstance(outputs, list):
87-
outputs = [outputs]
8844
Context.root_block.fns.append(fn)
8945
Context.root_block.dependencies.append(
9046
{
9147
"targets": [self._id],
92-
"trigger": "save",
48+
"trigger": event_name,
9349
"inputs": [block._id for block in inputs],
9450
"outputs": [block._id for block in outputs],
9551
}
@@ -120,7 +76,15 @@ def get_template_context(self):
12076

12177

12278
class Tabs(BlockContext):
123-
pass
79+
def change(self, fn: Callable, inputs: List[Component], outputs: List[Component]):
80+
"""
81+
Parameters:
82+
fn: Callable function
83+
inputs: List of inputs
84+
outputs: List of outputs
85+
Returns: None
86+
"""
87+
self.set_event_trigger("change", fn, inputs, outputs)
12488

12589

12690
class TabItem(BlockContext):
@@ -131,6 +95,16 @@ def __init__(self, label):
13195
def get_template_context(self):
13296
return {"label": self.label}
13397

98+
def change(self, fn: Callable, inputs: List[Component], outputs: List[Component]):
99+
"""
100+
Parameters:
101+
fn: Callable function
102+
inputs: List of inputs
103+
outputs: List of outputs
104+
Returns: None
105+
"""
106+
self.set_event_trigger("change", fn, inputs, outputs)
107+
134108

135109
class Blocks(Launchable, BlockContext):
136110
def __init__(self, theme="default"):

0 commit comments

Comments
 (0)