Actually fix adding/removing
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from typing import
|
2 |
|
3 |
|
4 |
import gradio as gr
|
@@ -21,7 +21,7 @@ class Input:
|
|
21 |
interactive=True,
|
22 |
placeholder="Variable value",
|
23 |
)
|
24 |
-
|
25 |
|
26 |
|
27 |
class AITask:
|
@@ -59,11 +59,12 @@ class Component:
|
|
59 |
# Internal state
|
60 |
self._id = id_
|
61 |
self.internal = internal
|
62 |
-
self.
|
63 |
|
64 |
# Gradio state
|
65 |
self.component_id: gr.Textbox
|
66 |
self.source: gr.Textbox
|
|
|
67 |
self.gr_component = gr.Box
|
68 |
self.output_name: gr.Textbox
|
69 |
self.output: gr.Textbox
|
@@ -71,7 +72,8 @@ class Component:
|
|
71 |
def render(self) -> None:
|
72 |
self.component_id = gr.Textbox(value=str(self._id), visible=False)
|
73 |
self.source = gr.Textbox(value=self.internal.__class__.__name__, visible=False)
|
74 |
-
self.
|
|
|
75 |
self.output_name = self.internal.output_name
|
76 |
self.output = self.internal.output
|
77 |
|
@@ -86,44 +88,54 @@ class Variable(NamedTuple):
|
|
86 |
all_inputs = {i: Component(i, Input()) for i in range(MAX_INPUTS)}
|
87 |
all_tasks = {i: Component(i, AITask()) for i in range(MAX_TASKS)}
|
88 |
|
89 |
-
all_inputs[0].
|
90 |
-
all_tasks[0].
|
91 |
|
92 |
|
93 |
-
def add_input()
|
94 |
-
for i,
|
95 |
-
if not
|
96 |
-
|
97 |
-
|
98 |
-
gr.Row.update(visible=False)
|
99 |
-
|
|
|
|
|
100 |
|
101 |
|
102 |
-
def remove_input()
|
103 |
-
for i,
|
104 |
-
|
105 |
-
|
106 |
-
return
|
107 |
-
gr.Row.update(visible=
|
108 |
-
|
|
|
|
|
|
|
109 |
|
110 |
|
111 |
-
def add_task()
|
112 |
-
for i,
|
113 |
-
if not
|
114 |
-
|
115 |
-
|
116 |
-
gr.Box.update(visible=False)
|
117 |
-
|
|
|
|
|
118 |
|
119 |
|
120 |
-
def remove_task()
|
121 |
-
for i,
|
122 |
-
|
123 |
-
|
124 |
-
return
|
125 |
-
gr.Box.update(visible=
|
126 |
-
|
|
|
|
|
|
|
127 |
|
128 |
|
129 |
with gr.Blocks() as demo:
|
@@ -153,23 +165,27 @@ with gr.Blocks() as demo:
|
|
153 |
# Layout editing
|
154 |
add_input_btn.click(
|
155 |
add_input,
|
156 |
-
inputs=[],
|
157 |
-
outputs=[i.gr_component for i in all_inputs.values()]
|
|
|
158 |
)
|
159 |
remove_input_btn.click(
|
160 |
remove_input,
|
161 |
-
inputs=[],
|
162 |
-
outputs=[i.gr_component for i in all_inputs.values()]
|
|
|
163 |
)
|
164 |
add_task_btn.click(
|
165 |
add_task,
|
166 |
-
inputs=[],
|
167 |
-
outputs=[
|
|
|
168 |
)
|
169 |
remove_task_btn.click(
|
170 |
remove_task,
|
171 |
-
inputs=[],
|
172 |
-
outputs=[
|
|
|
173 |
)
|
174 |
|
175 |
demo.launch()
|
|
|
1 |
+
from typing import NamedTuple, Type, Union
|
2 |
|
3 |
|
4 |
import gradio as gr
|
|
|
21 |
interactive=True,
|
22 |
placeholder="Variable value",
|
23 |
)
|
24 |
+
return gr_component
|
25 |
|
26 |
|
27 |
class AITask:
|
|
|
59 |
# Internal state
|
60 |
self._id = id_
|
61 |
self.internal = internal
|
62 |
+
self._initial_visibility = visible
|
63 |
|
64 |
# Gradio state
|
65 |
self.component_id: gr.Textbox
|
66 |
self.source: gr.Textbox
|
67 |
+
self.visible: gr.Number
|
68 |
self.gr_component = gr.Box
|
69 |
self.output_name: gr.Textbox
|
70 |
self.output: gr.Textbox
|
|
|
72 |
def render(self) -> None:
|
73 |
self.component_id = gr.Textbox(value=str(self._id), visible=False)
|
74 |
self.source = gr.Textbox(value=self.internal.__class__.__name__, visible=False)
|
75 |
+
self.visible = gr.Number(int(self._initial_visibility), visible=False)
|
76 |
+
self.gr_component = self.internal.render(self._initial_visibility)
|
77 |
self.output_name = self.internal.output_name
|
78 |
self.output = self.internal.output
|
79 |
|
|
|
88 |
all_inputs = {i: Component(i, Input()) for i in range(MAX_INPUTS)}
|
89 |
all_tasks = {i: Component(i, AITask()) for i in range(MAX_TASKS)}
|
90 |
|
91 |
+
all_inputs[0]._initial_visibility = True
|
92 |
+
all_tasks[0]._initial_visibility = True
|
93 |
|
94 |
|
95 |
+
def add_input(*visibility):
|
96 |
+
for i, visible in enumerate(visibility, 1):
|
97 |
+
if not bool(visible):
|
98 |
+
return (
|
99 |
+
[gr.Row.update(visible=True)] * i
|
100 |
+
+ [gr.Row.update(visible=False)] * (MAX_INPUTS - i)
|
101 |
+
+ [1] * i
|
102 |
+
+ [0] * (MAX_INPUTS - i)
|
103 |
+
)
|
104 |
|
105 |
|
106 |
+
def remove_input(*visibility):
|
107 |
+
for i, visible in reversed(list(enumerate(visibility, 1))):
|
108 |
+
print(i, visible)
|
109 |
+
if bool(visible):
|
110 |
+
return (
|
111 |
+
[gr.Row.update(visible=True)] * (i - 1)
|
112 |
+
+ [gr.Row.update(visible=False)] * (MAX_INPUTS - i + 1)
|
113 |
+
+ [1] * (i - 1)
|
114 |
+
+ [0] * (MAX_INPUTS - i + 1)
|
115 |
+
)
|
116 |
|
117 |
|
118 |
+
def add_task(*visibility):
|
119 |
+
for i, visible in enumerate(visibility, 1):
|
120 |
+
if not bool(visible):
|
121 |
+
return (
|
122 |
+
[gr.Box.update(visible=True)] * i
|
123 |
+
+ [gr.Box.update(visible=False)] * (MAX_TASKS - i)
|
124 |
+
+ [1] * i
|
125 |
+
+ [0] * (MAX_TASKS - i)
|
126 |
+
)
|
127 |
|
128 |
|
129 |
+
def remove_task(*visibility):
|
130 |
+
for i, visible in reversed(list(enumerate(visibility, 1))):
|
131 |
+
print(i, visible)
|
132 |
+
if bool(visible):
|
133 |
+
return (
|
134 |
+
[gr.Box.update(visible=True)] * (i - 1)
|
135 |
+
+ [gr.Box.update(visible=False)] * (MAX_TASKS - i + 1)
|
136 |
+
+ [1] * (i - 1)
|
137 |
+
+ [0] * (MAX_TASKS - i + 1)
|
138 |
+
)
|
139 |
|
140 |
|
141 |
with gr.Blocks() as demo:
|
|
|
165 |
# Layout editing
|
166 |
add_input_btn.click(
|
167 |
add_input,
|
168 |
+
inputs=[i.visible for i in all_inputs.values()],
|
169 |
+
outputs=[i.gr_component for i in all_inputs.values()]
|
170 |
+
+ [i.visible for i in all_inputs.values()],
|
171 |
)
|
172 |
remove_input_btn.click(
|
173 |
remove_input,
|
174 |
+
inputs=[i.visible for i in all_inputs.values()],
|
175 |
+
outputs=[i.gr_component for i in all_inputs.values()]
|
176 |
+
+ [i.visible for i in all_inputs.values()],
|
177 |
)
|
178 |
add_task_btn.click(
|
179 |
add_task,
|
180 |
+
inputs=[i.visible for i in all_tasks.values()],
|
181 |
+
outputs=[i.gr_component for i in all_tasks.values()]
|
182 |
+
+ [i.visible for i in all_tasks.values()],
|
183 |
)
|
184 |
remove_task_btn.click(
|
185 |
remove_task,
|
186 |
+
inputs=[i.visible for i in all_tasks.values()],
|
187 |
+
outputs=[i.gr_component for i in all_tasks.values()]
|
188 |
+
+ [i.visible for i in all_tasks.values()],
|
189 |
)
|
190 |
|
191 |
demo.launch()
|