Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 10 additions & 54 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,77 +19,33 @@ def __init__(self):
Context.root_block.blocks[self._id] = self
self.events = []

def click(self, fn, inputs, outputs):
if not isinstance(inputs, list):
inputs = [inputs]
if not isinstance(outputs, list):
outputs = [outputs]
Context.root_block.fns.append(fn)
Context.root_block.dependencies.append(
{
"id": len(Context.root_block.dependencies),
"targets": [self._id],
"trigger": "click",
"inputs": [block._id for block in inputs],
"outputs": [block._id for block in outputs],
}
)

def change(
self, fn: str, inputs: List["Component"], outputs: List["Component"]
def set_event_trigger(
self,
event_name: str,
fn: str,
Copy link
Copy Markdown
Member

@abidlabs abidlabs Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of fn should be Callable (here and below)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

inputs: List[Component],
outputs: List[Component],
) -> None:
"""
Adds change event to the component's dependencies.

Whenever the component changes the function is triggered.

Adds an event to the component's dependencies.
Parameters:
event_name: event name
fn: function name
inputs: input list
outputs: output list

Returns: None

"""
# Support for singular parameter
if not isinstance(inputs, list):
inputs = [inputs]
if not isinstance(outputs, list):
outputs = [outputs]
Context.root_block.fns.append(fn)
Context.root_block.dependencies.append(
{
"targets": [self._id],
"trigger": "change",
"inputs": [block._id for block in inputs],
"outputs": [block._id for block in outputs],
}
)

def save(
self, fn: str, inputs: List["Component"], outputs: List["Component"]
) -> None:
"""
Adds save event to the component's dependencies.

Whenever the component is saved the function is triggered.

Parameters:
fn: function name
inputs: input list
outputs: output list

Returns: None

"""
if not isinstance(inputs, list):
inputs = [inputs]
if not isinstance(outputs, list):
outputs = [outputs]
Context.root_block.fns.append(fn)
Context.root_block.dependencies.append(
{
"targets": [self._id],
"trigger": "save",
"trigger": event_name,
"inputs": [block._id for block in inputs],
"outputs": [block._id for block in outputs],
}
Expand Down
Loading