Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion demo/blocks_flipper/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def flip_image(x):
image_input = gr.Image()
image_output = gr.Image()
image_button = gr.Button("Flip")


with gr.Accordion("Open for More!"):
gr.Markdown("Look at me...")

text_button.click(flip_text, inputs=text_input, outputs=text_output)
image_button.click(flip_image, inputs=image_input, outputs=image_output)

Expand Down
1 change: 1 addition & 0 deletions demo/rows_and_columns/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
with gr.Column(scale=2, min_width=600):
img1 = gr.Image("images/cheetah.jpg")
btn = gr.Button("Go").style(full_width=True)

if __name__ == "__main__":
demo.launch()
2 changes: 1 addition & 1 deletion gradio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
)
from gradio.interface import Interface, TabbedInterface, close_all
from gradio.ipython_ext import load_ipython_extension
from gradio.layouts import Box, Column, Group, Row, Tab, TabItem, Tabs
from gradio.layouts import Accordion, Box, Column, Group, Row, Tab, TabItem, Tabs
from gradio.mix import Parallel, Series
from gradio.templates import (
Files,
Expand Down
42 changes: 42 additions & 0 deletions gradio/layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,45 @@ def style(
class Form(BlockContext):
def get_config(self):
return {"type": "form", **super().get_config()}


@document()
class Accordion(BlockContext):
"""
Accordion is a layout element which can be toggled to show/hide the contained content.
Example:
with gradio.Accordion("See Details"):
gr.Markdown("lorem ipsum")
"""

def __init__(
self, label, *, open: bool = True, elem_id: Optional[str] = None, **kwargs
):
"""
Parameters:
label: name of accordion section.
open: if True, accordion is open by default.
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.
"""
self.label = label
self.open = open
super().__init__(elem_id=elem_id, **kwargs)

def get_config(self):
return {
"type": "accordion",
"open": self.open,
"label": self.label,
**super().get_config(),
}

@staticmethod
def update(
open: Optional[bool] = None,
visible: Optional[bool] = None,
):
return {
"visible": visible,
"open": open,
"__type__": "update",
}
4 changes: 3 additions & 1 deletion guides/3)building_with_blocks/2)controlling_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $demo_rows_and_columns

See how the first column has two Textboxes arranged vertically. The second column has an Image and Button arranged vertically. Notice how the relative widths of the two columns is set by the `scale` parameter. The column with twice the `scale` value takes up twice the width.

## Tabs
## Tabs amd Accordions
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
## Tabs amd Accordions
## Tabs and Accordions


You can also create Tabs using the `with gradio.Tab('tab_name'):` clause. Any component created inside of a `with gradio.Tab('tab_name'):` context appears in that tab. Consecutive Tab clauses are grouped together so that a single tab can be selected at one time, and only the components within that Tab's context are shown.

Expand All @@ -40,6 +40,8 @@ For example:
$code_blocks_flipper
$demo_blocks_flipper

Also note the Accordion that can be opened and closed - another layout element to selectively show content.
Copy link
Copy Markdown
Member

@abidlabs abidlabs Sep 8, 2022

Choose a reason for hiding this comment

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

Suggested change
Also note the Accordion that can be opened and closed - another layout element to selectively show content.
Also note the `gradio.Accordion('label')` in this example. The Accordion is a layout that can be toggled open or closed. Like `Tabs`, it is a layout element that can selectively hide or show content. Any components that are defined inside of a `with gradio.Accordion('label'):` will be hidden or shown when the accordion's toggle icon is clicked.


## Visibility

Both Components and Layout elements have a `visible` argument that can set initially and also updated using `gr.update()`. Setting `gr.update(visible=...)` on a Column can be used to show or hide a set of Components.
Expand Down
25 changes: 25 additions & 0 deletions ui/packages/app/src/components/Accordion/Accordion.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
import { Component as Column } from "../Column";
export let label: string;
export let elem_id: string;
export let visible: string;
export let open: boolean = true;

const toggle = () => {
open = !open;
};
</script>

<div
id={elem_id}
class="p-3 border border-gray-200 dark:border-gray-700 rounded-lg flex flex-col gap-3 hover:border-gray-300 dark:hover:border-gray-600 transition"
class:hidden={!visible}
>
<div on:click={toggle} class="w-full flex justify-between cursor-pointer">
<span>{label}</span>
<span class:rotate-90={!open} class="transition">▼</span>
</div>
<Column visible={open}>
<slot />
</Column>
</div>
2 changes: 2 additions & 0 deletions ui/packages/app/src/components/Accordion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Component } from "./Accordion.svelte";
export const modes = ["static"];
1 change: 1 addition & 0 deletions ui/packages/app/src/components/directory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const component_map = {
accordion: () => import("./Accordion"),
audio: () => import("./Audio"),
box: () => import("./Box"),
button: () => import("./Button"),
Expand Down