lgaleana commited on
Commit
bc38777
1 Parent(s): 418800c

Fix adding/removing

Browse files
Files changed (1) hide show
  1. app.py +59 -62
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import NamedTuple, Type, Union
2
 
3
 
4
  import gradio as gr
@@ -9,19 +9,18 @@ MAX_TASKS = 50
9
 
10
 
11
  class Input:
12
- def render(self, visible: bool) -> gr.Box:
13
- with gr.Box(visible=visible) as gr_component: # TODO: Remove this
14
- with gr.Row():
15
- self.output_name = gr.Textbox(
16
- label="Input name (can be referenced with {})",
17
- interactive=True,
18
- placeholder="Variable name",
19
- )
20
- self.output = gr.Textbox(
21
- label="Input value",
22
- interactive=True,
23
- placeholder="Variable value",
24
- )
25
  return gr_component
26
 
27
 
@@ -56,21 +55,23 @@ class AITask:
56
 
57
 
58
  class Component:
59
- def __init__(self, id_: int, internal: Union[Input, AITask], visible: str = "0"):
 
60
  self._id = id_
61
- self.component_id: gr.Textbox
62
  self.internal = internal
 
 
 
 
 
63
  self.gr_component = gr.Box
64
- self._visible = visible
65
  self.output_name: gr.Textbox
66
  self.output: gr.Textbox
67
- self.source: gr.Textbox
68
 
69
  def render(self) -> None:
70
  self.component_id = gr.Textbox(value=str(self._id), visible=False)
71
  self.source = gr.Textbox(value=self.internal.__class__.__name__, visible=False)
72
- self.visible = gr.Textbox(value=self._visible, visible=False)
73
- self.gr_component = self.internal.render(bool(self._visible))
74
  self.output_name = self.internal.output_name
75
  self.output = self.internal.output
76
 
@@ -82,56 +83,52 @@ class Variable(NamedTuple):
82
  value: str
83
 
84
 
85
- all_inputs = [Component(i, Input()) for i in range(MAX_INPUTS)]
86
- all_tasks = [Component(i, AITask()) for i in range(MAX_TASKS)]
87
- all_components = all_inputs + all_tasks
88
-
89
- all_inputs[0]._visible = "1"
90
- all_tasks[0]._visible = "1"
91
-
92
-
93
- def _update_components(i: int, max: int):
94
- return [gr.Box.update(visible=True)] * i + [gr.Box.update(visible=False)] * (
95
- max - i
96
- )
97
-
98
 
99
- def add_input():
100
- global next_input
101
- if next_input < MAX_INPUTS:
102
- next_input += 1
103
- return _update_components(next_input, MAX_INPUTS)
104
 
105
 
106
- def remove_input():
107
- global next_input
108
- if next_input > 0:
109
- next_input -= 1
110
- return _update_components(next_input, MAX_INPUTS)
 
 
111
 
112
 
113
- def add_task():
114
- global next_task
115
- if next_task < MAX_TASKS:
116
- next_task += 1
117
- return _update_components(next_task, MAX_TASKS)
 
 
118
 
119
 
120
- def remove_task():
121
- global next_task
122
- if next_task > 0:
123
- next_task -= 1
124
- return _update_components(next_task, MAX_TASKS)
 
 
125
 
126
 
127
- def execute(output_names, outputs):
128
- for output_name in output_names:
129
- print(output_name)
 
 
 
 
130
 
131
 
132
  with gr.Blocks() as demo:
133
  # Initial layout
134
- for i in all_inputs:
135
  i.render()
136
  input_error = gr.HighlightedText(
137
  [("Repeated variable names in inputs. Please pick different names.", "Error")],
@@ -142,7 +139,7 @@ with gr.Blocks() as demo:
142
  add_input_btn = gr.Button("Add input variable")
143
  remove_input_btn = gr.Button("Remove input variable")
144
  execute_btn = gr.Button("Execute")
145
- for t in all_tasks:
146
  t.render()
147
  task_error = gr.HighlightedText(
148
  [("Repeated variable names in tasks. Please pick different names.", "Error")],
@@ -157,22 +154,22 @@ with gr.Blocks() as demo:
157
  add_input_btn.click(
158
  add_input,
159
  inputs=[],
160
- outputs=[i.gr_component for i in all_inputs],
161
  )
162
  remove_input_btn.click(
163
  remove_input,
164
  inputs=[],
165
- outputs=[i.gr_component for i in all_inputs],
166
  )
167
  add_task_btn.click(
168
  add_task,
169
  inputs=[],
170
- outputs=[t.gr_component for t in all_tasks],
171
  )
172
  remove_task_btn.click(
173
  remove_task,
174
  inputs=[],
175
- outputs=[t.gr_component for t in all_tasks],
176
  )
177
 
178
  demo.launch()
 
1
+ from typing import Dict, List, Optional, NamedTuple, Type, Union
2
 
3
 
4
  import gradio as gr
 
9
 
10
 
11
  class Input:
12
+ def render(self, visible: bool) -> gr.Row:
13
+ with gr.Row(visible=visible) as gr_component:
14
+ self.output_name = gr.Textbox(
15
+ label="Input name (can be referenced with {})",
16
+ interactive=True,
17
+ placeholder="Variable name",
18
+ )
19
+ self.output = gr.Textbox(
20
+ label="Input value",
21
+ interactive=True,
22
+ placeholder="Variable value",
23
+ )
 
24
  return gr_component
25
 
26
 
 
55
 
56
 
57
  class Component:
58
+ def __init__(self, id_: int, internal: Union[Input, AITask], visible: bool = False):
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
 
70
 
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
 
 
83
  value: str
84
 
85
 
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:
130
  # Initial layout
131
+ for i in all_inputs.values():
132
  i.render()
133
  input_error = gr.HighlightedText(
134
  [("Repeated variable names in inputs. Please pick different names.", "Error")],
 
139
  add_input_btn = gr.Button("Add input variable")
140
  remove_input_btn = gr.Button("Remove input variable")
141
  execute_btn = gr.Button("Execute")
142
+ for t in all_tasks.values():
143
  t.render()
144
  task_error = gr.HighlightedText(
145
  [("Repeated variable names in tasks. Please pick different names.", "Error")],
 
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()