add change and save events on backend

This commit is contained in:
Ömer Faruk Özdemir 2022-03-01 12:45:24 +03:00
parent 11dda55352
commit b6609915e0
2 changed files with 57 additions and 2 deletions

View File

@ -11,6 +11,5 @@ from gradio.interface import Interface, close_all, reset_all
from gradio.mix import Parallel, Series
from gradio.routes import get_state, set_state
from gradio.static import Button, Markdown
current_pkg_version = pkg_resources.require("gradio")[0].version
__version__ = current_pkg_version

View File

@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple
from gradio import utils
from gradio.context import Context
from gradio.launchable import Launchable
from gradio.component import Component
class Block:
def __init__(self):
@ -30,6 +30,62 @@ class Block:
}
)
def change(self, fn: str, inputs: List[Component], outputs: List[Component]) -> None:
"""
Adds change event to the dependencies.
Whenever one of the inputs changes the function is triggered.
Parameters:
fn:
inputs:
outputs:
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": "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 dependencies.
Whenever one of the inputs is saved, 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",
"inputs": [block._id for block in inputs],
"outputs": [block._id for block in outputs],
}
)
class BlockContext(Block):
def __init__(self):