lgaleana commited on
Commit
b92b58b
1 Parent(s): bc38777

Actually fix adding/removing

Browse files
Files changed (1) hide show
  1. app.py +58 -42
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Dict, List, Optional, NamedTuple, Type, Union
2
 
3
 
4
  import gradio as gr
@@ -21,7 +21,7 @@ class Input:
21
  interactive=True,
22
  placeholder="Variable value",
23
  )
24
- return gr_component
25
 
26
 
27
  class AITask:
@@ -59,11 +59,12 @@ class Component:
59
  # Internal state
60
  self._id = id_
61
  self.internal = internal
62
- self.visible = visible
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.gr_component = self.internal.render(bool(self.visible))
 
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].visible = True
90
- all_tasks[0].visible = True
91
 
92
 
93
- def add_input() -> Optional[List[Dict]]:
94
- for i, input_ in all_inputs.items():
95
- if not input_.visible:
96
- input_.visible = True
97
- return [gr.Row.update(visible=True)] * (i + 1) + [
98
- gr.Row.update(visible=False)
99
- ] * (MAX_INPUTS - i)
 
 
100
 
101
 
102
- def remove_input() -> Optional[List[Dict]]:
103
- for i, input_ in reversed(all_inputs.items()):
104
- if input_.visible:
105
- input_.visible = False
106
- return [gr.Row.update(visible=True)] * i + [
107
- gr.Row.update(visible=False)
108
- ] * (MAX_INPUTS - i + 1)
 
 
 
109
 
110
 
111
- def add_task() -> Optional[List[Dict]]:
112
- for i, task in all_tasks.items():
113
- if not task.visible:
114
- task.visible = True
115
- return [gr.Box.update(visible=True)] * (i + 1) + [
116
- gr.Box.update(visible=False)
117
- ] * (MAX_TASKS - i)
 
 
118
 
119
 
120
- def remove_task() -> Optional[List[Dict]]:
121
- for i, task in reversed(all_tasks.items()):
122
- if task.visible:
123
- task.visible = False
124
- return [gr.Box.update(visible=True)] * i + [
125
- gr.Box.update(visible=False)
126
- ] * (MAX_TASKS - i + 1)
 
 
 
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=[t.gr_component for t in all_tasks.values()],
 
168
  )
169
  remove_task_btn.click(
170
  remove_task,
171
- inputs=[],
172
- outputs=[t.gr_component for t in all_tasks.values()],
 
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()