WoWoWoWololo commited on
Commit
c5a8a47
1 Parent(s): 8ff4448

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +195 -0
app.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from gradio.blocks import Block, Blocks
4
+ from typing import Dict
5
+ from fastapi import FastAPI
6
+
7
+
8
+ class LayoutBase:
9
+ main_layout: Block
10
+ name: str
11
+ global_children_dict: Dict[str, Block]
12
+ renderables: list
13
+
14
+ def __init__(self) -> None:
15
+ self.main_layout = None
16
+ self.name = "Layout Base"
17
+ self.global_children_dict = {}
18
+ self.renderables = []
19
+
20
+ def add_component(self, name: str, component: Block) -> None:
21
+ self.renderables.append(component)
22
+ self.global_children_dict[name] = component
23
+
24
+ def add_layout(self, layout: LayoutBase) -> None:
25
+ self.renderables.append(layout)
26
+ self.global_children_dict.update(layout.global_children_dict)
27
+
28
+ def render(self) -> None:
29
+ with self.main_layout:
30
+ for renderable in self.renderables:
31
+ renderable.render()
32
+
33
+ self.main_layout.render()
34
+
35
+ def clear(self) -> None:
36
+ self.global_children_dict.clear()
37
+
38
+ for renderable in self.renderables:
39
+ if isinstance(renderable, LayoutBase):
40
+ renderable.clear()
41
+
42
+ self.renderables.clear()
43
+
44
+ def attach_event(self, block_dict: Dict[str, Block]) -> None:
45
+ raise NotImplementedError
46
+
47
+
48
+ class Application:
49
+ app: Blocks
50
+ children: list[LayoutBase]
51
+
52
+ # Blocks constructor parameters are omitted for brevity
53
+ def __init__(self, title: str) -> None:
54
+ self.app = Blocks(title=title)
55
+ self.children = []
56
+
57
+ def add(self, child: LayoutBase):
58
+ self.children.append(child)
59
+
60
+ def _render(self):
61
+ with self.app:
62
+ for child in self.children:
63
+ child.render()
64
+
65
+ self.app.render()
66
+
67
+ def _attach_event(self):
68
+ block_dict: Dict[str, Block] = {}
69
+
70
+ for child in self.children:
71
+ block_dict.update(child.global_children_dict)
72
+
73
+ with self.app:
74
+ for child in self.children:
75
+ try:
76
+ child.attach_event(block_dict=block_dict)
77
+ except NotImplementedError:
78
+ print(f"{child.name}'s attach_event is not implemented")
79
+
80
+ def _clear(self):
81
+ from gc import collect
82
+
83
+ for child in self.children:
84
+ child.clear()
85
+
86
+ self.children.clear()
87
+
88
+ collect()
89
+
90
+ # launch function parameters are omitted for the brevity
91
+ def launch(self) -> tuple[FastAPI, str, str]:
92
+ self._render()
93
+ self._attach_event()
94
+ self._clear()
95
+
96
+ return self.app.launch()
97
+
98
+
99
+ from gradio import Row, Column, Tab, Textbox
100
+
101
+
102
+ class RowLayout(LayoutBase):
103
+ def __init__(self, name: str) -> None:
104
+ super().__init__()
105
+
106
+ self.main_layout = Row()
107
+
108
+ self.global_children_dict[name] = self.main_layout
109
+
110
+
111
+ class ColumnLayout(LayoutBase):
112
+ def __init__(self, name: str) -> None:
113
+ super().__init__()
114
+
115
+ self.main_layout = Column()
116
+
117
+ self.global_children_dict[name] = self.main_layout
118
+
119
+
120
+ class TabLayout(LayoutBase):
121
+ def __init__(self, name: str) -> None:
122
+ super().__init__()
123
+
124
+ self.main_layout = Tab(label=name)
125
+
126
+ self.global_children_dict[name] = self.main_layout
127
+
128
+
129
+ def change_text(new_str: str):
130
+ return Textbox(value=new_str)
131
+
132
+
133
+ class RowExample(RowLayout):
134
+ def __init__(self, name: str) -> None:
135
+ super().__init__(name=name)
136
+
137
+ self.left_textbox = Textbox(
138
+ value="Left Textbox", interactive=True, render=False
139
+ )
140
+ self.right_textbox = Textbox(value="Right Textbox", render=False)
141
+
142
+ self.add_component("left_textbox", self.left_textbox)
143
+ self.add_component("right_textbox", self.right_textbox)
144
+
145
+ def attach_event(self, block_dict: Dict[str, Block]) -> None:
146
+ self.left_textbox.change(
147
+ change_text,
148
+ inputs=self.left_textbox,
149
+ outputs=self.right_textbox,
150
+ )
151
+
152
+
153
+ class FirstTab(TabLayout):
154
+ def __init__(self, name: str) -> None:
155
+ super().__init__(name)
156
+
157
+ self.row = RowExample(name="first tab row layout")
158
+
159
+ self.add_layout(self.row)
160
+
161
+ def attach_event(self, block_dict: Dict[str, Block]) -> None:
162
+ self.row.attach_event(block_dict)
163
+
164
+
165
+ class SecondTab(TabLayout):
166
+ def __init__(self, name: str) -> None:
167
+ super().__init__(name)
168
+
169
+ self.column = ColumnLayout(name="second tab column layout")
170
+
171
+ self.top_textbox = Textbox(value="Top Textbox", interactive=True, render=False)
172
+ self.bottom_textbox = Textbox(value="Bottom Textbox", render=False)
173
+
174
+ self.column.add_component("top_textbox", self.top_textbox)
175
+ self.column.add_component("bottom_textbox", self.bottom_textbox)
176
+
177
+ self.add_layout(self.column)
178
+
179
+ def attach_event(self, block_dict: Dict[str, Block]) -> None:
180
+ block_dict["left_textbox"].change(
181
+ change_text,
182
+ inputs=block_dict["left_textbox"],
183
+ outputs=self.bottom_textbox,
184
+ )
185
+
186
+
187
+ gui = Application(title="Wrap Gradio")
188
+
189
+ first_tab = FirstTab(name="First Tab")
190
+ second_tab = SecondTab(name="Second Tab")
191
+
192
+ gui.add(first_tab)
193
+ gui.add(second_tab)
194
+
195
+ gui.launch()