File size: 3,016 Bytes
ce1720b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import gradio as gr

# --- Core Data ---

Inventory = [
    (101, "Hammer", 120),
    (102, "Screwdriver Set", 350),
    (103, "Drill Machine", 2500),
    (104, "Pliers", 180),
    (105, "Wrench", 220),
]


def format_cart(cart):
    if not cart:
        return "Cart is empty."
    lines = []
    for item_id, name, price in cart:
        lines.append(f"{item_id} - {name}: ₹{price}")
    return "\n".join(lines)


def inventory_choices():
    return [f"{item_id} - {name} (₹{price})" for item_id, name, price in Inventory]


def cart_choices(cart):
    return [f"{item_id} - {name} (₹{price})" for item_id, name, price in cart]


def parse_id_from_choice(choice: str | None):
    if not choice:
        return None
    # choice format: "101 - Hammer (₹120)"
    try:
        return int(choice.split(" - ")[0].strip())
    except Exception:
        return None


# --- Actions ---

def add_item(selected_inventory_item, cart):
    cart = cart or []
    item_id = parse_id_from_choice(selected_inventory_item)
    if item_id is None:
        return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart))

    for inv_item in Inventory:
        if inv_item[0] == item_id:
            cart.append(inv_item)
            break

    return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart))


def remove_item(selected_cart_item, cart):
    cart = cart or []
    item_id = parse_id_from_choice(selected_cart_item)
    if item_id is None:
        return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart))

    cart = [item for item in cart if item[0] != item_id]
    return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart))


def calculate_total(cart):
    cart = cart or []
    return sum(price for _, _, price in cart)


# --- UI ---

with gr.Blocks() as demo:
    gr.Markdown("## 🧰 Hardware Store Billing System")

    cart_state = gr.State([])

    with gr.Row():
        inventory_dropdown = gr.Dropdown(
            choices=inventory_choices(),
            label="Select Item to Add",
        )
        add_btn = gr.Button("Add Tool")

    with gr.Row():
        cart_dropdown = gr.Dropdown(
            choices=[],
            label="Select Item to Remove",
        )
        remove_btn = gr.Button("Remove Tool")

    cart_display = gr.Textbox(
        label="Cart Contents",
        lines=10,
        interactive=False,
    )

    total_btn = gr.Button("Calculate Total")
    total_output = gr.Number(label="Total Bill")

    # Add item
    add_btn.click(
        fn=add_item,
        inputs=[inventory_dropdown, cart_state],
        outputs=[cart_state, cart_display, cart_dropdown],
    )

    # Remove item
    remove_btn.click(
        fn=remove_item,
        inputs=[cart_dropdown, cart_state],
        outputs=[cart_state, cart_display, cart_dropdown],
    )

    # Calculate total
    total_btn.click(
        fn=calculate_total,
        inputs=[cart_state],
        outputs=[total_output],
    )

if __name__ == "__main__":
    demo.launch()