File size: 10,243 Bytes
a03b3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import gradio as gr
import time
from os.path import abspath, join, pardir

KS_FILES = abspath(join(__file__, pardir, pardir, "kitchen_sink", "files"))

base_theme = gr.themes.Base()
default_theme = gr.themes.Default()
monochrome_theme = gr.themes.Monochrome()
soft_theme = gr.themes.Soft()
glass_theme = gr.themes.Glass()

with gr.Blocks(theme=base_theme) as demo:
    gr.Markdown(
        """
    # Blocks Kitchen Sink
    This is a demo of most Gradio features. Test all themes and toggle dark mode
    ## Elements
    - Use of Rows, Columns, Tabs, and Accordion
    - Use of Form elements: Textbox, Dropdown, Checkbox, Radio, Slider
    ## Other
    Other stuff
    - Buttons of variants: "primary", "secondary", "stop"
    - Embedded interface
    - Custom progress bar
    """
    )
    toggle_dark = gr.Button("Toggle Dark", scale=0)
    toggle_dark.click(
        None,
        js="""
        () => { 
            document.body.classList.toggle('dark');
        }
        """,
    )
    theme_selector = gr.Radio(
        ["Base", "Default", "Monochrome", "Soft", "Glass"],
        value="Base",
        label="Theme",
    )
    theme_selector.change(
        None,
        theme_selector,
        None,
        js=f"""
        (theme) => {{
            if (!document.querySelector('.theme-css')) {{
                var theme_elem = document.createElement('style');
                theme_elem.classList.add('theme-css');
                document.head.appendChild(theme_elem);

                var link_elem = document.createElement('link');
                link_elem.classList.add('link-css');
                link_elem.rel = 'stylesheet';
                document.head.appendChild(link_elem);
            }} else {{
                var theme_elem = document.querySelector('.theme-css');
                var link_elem = document.querySelector('.link-css');
            }}
            if (theme == "Base") {{
                var theme_css = `{base_theme._get_theme_css()}`;
                var link_css = `{base_theme._stylesheets[0]}`;
            }} else if (theme == "Default") {{
                var theme_css = `{default_theme._get_theme_css()}`;
                var link_css = `{default_theme._stylesheets[0]}`;
            }} else if (theme == "Monochrome") {{
                var theme_css = `{monochrome_theme._get_theme_css()}`;
                var link_css = `{monochrome_theme._stylesheets[0]}`;
            }} else if (theme == "Soft") {{
                var theme_css = `{soft_theme._get_theme_css()}`;
                var link_css = `{soft_theme._stylesheets[0]}`;
            }} else if (theme == "Glass") {{
                var theme_css = `{glass_theme._get_theme_css()}`;
                var link_css = `{glass_theme._stylesheets[0]}`;
            }}
            theme_elem.innerHTML = theme_css;
            link_elem.href = link_css;
        }}
    """,
    )

    name = gr.Textbox(
        label="Name (select)",
        info="Full name, including middle name. No special characters.",
        placeholder="John Doe",
        value="John Doe",
        interactive=True,
    )

    with gr.Row():
        slider1 = gr.Slider(label="Slider 1")
        slider2 = gr.Slider(label="Slider 2")
    checkboxes = gr.CheckboxGroup(["A", "B", "C"], label="Checkbox Group (select)")

    with gr.Row():
        with gr.Column(variant="panel", scale=1):
            gr.Markdown("## Panel 1")
            radio = gr.Radio(
                ["A", "B", "C"],
                label="Radio (select)",
                info="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
            )
            drop = gr.Dropdown(["Option 1", "Option 2", "Option 3"], show_label=False)
            drop_2 = gr.Dropdown(
                ["Option A", "Option B", "Option C"],
                multiselect=True,
                value=["Option A"],
                label="Dropdown (select)",
                interactive=True,
            )
            check = gr.Checkbox(label="Go")
        with gr.Column(variant="panel", scale=2):
            img = gr.Image(
                "https://picsum.photos/536/354",
                label="Image",
                height=320,
            )
            with gr.Row():
                go_btn = gr.Button("Go", label="Primary Button", variant="primary")
                clear_btn = gr.Button(
                    "Clear", label="Secondary Button", variant="secondary"
                )

                def go(*args):
                    time.sleep(3)
                    return "https://i.ibb.co/6BgKdSj/groot.jpg"

                go_btn.click(go, [radio, drop, drop_2, check, name], img, api_name="go")

                def clear():
                    time.sleep(0.2)
                    return None

                clear_btn.click(clear, None, img)

            with gr.Row():
                btn1 = gr.Button("Button 1", size="sm")
                btn2 = gr.UploadButton(size="sm")
                stop_btn = gr.Button(
                    "Stop", label="Stop Button", variant="stop", size="sm"
                )

            gr.Examples(
                examples=[join(KS_FILES, "lion.jpg"), join(KS_FILES, "tower.jpg")],
                inputs=img,
            )

    gr.Examples(
        examples=[
            ["A", "Option 1", ["Option B"], True, join(KS_FILES, "lion.jpg")],
            [
                "B",
                "Option 2",
                ["Option B", "Option C"],
                False,
                join(KS_FILES, "tower.jpg"),
            ],
        ],
        inputs=[radio, drop, drop_2, check, img],
        label="Examples (select)",
    )

    gr.Markdown("## Media Files")

    with gr.Tabs() as tabs:
        with gr.Tab("Audio"):
            with gr.Row():
                gr.Audio()
                gr.Audio(sources=["microphone"])
                gr.Audio(join(KS_FILES, "cantina.wav"))
        with gr.Tab("Other"):
            # gr.Image(source="webcam")
            gr.HTML(
                "<div style='width: 100px; height: 100px; background-color: blue;'></div>"
            )
    with gr.Row():
        dataframe = gr.Dataframe(
            value=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], label="Dataframe (select)"
        )
        gr.JSON(
            value={"a": 1, "b": 2, "c": {"test": "a", "test2": [1, 2, 3]}}, label="JSON"
        )
        label = gr.Label(
            value={"cat": 0.7, "dog": 0.2, "fish": 0.1}, label="Label (select)"
        )
        file = gr.File(label="File (select)")
    with gr.Row():
        gr.ColorPicker()
        gr.Video(join(KS_FILES, "world.mp4"))
        gallery = gr.Gallery(
            [
                (join(KS_FILES, "lion.jpg"), "lion"),
                (join(KS_FILES, "logo.png"), "logo"),
                (join(KS_FILES, "tower.jpg"), "tower"),
            ],
            label="Gallery (select)",
        )

    with gr.Row():
        with gr.Column(scale=2):
            highlight = gr.HighlightedText(
                [["The", "art"], ["dog", "noun"], ["is", None], ["fat", "adj"]],
                label="Highlighted Text (select)",
            )
            chatbot = gr.Chatbot([["Hello", "Hi"]], label="Chatbot (select)")
            chat_btn = gr.Button("Add messages")

            def chat(history):
                time.sleep(2)
                yield [["How are you?", "I am good."]]
                time

            chat_btn.click(
                lambda history: history
                + [["How are you?", "I am good."]]
                + (time.sleep(2) or []),
                chatbot,
                chatbot,
            )
        with gr.Column(scale=1):
            with gr.Accordion("Select Info"):
                gr.Markdown(
                    "Click on any part of any component with '(select)' in the label and see the SelectData data here."
                )
                select_index = gr.Textbox(label="Index")
                select_value = gr.Textbox(label="Value")
                select_selected = gr.Textbox(label="Selected")

                selectables = [
                    name,
                    checkboxes,
                    radio,
                    drop_2,
                    dataframe,
                    label,
                    file,
                    highlight,
                    chatbot,
                    gallery,
                    tabs,
                ]

                def select_data(evt: gr.SelectData):
                    return [
                        evt.index,
                        evt.value,
                        evt.selected,
                    ]

                for selectable in selectables:
                    selectable.select(
                        select_data,
                        None,
                        [select_index, select_value, select_selected],
                    )

    gr.Markdown("## Dataset Examples")

    component_example_set = [
        (gr.Audio(render=False), join(KS_FILES, "cantina.wav")),
        (gr.Checkbox(render=False), True),
        (gr.CheckboxGroup(render=False, choices=["A", "B"]), ["A", "B"]),
        (gr.ColorPicker(render=False), "#FF0000"),
        (gr.Dataframe(render=False), [[1, 2, 3], [4, 5, 6]]),
        (gr.Dropdown(render=False), "A"),
        (gr.File(render=False), join(KS_FILES, "lion.jpg")),
        (gr.HTML(render=False), "<div>Test</div>"),
        (gr.Image(render=False), join(KS_FILES, "lion.jpg")),
        (gr.Markdown(render=False), "# Test"),
        (gr.Number(render=False), 1),
        (gr.Radio(render=False), "A"),
        (gr.Slider(render=False), 1),
        (gr.Textbox(render=False), "A"),
        (gr.Video(render=False), join(KS_FILES, "world.mp4")),
    ]
    gr.Dataset(
        components=[c for c, _ in component_example_set],
        samples=[[e for _, e in component_example_set]],
    )

    with gr.Tabs():
        for c, e in component_example_set:
            with gr.Tab(c.__class__.__name__):
                gr.Dataset(components=[c], samples=[[e]] * 3)


if __name__ == "__main__":
    demo.launch(allowed_paths=[KS_FILES])