Sami2000 commited on
Commit
020e118
·
verified ·
1 Parent(s): 83ae4f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ """
4
+ Create a Gradio interface for the complete code of the above case study.
5
+ The interface should allow users to add items to the cart, remove items, display the cart, and calculate the total cost interactively.
6
+ """
7
+
8
+ from gradio import Blocks, Number, Button, Textbox
9
+
10
+ Inventory = [
11
+ (1, "Apples", 250),
12
+ (2, "Cherry", 650),
13
+ (3, "Chickoo", 50),
14
+ (4, "Grapes", 90),
15
+ (5, "Mangoes", 150),
16
+ ]
17
+
18
+ Your_Cart = []
19
+
20
+ def add_item_to_cart(item_id, Inventory):
21
+ """
22
+ Adds an item from the Inventory to the shopping cart.
23
+ """
24
+ try:
25
+ item = Inventory[item_id - 1]
26
+ Your_Cart.append(item)
27
+ return f"{'>'*10} The item '{item[1]}' has been added to the cart. {'<'*10} \n\n"
28
+ except IndexError:
29
+ return f"Invalid item ID: {item_id}"
30
+
31
+ def remove_item(cart_index, cart_items):
32
+ """
33
+ Removes an item from the shopping cart.
34
+ """
35
+ try:
36
+ removed_item = cart_items.pop(cart_index - 1)
37
+ return f"{'>'*10} The item '{removed_item[1]}' has been removed from the cart, and your shopping cart has been updated. {'<'*10} \n\n"
38
+ except IndexError:
39
+ return "Invalid cart index or cart is empty."
40
+
41
+ def display_cart(Your_Cart):
42
+ """
43
+ Displays the items in the shopping cart.
44
+ """
45
+ if Your_Cart:
46
+ cart_str = "" # Initialize an empty string to store the cart display
47
+ cart_str += "+" * 50 + "\n"
48
+ cart_str += f"{'Your Cart'.center(40)}" + "\n"
49
+ cart_str += "+" * 50 + "\n"
50
+ cart_str += f"{'Index':<10} {'Name':<20} {'Quantity':<10} {'Price Per KG':<10}" + "\n"
51
+ cart_str += "-" * 50 + "\n"
52
+ unique_items = set(Your_Cart) # Get unique items in the cart
53
+ total_cost = 0
54
+ for index, item in enumerate(unique_items):
55
+ quantity = Your_Cart.count(item)
56
+ price_per_kg = item[2]
57
+ total_price = price_per_kg * quantity
58
+ cart_str += f"{index+1:<10} {item[1]:<20} {quantity:<10} ${total_price:<10.2f}" + "\n"
59
+ total_cost += total_price
60
+ cart_str += "-" * 50 + "\n"
61
+ cart_str += f"{'Total Cost':<40} ${total_cost:<10.2f}" # Display grand total
62
+ return cart_str # Return the string representation of the cart
63
+ else:
64
+ return f"{'>'*10} Your cart is empty !!!, Why not buy Fruits from us ... {'<'*10} \n\n"
65
+
66
+ def total_cost(cart_items):
67
+ """
68
+ Calculates the total cost of all items in the shopping cart.
69
+ """
70
+ total = 0
71
+ for item in cart_items:
72
+ total += item[2] # Assuming quantity is 1 for now
73
+ return f"~"*40 + "\n" + f"Total cost: {total}".center(40) + "\n" + "~"*40 + "\n\n"
74
+
75
+ def gradio_interface(item_id, cart_index):
76
+ """
77
+ Handles adding/removing items, displaying the cart, and calculating the total cost.
78
+
79
+ Args:
80
+ item_id (int): The product ID to add to the cart (if any).
81
+ cart_index (int): The product ID to remove from the cart (if any).
82
+
83
+ Returns:
84
+ tuple: A tuple containing the updated cart display, add message, remove message, and total cost.
85
+ """
86
+
87
+ # Add item
88
+ add_msg = ""
89
+ if item_id:
90
+ add_msg = add_item_to_cart(item_id, Inventory)
91
+
92
+ # Remove item
93
+ remove_msg = ""
94
+ if cart_index:
95
+ remove_msg = remove_item(cart_index, Your_Cart)
96
+
97
+ # Display the cart and calculate total cost
98
+ cart_display = display_cart(Your_Cart)
99
+ cost_msg = total_cost(Your_Cart)
100
+
101
+ return cart_display, add_msg, remove_msg, cost_msg
102
+
103
+ # Gradio interface creation
104
+ def launch_gradio():
105
+ """
106
+ Launches the Gradio interface for the fruit shopping cart system.
107
+ """
108
+
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown("### Fruit Shopping Cart System")
111
+
112
+ with gr.Row():
113
+ item_id_input = gr.Number(label="Enter Product ID to Add to Cart", interactive=True)
114
+ cart_index_input = gr.Number(label="Enter Index ID to Remove from Cart", interactive=True)
115
+
116
+ with gr.Row():
117
+ add_button = gr.Button("Add to Cart")
118
+ remove_button = gr.Button("Remove from Cart")
119
+
120
+ cart_display_output = gr.Textbox(label="Your Cart", interactive=False)
121
+ add_message_output = gr.Textbox(label="Add Item Message", interactive=False)
122
+ remove_message_output = gr.Textbox(label="Remove Item Message", interactive=False)
123
+ total_cost_output = gr.Textbox(label="Total Cost", interactive=False)
124
+
125
+ # Add item button click behavior
126
+ add_button.click(
127
+ gradio_interface,
128
+ inputs=[item_id_input, gr.Number(visible=False, value=0)], # Pass a hidden Number component for cart_index
129
+ outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
130
+ )
131
+
132
+ # Remove item button click behavior
133
+ remove_button.click(
134
+ gradio_interface,
135
+ inputs=[gr.Number(visible=False, value=0), cart_index_input], # Pass a hidden Number component for item_id
136
+ outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
137
+ )
138
+ demo.launch()
139
+
140
+ # Run the Gradio interface
141
+ if __name__ == "__main__":
142
+ launch_gradio()