ysharma HF Staff commited on
Commit
50135e4
·
verified ·
1 Parent(s): c8956d3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -0
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Examples of Gradio demos that showcase server round trips when updating component properties
2
+ import gradio as gr
3
+ import time
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ from PIL import Image
7
+ import random
8
+
9
+ # Example 1: Interface Transformation - Components change their properties with each click
10
+ def create_transformation_demo():
11
+ with gr.Blocks() as demo:
12
+ gr.Markdown("# UI Transformation Demo")
13
+ gr.Markdown("Each button click transform the interface and use to cause server round trips (SLOW Updates)")
14
+
15
+ # Initial components
16
+ text_box = gr.Textbox(placeholder="I will change shape and size...", label="Morphing Textbox", lines=2)
17
+
18
+ with gr.Row(visible=True) as row1:
19
+ image1 = gr.Image(value=None, label="Appearing Image", scale=4)
20
+ text_output = gr.Textbox(value="I'll move around", label="Moving Textbox", scale=6)
21
+
22
+ with gr.Row(visible=False) as row2:
23
+ slider = gr.Slider(minimum=0, maximum=100, value=50, label="Hidden Slider")
24
+ number = gr.Number(value=42, label="Magic Number")
25
+
26
+ button1 = gr.Button("Transform UI - Step 1")
27
+ button2 = gr.Button("Transform UI - Step 2")
28
+ button3 = gr.Button("Transform UI - Step 3")
29
+ button4 = gr.Button("Reset")
30
+
31
+ # Step 1: First transformation
32
+ def transform_step1():
33
+ # Artificial delay to make the server round trip obvious
34
+ return [
35
+ gr.Textbox(label="Expanding Textbox", lines=10, placeholder="I now have a new placeholder and label"), # textbox changes
36
+ gr.Row(visible=False), # row1 hides
37
+ gr.Row(visible=True), # row2 appears
38
+ ]
39
+
40
+ # Step 2: Second transformation
41
+ def transform_step2():
42
+ return [
43
+ gr.Textbox(label="New-Textbox-Label", lines=1,
44
+ container=False,
45
+ info="Just added a new info field for the textbox",
46
+ placeholder="I changed my label, added info, removed the container, and shrinked the size"),
47
+ gr.Row(visible=True), # row1 reappears
48
+ gr.Textbox(label="Relocated Textbox",scale=1) # text_output changes
49
+ ]
50
+
51
+ # Step 3: Third transformation
52
+ def transform_step3():
53
+ return [
54
+ gr.Row(variant="panel" ), gr.Row(variant="compact" )
55
+ ]
56
+
57
+ # Reset everything
58
+ def reset_ui():
59
+ return [
60
+ gr.Textbox(label="Morphing Textbox", placeholder="I will change shape and size...",lines=2, container=True, info=None),
61
+ gr.Row(visible=True, variant ="default"),
62
+ gr.Row(visible=False, variant ="default"),
63
+ gr.Image(label="Appearing Image"),
64
+ gr.Textbox(label="Moving Text"),
65
+ gr.Slider(minimum=0, maximum=100,label="Hidden Slider"),
66
+ gr.Number(label="Magic Number")
67
+ ]
68
+
69
+ # Set up events
70
+ button1.click(
71
+ fn=transform_step1,
72
+ inputs=None,
73
+ outputs=[text_box, row1, row2],
74
+ js=True
75
+ )
76
+
77
+ button2.click(
78
+ fn=transform_step2,
79
+ inputs=None,
80
+ outputs=[text_box, row1, text_output],
81
+ js=True
82
+ )
83
+
84
+ button3.click(
85
+ fn=transform_step3,
86
+ inputs=None,
87
+ outputs=[row1, row2],
88
+ js=True
89
+ )
90
+
91
+ button4.click(
92
+ fn=reset_ui,
93
+ inputs=None,
94
+ outputs=[text_box, row1, row2, image1, text_output, slider, number],
95
+ js=True
96
+ )
97
+
98
+ return demo
99
+
100
+ # Example 2: Accordion Madness - Components that expand and collapse unpredictably
101
+ def create_accordion_demo():
102
+ with gr.Blocks() as demo:
103
+ gr.Markdown("# Accordion Madness")
104
+ gr.Markdown("Each button click triggers component visibility changes WITHOUT server round trips")
105
+
106
+ # Initial setup with various accordions
107
+ with gr.Accordion("Section 1", open=True) as acc1:
108
+ text1 = gr.Textbox(value="Content in section 1")
109
+
110
+ with gr.Accordion("Section 2", open=False) as acc2:
111
+ text2 = gr.Textbox(value="Content in section 2")
112
+
113
+ with gr.Accordion("Section 3", open=False) as acc3:
114
+ text3 = gr.Textbox(value="Content in section 3")
115
+
116
+ with gr.Accordion("Section 4", open=False) as acc4:
117
+ text4 = gr.Textbox(value="Content in section 4")
118
+
119
+ with gr.Row():
120
+ #shuffle_btn = gr.Button("Shuffle Sections")
121
+ #cascade_btn = gr.Button("Cascade Open")
122
+ collapse_btn = gr.Button("Collapse All")
123
+ expand_btn = gr.Button("Expand All")
124
+
125
+ # Handlers to collapse or expand all
126
+ def collapse_all():
127
+ return [
128
+ gr.Accordion(open=False),
129
+ gr.Accordion(open=False),
130
+ gr.Accordion(open=False),
131
+ gr.Accordion(open=False),
132
+ ]
133
+
134
+ def expand_all():
135
+ return [
136
+ gr.Accordion(open=True),
137
+ gr.Accordion(open=True),
138
+ gr.Accordion(open=True),
139
+ gr.Accordion(open=True),
140
+ ]
141
+
142
+ collapse_btn.click(
143
+ fn=collapse_all,
144
+ inputs=None,
145
+ outputs=[acc1, acc2, acc3, acc4],
146
+ js=True
147
+ )
148
+
149
+ expand_btn.click(
150
+ fn=expand_all,
151
+ inputs=None,
152
+ outputs=[acc1, acc2, acc3, acc4],
153
+ js=True
154
+ )
155
+
156
+ return demo
157
+
158
+ # Create the tabbed interface with all demos
159
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
160
+ gr.Markdown("# Gradio Server Round Trip Demos")
161
+ gr.Markdown("""
162
+ These demos showcase how Gradio now DOES NOT trigger server round trips when updating component properties.
163
+ Each button click only updates component properties WITHOUT requiring a full server round trip.
164
+ """)
165
+
166
+ with gr.Tabs():
167
+ with gr.TabItem("UI Transformation"):
168
+ transformation_demo = create_transformation_demo()
169
+
170
+ with gr.TabItem("Accordion Madness"):
171
+ accordion_demo = create_accordion_demo()
172
+
173
+
174
+ if __name__ == "__main__":
175
+ demo.launch()