jedeland commited on
Commit
bde77a1
·
1 Parent(s): bee75d0
Files changed (1) hide show
  1. app.py +40 -35
app.py CHANGED
@@ -1,48 +1,53 @@
1
  import gradio as gr
2
 
 
 
3
 
4
- # Function to dynamically add new text fields
5
- def add_text_field(inputs):
6
- inputs.append("")
7
- return gr.update(visible=True), inputs
8
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Function to aggregate all inputs into a list
11
- def process_ingredients(*ingredients):
12
- return [ingredient for ingredient in ingredients if ingredient]
13
 
 
 
 
 
 
14
 
 
 
15
  def app():
16
  with gr.Blocks() as demo:
17
- inputs = [] # Shared list to store ingredients
18
-
19
- # Container to hold text fields
20
- ingredient_fields = gr.Row(visible=True)
21
-
22
- # Buttons for adding and submitting
23
  with gr.Row():
24
- add_button = gr.Button("+ Add Ingredient")
25
- submit_button = gr.Button("Submit")
26
-
27
- # Output field for displaying the list
28
- output = gr.Textbox(label="Ingredients List", interactive=False)
29
-
30
- # Function to add a new ingredient field
31
- def add_field():
32
- inputs.append("") # Add a new placeholder for the new input
33
- with ingredient_fields:
34
- for i, ingredient in enumerate(inputs):
35
- inputs[i] = gr.Textbox(
36
- value=inputs[i], label=f"Ingredient {i + 1}"
37
- ).value
38
-
39
- # Function to submit
40
- def submit_list():
41
- return process_ingredients(*inputs)
42
-
43
- # Click logic for the buttons
44
- add_button.click(fn=add_field)
45
- submit_button.click(fn=submit_list, outputs=output)
46
 
47
  return demo
48
 
 
1
  import gradio as gr
2
 
3
+ # Data storage
4
+ ingredients_list = []
5
 
 
 
 
 
6
 
7
+ # Function to add ingredient
8
+ def add_ingredient(ingredient, quantity):
9
+ if ingredient and quantity > 0:
10
+ ingredients_list.append(f"{ingredient}: {quantity} grams")
11
+ return (
12
+ "\n".join(ingredients_list),
13
+ gr.update(value="", interactive=True),
14
+ gr.update(value=None, interactive=True),
15
+ )
16
 
 
 
 
17
 
18
+ # Function to enable/disable add button
19
+ def validate_inputs(ingredient, quantity):
20
+ if ingredient and quantity > 0:
21
+ return gr.update(interactive=True)
22
+ return gr.update(interactive=False)
23
 
24
+
25
+ # App
26
  def app():
27
  with gr.Blocks() as demo:
 
 
 
 
 
 
28
  with gr.Row():
29
+ ingredient_input = gr.Textbox(
30
+ label="Ingredient", placeholder="Enter ingredient name"
31
+ )
32
+ quantity_input = gr.Number(label="Quantity (grams)", value=None)
33
+
34
+ add_button = gr.Button("Add Ingredient", interactive=False)
35
+ output = gr.Textbox(label="Ingredients List", lines=10, interactive=False)
36
+
37
+ # Validate inputs
38
+ ingredient_input.change(
39
+ validate_inputs, [ingredient_input, quantity_input], add_button
40
+ )
41
+ quantity_input.change(
42
+ validate_inputs, [ingredient_input, quantity_input], add_button
43
+ )
44
+
45
+ # Add ingredient logic
46
+ add_button.click(
47
+ add_ingredient,
48
+ [ingredient_input, quantity_input],
49
+ [output, ingredient_input, quantity_input],
50
+ )
51
 
52
  return demo
53