File size: 1,468 Bytes
c54c38e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee75d0
 
 
 
c54c38e
bee75d0
c54c38e
 
 
 
bee75d0
c54c38e
 
bee75d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c54c38e
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr


# Function to dynamically add new text fields
def add_text_field(inputs):
    inputs.append("")
    return gr.update(visible=True), inputs


# Function to aggregate all inputs into a list
def process_ingredients(*ingredients):
    return [ingredient for ingredient in ingredients if ingredient]


def app():
    with gr.Blocks() as demo:
        inputs = []  # Shared list to store ingredients

        # Container to hold text fields
        ingredient_fields = gr.Row(visible=True)

        # Buttons for adding and submitting
        with gr.Row():
            add_button = gr.Button("+ Add Ingredient")
            submit_button = gr.Button("Submit")

        # Output field for displaying the list
        output = gr.Textbox(label="Ingredients List", interactive=False)

        # Function to add a new ingredient field
        def add_field():
            inputs.append("")  # Add a new placeholder for the new input
            with ingredient_fields:
                for i, ingredient in enumerate(inputs):
                    inputs[i] = gr.Textbox(
                        value=inputs[i], label=f"Ingredient {i + 1}"
                    ).value

        # Function to submit
        def submit_list():
            return process_ingredients(*inputs)

        # Click logic for the buttons
        add_button.click(fn=add_field)
        submit_button.click(fn=submit_list, outputs=output)

    return demo


demo = app()
demo.launch()