Skip to content

Commit

Permalink
Clean root url (#6159)
Browse files Browse the repository at this point in the history
* clean

* cleanup

* formatting

* add changeset

* fe

* add changeset

* selectable

* block

* fix

* fixes

* fix update

* gradio/events

* configs

* remove

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot authored Oct 31, 2023
1 parent 040ec31 commit 44ead3b
Show file tree
Hide file tree
Showing 73 changed files with 1,986 additions and 1,817 deletions.
14 changes: 14 additions & 0 deletions .changeset/grumpy-buttons-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@gradio/annotatedimage": minor
"@gradio/audio": minor
"@gradio/button": minor
"@gradio/chatbot": minor
"@gradio/dataset": minor
"@gradio/file": minor
"@gradio/gallery": minor
"@gradio/model3d": minor
"@gradio/video": minor
"gradio": minor
---

feat:Clean root url
2 changes: 1 addition & 1 deletion demo/tictactoe/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: tictactoe"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", " turn = gr.Textbox(\"X\", interactive=False, label=\"Turn\")\n", " board = gr.Dataframe(value=[[\"\", \"\", \"\"]] * 3, interactive=False, type=\"array\")\n", "\n", " def place(board, turn, evt: gr.SelectData):\n", " if evt.value:\n", " return board, turn\n", " board[evt.index[0]][evt.index[1]] = turn\n", " turn = \"O\" if turn == \"X\" else \"X\"\n", " return board, turn\n", "\n", " board.select(place, [board, turn], [board, turn])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: tictactoe"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", " turn = gr.Textbox(\"X\", interactive=False, label=\"Turn\")\n", " board = gr.Dataframe(value=[[\"\", \"\", \"\"]] * 3, interactive=False, type=\"array\")\n", "\n", " def place(board, turn, evt: gr.SelectData):\n", " if evt.value:\n", " return board, turn\n", " board[evt.index[0]][evt.index[1]] = turn\n", " turn = \"O\" if turn == \"X\" else \"X\"\n", " return board, turn\n", "\n", " board.select(place, [board, turn], [board, turn], show_progress=\"hidden\")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
2 changes: 1 addition & 1 deletion demo/tictactoe/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def place(board, turn, evt: gr.SelectData):
turn = "O" if turn == "X" else "X"
return board, turn

board.select(place, [board, turn], [board, turn])
board.select(place, [board, turn], [board, turn], show_progress="hidden")

if __name__ == "__main__":
demo.launch()
6 changes: 0 additions & 6 deletions gradio/_simple_templates/simpledropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
):
"""
Parameters:
Expand All @@ -52,8 +50,6 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
"""
self.choices = (
# Although we expect choices to be a list of lists, it can be a list of tuples if the Gradio app
Expand All @@ -75,8 +71,6 @@ def __init__(
elem_classes=elem_classes,
value=value,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
)

def api_info(self) -> dict[str, Any]:
Expand Down
5 changes: 0 additions & 5 deletions gradio/_simple_templates/simpletextbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
):
"""
Parameters:
Expand All @@ -54,7 +52,6 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
root_url: The remote URL that of the Gradio app that this component belongs to. Used in `gr.load()`. Should not be set manually.
"""
self.placeholder = placeholder
self.rtl = rtl
Expand All @@ -70,8 +67,6 @@ def __init__(
elem_classes=elem_classes,
value=value,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
)

def preprocess(self, x: str | None) -> str | None:
Expand Down
47 changes: 25 additions & 22 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
visible: bool = True,
proxy_url: str | None = None,
_skip_init_processing: bool = False,
):
self._id = Context.id
Context.id += 1
Expand All @@ -113,7 +113,7 @@ def __init__(
self.elem_classes = (
[elem_classes] if isinstance(elem_classes, str) else elem_classes
)
self.root_url = root_url
self.proxy_url = proxy_url
self.share_token = secrets.token_urlsafe(32)
self._skip_init_processing = _skip_init_processing
self.parent: BlockContext | None = None
Expand Down Expand Up @@ -207,9 +207,11 @@ def get_config(self):
to_add = e.config_data()
if to_add:
config = {**to_add, **config}
config.pop("_skip_init_processing", None)
config.pop("render", None)
return {**config, "root_url": self.root_url, "name": self.get_block_name()}
config = {**config, "proxy_url": self.proxy_url, "name": self.get_block_name()}
if (_selectable := getattr(self, "_selectable", None)) is not None:
config["_selectable"] = _selectable
return config

@classmethod
def recover_kwargs(
Expand All @@ -234,16 +236,13 @@ def __init__(
elem_classes: list[str] | str | None = None,
visible: bool = True,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
):
"""
Parameters:
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional string or list of strings that are assigned as the class of this component in the HTML DOM. Can be used for targeting CSS styles.
visible: If False, this will be hidden but included in the Blocks config file (its visibility can later be updated).
render: If False, this will not be included in the Blocks config file at all.
root_url: The remote URL that of the Gradio app that this layout belongs to. Used in `gr.load()`. Should not be set manually.
"""
self.children: list[Block] = []
Block.__init__(
Expand All @@ -252,8 +251,6 @@ def __init__(
elem_classes=elem_classes,
visible=visible,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
)

TEMPLATE_DIR = "./templates/"
Expand Down Expand Up @@ -557,7 +554,7 @@ def __init__(
self.allowed_paths = []
self.blocked_paths = []
self.root_path = os.environ.get("GRADIO_ROOT_PATH", "")
self.root_urls = set()
self.proxy_urls = set()

if self.analytics_enabled:
is_custom_theme = not any(
Expand Down Expand Up @@ -591,7 +588,7 @@ def from_config(
cls,
config: dict,
fns: list[Callable],
root_url: str,
proxy_url: str,
) -> Blocks:
"""
Factory method that creates a Blocks from a config and list of functions. Used
Expand All @@ -600,7 +597,7 @@ def from_config(
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 external url to use as a root URL when serving files for components in the Blocks.
proxy_url: an external url to use as a root URL when serving files for components in the Blocks.
"""
config = copy.deepcopy(config)
components_config = config["components"]
Expand All @@ -611,7 +608,7 @@ def from_config(
del component_config["props"]["style"]
theme = config.get("theme", "default")
original_mapping: dict[int, Block] = {}
root_urls = {root_url}
proxy_urls = {proxy_url}

def get_block_instance(id: int) -> Block:
for block_config in components_config:
Expand All @@ -624,15 +621,22 @@ def get_block_instance(id: int) -> Block:
block_config["props"] = cls.recover_kwargs(block_config["props"])

# If a Gradio app B is loaded into a Gradio app A, and B itself loads a
# Gradio app C, then the root_urls of the components in A need to be the
# Gradio app C, then the proxy_urls of the components in A need to be the
# URL of C, not B. The else clause below handles this case.
if block_config["props"].get("root_url") is None:
block_config["props"]["root_url"] = f"{root_url}/"
if block_config["props"].get("proxy_url") is None:
block_proxy_url = f"{proxy_url}/"
else:
root_urls.add(block_config["props"]["root_url"])
block_proxy_url = block_config["props"]["proxy_url"]
proxy_urls.add(block_proxy_url)

_selectable = block_config["props"].pop("_selectable", None)
block = cls(**block_config["props"])
# Any component has already processed its initial value, so we skip that step here
block = cls(**block_config["props"], _skip_init_processing=True)
block._skip_init_processing = True
block.proxy_url = block_proxy_url
if _selectable is not None:
block._selectable = _selectable # type: ignore

return block

def iterate_over_children(children_list):
Expand Down Expand Up @@ -720,7 +724,7 @@ def iterate_over_children(children_list):
]
blocks.__name__ = "Interface"
blocks.api_mode = True
blocks.root_urls = root_urls
blocks.proxy_urls = proxy_urls
return blocks

def __str__(self):
Expand Down Expand Up @@ -953,7 +957,7 @@ def render(self):
Context.root_block.fns[dependency_offset + i] = new_fn
Context.root_block.dependencies.append(dependency)
Context.root_block.temp_file_sets.extend(self.temp_file_sets)
Context.root_block.root_urls.update(self.root_urls)
Context.root_block.proxy_urls.update(self.proxy_urls)

if Context.block is not None:
Context.block.children.extend(self.children)
Expand Down Expand Up @@ -1344,7 +1348,6 @@ def postprocess_data(
args.pop("value", None)
args.pop("__type__")
args["render"] = False
args["_skip_init_processing"] = not block_fn.postprocess
state[output_id] = self.blocks[output_id].__class__(**args)

prediction_value = postprocess_update_dict(
Expand Down
7 changes: 0 additions & 7 deletions gradio/components/annotated_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_selectable: bool = False,
_skip_init_processing: bool = False,
):
"""
Parameters:
Expand All @@ -85,13 +82,11 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
root_url: The remote URL that of the Gradio app that this component belongs to. Used in `gr.load()`. Should not be set manually.
"""
self.show_legend = show_legend
self.height = height
self.width = width
self.color_map = color_map
self._selectable = _selectable
super().__init__(
label=label,
every=every,
Expand All @@ -103,8 +98,6 @@ def __init__(
elem_id=elem_id,
elem_classes=elem_classes,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
value=value,
)

Expand Down
5 changes: 0 additions & 5 deletions gradio/components/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
format: Literal["wav", "mp3"] = "wav",
autoplay: bool = False,
show_download_button=True,
Expand All @@ -101,7 +99,6 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
root_url: The remote URL that of the Gradio app that this component belongs to. Used in `gr.load()`. Should not be set manually.
format: The file format to save audio files. Either 'wav' or 'mp3'. wav files are lossless but will tend to be larger files. mp3 files tend to be smaller. Default is wav. Applies both when this component is used as an input (when `type` is "format") and when this component is used as an output.
autoplay: Whether to automatically play the audio when the component is used as an output. Note: browsers will not autoplay audio files if the user has not interacted with the page yet.
show_download_button: If True, will show a download button in the corner of the component for saving audio. If False, icon does not appear.
Expand Down Expand Up @@ -158,8 +155,6 @@ def __init__(
elem_id=elem_id,
elem_classes=elem_classes,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
value=value,
)

Expand Down
5 changes: 0 additions & 5 deletions gradio/components/bar_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
sort: Literal["x", "y", "-x", "-y"] | None = None,
show_actions_button: bool = False,
):
Expand Down Expand Up @@ -104,7 +102,6 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
root_url: The remote URL that of the Gradio app that this component belongs to. Used in `gr.load()`. Should not be set manually.
sort: Specifies the sorting axis as either "x", "y", "-x" or "-y". If None, no sorting is applied.
show_actions_button: Whether to show the actions button on the top right corner of the plot.
"""
Expand Down Expand Up @@ -141,8 +138,6 @@ def __init__(
elem_id=elem_id,
elem_classes=elem_classes,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
every=every,
)

Expand Down
4 changes: 0 additions & 4 deletions gradio/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
load_fn: Callable | None = None,
every: float | None = None,
):
Expand Down Expand Up @@ -179,8 +177,6 @@ def __init__(
elem_classes=elem_classes,
visible=visible,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
)
if isinstance(self, StreamingInput):
self.check_streamable()
Expand Down
5 changes: 0 additions & 5 deletions gradio/components/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def __init__(
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
root_url: str | None = None,
_skip_init_processing: bool = False,
scale: int | None = None,
min_width: int | None = None,
):
Expand All @@ -56,7 +54,6 @@ def __init__(
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
root_url: The remote URL that of the Gradio app that this component belongs to. Used in `gr.load()`. Should not be set manually.
scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
"""
Expand All @@ -66,8 +63,6 @@ def __init__(
elem_id=elem_id,
elem_classes=elem_classes,
render=render,
root_url=root_url,
_skip_init_processing=_skip_init_processing,
value=value,
interactive=interactive,
scale=scale,
Expand Down
Loading

0 comments on commit 44ead3b

Please sign in to comment.