Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The `api_name` parameter will take precendence over the `fn_index` parameter.
## Bug Fixes:
* Fixed bug where None could not be used for File,Model3D, and Audio examples by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2588](https://github.com/gradio-app/gradio/pull/2588)
* Fixed links in Plotly map guide + demo by [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 2578](https://github.com/gradio-app/gradio/pull/2578)
* `gr.Blocks.load()` now correctly loads example files from Spaces [@abidlabs](https://github.com/abidlabs) in [PR 2594](https://github.com/gradio-app/gradio/pull/2594)

## Documentation Changes:
No changes to highlight.
Expand Down
44 changes: 30 additions & 14 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
abidlabs marked this conversation as resolved.
**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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] = {}
Expand All @@ -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:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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
Copy link
Copy Markdown
Member Author

@abidlabs abidlabs Nov 3, 2022

Choose a reason for hiding this comment

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

Fixing a different bug that was preventing gr.Interface.load() from working if a dataset had caching enabled


iterate_over_children(config["layout"]["children"])

# add the event triggers
Expand All @@ -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]]
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
Loading