Spaces:
Running
Running
app.py is updated with comments
Browse files
app.py
CHANGED
@@ -4,12 +4,20 @@ 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[
|
12 |
-
|
|
|
|
|
13 |
|
14 |
def __init__(self) -> None:
|
15 |
self.main_layout = None
|
@@ -17,10 +25,20 @@ class LayoutBase:
|
|
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)
|
@@ -35,25 +53,36 @@ class LayoutBase:
|
|
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 |
|
@@ -77,6 +106,11 @@ class Application:
|
|
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 |
|
@@ -126,6 +160,12 @@ class TabLayout(LayoutBase):
|
|
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 |
|
@@ -184,6 +224,11 @@ class SecondTab(TabLayout):
|
|
184 |
)
|
185 |
|
186 |
|
|
|
|
|
|
|
|
|
|
|
187 |
gui = Application(title="Wrap Gradio")
|
188 |
|
189 |
first_tab = FirstTab(name="First Tab")
|
|
|
4 |
from typing import Dict
|
5 |
from fastapi import FastAPI
|
6 |
|
7 |
+
"""
|
8 |
+
---------------------------------------------------------------------------------------------------------
|
9 |
+
-- Base Classes
|
10 |
+
"""
|
11 |
|
12 |
+
|
13 |
+
# Base class for the RowLayout, ColumnLayout and TabLayout classes
|
14 |
class LayoutBase:
|
15 |
+
main_layout: Block # Stores the main layout from the gradio package of this class.
|
16 |
+
name: str # Name of the class. Used to differentiate from other layouts and for debug purposes.
|
17 |
+
global_children_dict: Dict[
|
18 |
+
str, Block
|
19 |
+
] # Stores the children components with given name.
|
20 |
+
renderables: list # Stores the renderable elements such as components and layouts.
|
21 |
|
22 |
def __init__(self) -> None:
|
23 |
self.main_layout = None
|
|
|
25 |
self.global_children_dict = {}
|
26 |
self.renderables = []
|
27 |
|
28 |
+
"""
|
29 |
+
components are coming from the gradio package
|
30 |
+
these components are inherited from the Block class eventually
|
31 |
+
"""
|
32 |
+
|
33 |
def add_component(self, name: str, component: Block) -> None:
|
34 |
self.renderables.append(component)
|
35 |
self.global_children_dict[name] = component
|
36 |
|
37 |
+
"""
|
38 |
+
layout has to be from RowLayout, ColumnLayout or TabLayout classes
|
39 |
+
the parent class includes all the components that children layouts have
|
40 |
+
"""
|
41 |
+
|
42 |
def add_layout(self, layout: LayoutBase) -> None:
|
43 |
self.renderables.append(layout)
|
44 |
self.global_children_dict.update(layout.global_children_dict)
|
|
|
53 |
def clear(self) -> None:
|
54 |
self.global_children_dict.clear()
|
55 |
|
56 |
+
# we can be sure that all objects are cleaned
|
57 |
for renderable in self.renderables:
|
58 |
if isinstance(renderable, LayoutBase):
|
59 |
renderable.clear()
|
60 |
|
61 |
self.renderables.clear()
|
62 |
|
63 |
+
"""
|
64 |
+
the inherited class has to implement this function.
|
65 |
+
block_dict is coming from Application class's _attach_event function or parent class's attach_event function
|
66 |
+
"""
|
67 |
+
|
68 |
def attach_event(self, block_dict: Dict[str, Block]) -> None:
|
69 |
raise NotImplementedError
|
70 |
|
71 |
|
72 |
+
# Responsible for rendering, attaching events and launching
|
73 |
class Application:
|
74 |
+
app: Blocks # Base application component from the gradio package.
|
75 |
+
children: list[LayoutBase] # Stores the layouts
|
76 |
|
77 |
# Blocks constructor parameters are omitted for brevity
|
78 |
def __init__(self, title: str) -> None:
|
79 |
self.app = Blocks(title=title)
|
80 |
self.children = []
|
81 |
|
82 |
+
"""
|
83 |
+
adding given RowLayout, ColumnLayout or TabLayout classes to children variable
|
84 |
+
"""
|
85 |
+
|
86 |
def add(self, child: LayoutBase):
|
87 |
self.children.append(child)
|
88 |
|
|
|
106 |
except NotImplementedError:
|
107 |
print(f"{child.name}'s attach_event is not implemented")
|
108 |
|
109 |
+
"""
|
110 |
+
clearing the children
|
111 |
+
we don't need them because they are going to live in the app.blocks and app.fns functions
|
112 |
+
"""
|
113 |
+
|
114 |
def _clear(self):
|
115 |
from gc import collect
|
116 |
|
|
|
160 |
self.global_children_dict[name] = self.main_layout
|
161 |
|
162 |
|
163 |
+
"""
|
164 |
+
---------------------------------------------------------------------------------------------------------
|
165 |
+
-- Example
|
166 |
+
"""
|
167 |
+
|
168 |
+
|
169 |
def change_text(new_str: str):
|
170 |
return Textbox(value=new_str)
|
171 |
|
|
|
224 |
)
|
225 |
|
226 |
|
227 |
+
"""
|
228 |
+
---------------------------------------------------------------------------------------------------------
|
229 |
+
-- Main
|
230 |
+
"""
|
231 |
+
|
232 |
gui = Application(title="Wrap Gradio")
|
233 |
|
234 |
first_tab = FirstTab(name="First Tab")
|