ysharma HF staff commited on
Commit
1aa9fbd
·
verified ·
1 Parent(s): a985bb5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -0
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )
75
+
76
+ button2.click(
77
+ fn=transform_step2,
78
+ inputs=None,
79
+ outputs=[text_box, row1, text_output],
80
+ )
81
+
82
+ button3.click(
83
+ fn=transform_step3,
84
+ inputs=None,
85
+ outputs=[row1, row2],
86
+ )
87
+
88
+ button4.click(
89
+ fn=reset_ui,
90
+ inputs=None,
91
+ outputs=[text_box, row1, row2, image1, text_output, slider, number],
92
+ )
93
+
94
+ return demo
95
+
96
+ # Example 2: Accordion Madness - Components that expand and collapse unpredictably
97
+ def create_accordion_demo():
98
+ with gr.Blocks() as demo:
99
+ gr.Markdown("# Accordion Madness")
100
+ gr.Markdown("Each button click triggers component visibility changes WITHOUT server round trips")
101
+
102
+ # Initial setup with various accordions
103
+ with gr.Accordion("Section 1", open=True) as acc1:
104
+ text1 = gr.Textbox(value="Content in section 1")
105
+
106
+ with gr.Accordion("Section 2", open=False) as acc2:
107
+ text2 = gr.Textbox(value="Content in section 2")
108
+
109
+ with gr.Accordion("Section 3", open=False) as acc3:
110
+ text3 = gr.Textbox(value="Content in section 3")
111
+
112
+ with gr.Accordion("Section 4", open=False) as acc4:
113
+ text4 = gr.Textbox(value="Content in section 4")
114
+
115
+ with gr.Row():
116
+ #shuffle_btn = gr.Button("Shuffle Sections")
117
+ #cascade_btn = gr.Button("Cascade Open")
118
+ collapse_btn = gr.Button("Collapse All")
119
+ expand_btn = gr.Button("Expand All")
120
+
121
+ # Handlers to collapse or expand all
122
+ def collapse_all():
123
+ #time.sleep(1)
124
+ return [
125
+ gr.Accordion(open=False),
126
+ gr.Accordion(open=False),
127
+ gr.Accordion(open=False),
128
+ gr.Accordion(open=False),
129
+ ]
130
+
131
+ def expand_all():
132
+ #time.sleep(1)
133
+ return [
134
+ gr.Accordion(open=True),
135
+ gr.Accordion(open=True),
136
+ gr.Accordion(open=True),
137
+ gr.Accordion(open=True),
138
+ ]
139
+
140
+ collapse_btn.click(
141
+ fn=collapse_all,
142
+ inputs=None,
143
+ outputs=[acc1, acc2, acc3, acc4],
144
+ )
145
+
146
+ expand_btn.click(
147
+ fn=expand_all,
148
+ inputs=None,
149
+ outputs=[acc1, acc2, acc3, acc4],
150
+ )
151
+
152
+ return demo
153
+
154
+ # Create the tabbed interface with all demos
155
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
156
+ gr.Markdown("# Gradio Server Round Trip Demos")
157
+ gr.Markdown("""
158
+ These demos showcase how Gradio now DOES NOT trigger server round trips when updating component properties.
159
+ Each button click only updates component properties WITHOUT requiring a full server round trip.
160
+ """)
161
+
162
+ with gr.Tabs():
163
+ with gr.TabItem("UI Transformation"):
164
+ transformation_demo = create_transformation_demo()
165
+
166
+ with gr.TabItem("Accordion Madness"):
167
+ accordion_demo = create_accordion_demo()
168
+
169
+
170
+ if __name__ == "__main__":
171
+ demo.launch()