Skip to content

Commit

Permalink
Allow app to fill width (#8651)
Browse files Browse the repository at this point in the history
* changes

* add changeset

* changes

* changes

---------

Co-authored-by: Ali Abid <aliabid94@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 11, 2024
1 parent 31a876d commit 1500b89
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/tangy-beds-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---

feat:Allow app to fill width
2 changes: 1 addition & 1 deletion demo/blocks_xray/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_xray"]}, {"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", "import time\n", "\n", "disease_values = [0.25, 0.5, 0.75]\n", "\n", "def xray_model(diseases, img):\n", " return [{disease: disease_values[idx] for idx,disease in enumerate(diseases)}]\n", "\n", "\n", "def ct_model(diseases, img):\n", " return [{disease: 0.1 for disease in diseases}]\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\n", " \"\"\"\n", "# Detect Disease From Scan\n", "With this model you can lorem ipsum\n", "- ipsum 1\n", "- ipsum 2\n", "\"\"\"\n", " )\n", " gr.DuplicateButton()\n", " disease = gr.CheckboxGroup(\n", " info=\"Select the diseases you want to scan for.\",\n", " choices=[\"Covid\", \"Malaria\", \"Lung Cancer\"], label=\"Disease to Scan For\"\n", " )\n", " slider = gr.Slider(0, 100)\n", "\n", " with gr.Tab(\"X-ray\") as x_tab:\n", " with gr.Row():\n", " xray_scan = gr.Image()\n", " xray_results = gr.JSON()\n", " xray_run = gr.Button(\"Run\")\n", " xray_run.click(\n", " xray_model,\n", " inputs=[disease, xray_scan],\n", " outputs=xray_results,\n", " api_name=\"xray_model\"\n", " )\n", "\n", " with gr.Tab(\"CT Scan\"):\n", " with gr.Row():\n", " ct_scan = gr.Image()\n", " ct_results = gr.JSON()\n", " ct_run = gr.Button(\"Run\")\n", " ct_run.click(\n", " ct_model,\n", " inputs=[disease, ct_scan],\n", " outputs=ct_results,\n", " api_name=\"ct_model\"\n", " )\n", "\n", " upload_btn = gr.Button(\"Upload Results\", variant=\"primary\")\n", " upload_btn.click(\n", " lambda ct, xr: None,\n", " inputs=[ct_results, xray_results],\n", " outputs=[],\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_xray"]}, {"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", "import time\n", "\n", "disease_values = [0.25, 0.5, 0.75]\n", "\n", "def xray_model(diseases, img):\n", " return [{disease: disease_values[idx] for idx,disease in enumerate(diseases)}]\n", "\n", "\n", "def ct_model(diseases, img):\n", " return [{disease: 0.1 for disease in diseases}]\n", "\n", "with gr.Blocks(fill_width=True) as demo:\n", " gr.Markdown(\n", " \"\"\"\n", "# Detect Disease From Scan\n", "With this model you can lorem ipsum\n", "- ipsum 1\n", "- ipsum 2\n", "\"\"\"\n", " )\n", " gr.DuplicateButton()\n", " disease = gr.CheckboxGroup(\n", " info=\"Select the diseases you want to scan for.\",\n", " choices=[\"Covid\", \"Malaria\", \"Lung Cancer\"], label=\"Disease to Scan For\"\n", " )\n", " slider = gr.Slider(0, 100)\n", "\n", " with gr.Tab(\"X-ray\") as x_tab:\n", " with gr.Row():\n", " xray_scan = gr.Image()\n", " xray_results = gr.JSON()\n", " xray_run = gr.Button(\"Run\")\n", " xray_run.click(\n", " xray_model,\n", " inputs=[disease, xray_scan],\n", " outputs=xray_results,\n", " api_name=\"xray_model\"\n", " )\n", "\n", " with gr.Tab(\"CT Scan\"):\n", " with gr.Row():\n", " ct_scan = gr.Image()\n", " ct_results = gr.JSON()\n", " ct_run = gr.Button(\"Run\")\n", " ct_run.click(\n", " ct_model,\n", " inputs=[disease, ct_scan],\n", " outputs=ct_results,\n", " api_name=\"ct_model\"\n", " )\n", "\n", " upload_btn = gr.Button(\"Upload Results\", variant=\"primary\")\n", " upload_btn.click(\n", " lambda ct, xr: None,\n", " inputs=[ct_results, xray_results],\n", " outputs=[],\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
2 changes: 1 addition & 1 deletion demo/blocks_xray/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def xray_model(diseases, img):
def ct_model(diseases, img):
return [{disease: 0.1 for disease in diseases}]

with gr.Blocks() as demo:
with gr.Blocks(fill_width=True) as demo:
gr.Markdown(
"""
# Detect Disease From Scan
Expand Down
4 changes: 4 additions & 0 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ def __init__(
js: str | None = None,
head: str | None = None,
fill_height: bool = False,
fill_width: bool = False,
delete_cache: tuple[int, int] | None = None,
**kwargs,
):
Expand All @@ -938,6 +939,7 @@ def __init__(
js: Custom js as a string or path to a js file. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
head: Custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, multiple scripts, stylesheets, etc. to the page.
fill_height: Whether to vertically expand top-level child components to the height of the window. If True, expansion occurs when the scale value of the child components >= 1.
fill_width: Whether to horizontally expand to fill container fully. If False, centers and constrains app to a maximum width.
delete_cache: A tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
"""
self.limiter = None
Expand Down Expand Up @@ -971,6 +973,7 @@ def __init__(
self.show_error = True
self.head = head
self.fill_height = fill_height
self.fill_width = fill_width
self.delete_cache = delete_cache
if css is not None and os.path.exists(css):
with open(css, encoding="utf-8") as css_file:
Expand Down Expand Up @@ -2013,6 +2016,7 @@ def get_config_file(self) -> BlocksConfigDict:
),
},
"fill_height": self.fill_height,
"fill_width": self.fill_width,
"theme_hash": self.theme_hash,
}
config.update(self.default_config.get_config()) # type: ignore
Expand Down
3 changes: 3 additions & 0 deletions gradio/chat_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(
fill_height: bool = True,
delete_cache: tuple[int, int] | None = None,
show_progress: Literal["full", "minimal", "hidden"] = "minimal",
fill_width: bool = False,
):
"""
Parameters:
Expand Down Expand Up @@ -115,6 +116,7 @@ def __init__(
fill_height: If True, the chat interface will expand to the height of window.
delete_cache: A tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
show_progress: whether to show progress animation while running.
fill_width: Whether to horizontally expand to fill container fully. If False, centers and constrains app to a maximum width.
"""
super().__init__(
analytics_enabled=analytics_enabled,
Expand All @@ -125,6 +127,7 @@ def __init__(
js=js,
head=head,
fill_height=fill_height,
fill_width=fill_width,
delete_cache=delete_cache,
)
self.msg_format: Literal["messages", "tuples"] = msg_format
Expand Down
1 change: 1 addition & 0 deletions gradio/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ class BlocksConfigDict(TypedDict):
protocol: Literal["ws", "sse", "sse_v1", "sse_v2", "sse_v2.1", "sse_v3"]
body_css: BodyCSS
fill_height: bool
fill_width: bool
theme_hash: str
layout: NotRequired[Layout]
dependencies: NotRequired[list[dict[str, Any]]]
Expand Down
3 changes: 3 additions & 0 deletions gradio/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __init__(
delete_cache: tuple[int, int] | None = None,
show_progress: Literal["full", "minimal", "hidden"] = "full",
example_labels: list[str] | None = None,
fill_width: bool = False,
**kwargs,
):
"""
Expand Down Expand Up @@ -170,6 +171,7 @@ def __init__(
delete_cache: A tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
show_progress: whether to show progress animation while running. Has no effect if the interface is `live`.
example_labels: A list of labels for each example. If provided, the length of this list should be the same as the number of examples, and these labels will be used in the UI instead of rendering the example values.
fill_width: Whether to horizontally expand to fill container fully. If False, centers and constrains app to a maximum width.
"""
super().__init__(
analytics_enabled=analytics_enabled,
Expand All @@ -180,6 +182,7 @@ def __init__(
js=js,
head=head,
delete_cache=delete_cache,
fill_width=fill_width,
**kwargs,
)
self.api_name: str | Literal[False] | None = api_name
Expand Down
12 changes: 7 additions & 5 deletions js/app/src/Embed.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export let wrapper: HTMLDivElement;
export let version: string;
export let initial_height: string;
export let fill_width: boolean;
export let is_embed: boolean;
export let space: string | null;
Expand All @@ -15,6 +16,7 @@
<div
bind:this={wrapper}
class:app={!display && !is_embed}
class:fill_width
class:embed-container={display}
class:with-info={info}
class="gradio-container gradio-container-{version}"
Expand Down Expand Up @@ -87,27 +89,27 @@
}
@media (--screen-sm) {
.app {
.app:not(.fill_width) {
max-width: 640px;
}
}
@media (--screen-md) {
.app {
.app:not(.fill_width) {
max-width: 768px;
}
}
@media (--screen-lg) {
.app {
.app:not(.fill_width) {
max-width: 1024px;
}
}
@media (--screen-xl) {
.app {
.app:not(.fill_width) {
max-width: 1280px;
}
}
@media (--screen-xxl) {
.app {
.app:not(.fill_width) {
max-width: 1536px;
}
}
Expand Down
2 changes: 2 additions & 0 deletions js/app/src/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
path: string;
app_id?: string;
fill_height?: boolean;
fill_width?: boolean;
theme_hash?: number;
username: string | null;
}
Expand Down Expand Up @@ -412,6 +413,7 @@
{initial_height}
{space}
loaded={loader_status === "complete"}
fill_width={config?.fill_width || false}
bind:wrapper
>
{#if (loader_status === "pending" || loader_status === "error") && !(config && config?.auth_required)}
Expand Down

0 comments on commit 1500b89

Please sign in to comment.