-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Load example from external Spaces #2594
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
Changes from 12 commits
3ce7264
0df3356
307e179
7668675
2ad6948
90250b8
214f82d
e09960d
ff5a60f
631dfdf
073c6f4
0757da8
07c9200
d2418af
9bea4ad
15797f5
39d35c6
3dfb56b
ee31eb3
a048653
4015942
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,11 +66,20 @@ | |
|
|
||
|
|
||
| class Block: | ||
| def __init__(self, *, render=True, elem_id=None, visible=True, **kwargs): | ||
| def __init__( | ||
| self, | ||
| *, | ||
| render: bool = True, | ||
| elem_id: str | None = None, | ||
| visible: bool = True, | ||
| root_url: str | None = None, # URL that is prepended to all file paths | ||
| **kwargs, | ||
| ): | ||
| self._id = Context.id | ||
| Context.id += 1 | ||
| self.visible = visible | ||
| self.elem_id = elem_id | ||
| self.root_url = root_url | ||
| self._style = {} | ||
| if render: | ||
| self.render() | ||
|
|
@@ -246,6 +255,7 @@ def get_config(self): | |
| "visible": self.visible, | ||
| "elem_id": self.elem_id, | ||
| "style": self._style, | ||
| "root_url": self.root_url, | ||
| } | ||
|
|
||
| @classmethod | ||
|
|
@@ -570,8 +580,17 @@ def share(self, value: Optional[bool]): | |
| self._share = value | ||
|
|
||
| @classmethod | ||
| def from_config(cls, config: dict, fns: List[Callable]) -> Blocks: | ||
| """Factory method that creates a Blocks from a config and list of functions.""" | ||
| def from_config( | ||
| cls, config: dict, fns: List[Callable], root_url: str | None = None | ||
| ) -> Blocks: | ||
| """ | ||
| Factory method that creates a Blocks from a config and list of functions. | ||
|
|
||
| Parameters: | ||
| config: a dictionary containing the configuration of the Blocks. | ||
| fns: a list of functions that are used in the Blocks. Must be in the same order as the dependencies in the config. | ||
| root_url: an optional root url to use for the components in the Blocks. Allows serving files from an external URL. | ||
| """ | ||
| config = copy.deepcopy(config) | ||
| components_config = config["components"] | ||
| original_mapping: Dict[int, Block] = {} | ||
|
|
@@ -586,6 +605,8 @@ def get_block_instance(id: int) -> Block: | |
| block_config["props"].pop("type", None) | ||
| block_config["props"].pop("name", None) | ||
| style = block_config["props"].pop("style", None) | ||
| if block_config["props"].get("root_url") is None: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the Space is already loading from another Space, it should use the root_url of the parent Space. This allows loading a chain of Spaces |
||
| block_config["props"]["root_url"] = root_url | ||
| block = cls(**block_config["props"]) | ||
| if style: | ||
| block.style(**style) | ||
|
|
@@ -603,6 +624,9 @@ def iterate_over_children(children_list): | |
| iterate_over_children(children) | ||
|
|
||
| with Blocks(theme=config["theme"], css=config["theme"]) as blocks: | ||
| # ID 0 should be the root Blocks component | ||
| original_mapping[0] = Context.root_block or blocks | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixing a different bug that was preventing |
||
|
|
||
| iterate_over_children(config["layout"]["children"]) | ||
|
|
||
| # add the event triggers | ||
|
|
@@ -618,13 +642,11 @@ def iterate_over_children(children_list): | |
| original_mapping[o] for o in dependency["outputs"] | ||
| ] | ||
| dependency.pop("status_tracker", None) | ||
| dependency["_js"] = dependency.pop("js", None) | ||
| dependency["preprocess"] = False | ||
| dependency["postprocess"] = False | ||
|
|
||
| for target in targets: | ||
| event_method = getattr(original_mapping[target], trigger) | ||
| event_method(fn=fn, **dependency) | ||
| original_mapping[target].set_event_trigger(event_name=trigger, fn=fn, **dependency) | ||
|
|
||
| # Allows some use of Interface-specific methods with loaded Spaces | ||
| blocks.predict = [fns[0]] | ||
|
|
@@ -1073,7 +1095,7 @@ def load( | |
| fn: Instance Method - Callable function | ||
| inputs: Instance Method - input list | ||
| outputs: Instance Method - output list | ||
| every: Run this event 'every' number of seconds. Interpreted in seconds. Queue must be enabled. | ||
| every: Instance Method - Run this event 'every' number of seconds. Interpreted in seconds. Queue must be enabled. | ||
| Example: | ||
| import gradio as gr | ||
| import datetime | ||
|
|
@@ -1088,14 +1110,8 @@ def get_time(): | |
| if isinstance(self_or_cls, type): | ||
| if name is None: | ||
| raise ValueError( | ||
| "Blocks.load() requires passing `name` as a keyword argument" | ||
| "Blocks.load() requires passing parameters as keyword arguments" | ||
| ) | ||
| if fn is not None: | ||
| kwargs["fn"] = fn | ||
| if inputs is not None: | ||
| kwargs["inputs"] = inputs | ||
| if outputs is not None: | ||
| kwargs["outputs"] = outputs | ||
| return external.load_blocks_from_repo(name, src, api_key, alias, **kwargs) | ||
| else: | ||
| return self_or_cls.set_event_trigger( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.