0% found this document useful (0 votes)
10 views5 pages

FLASK

This document contains a Flask application that serves a web interface for managing a shopping cart. It includes routes for the main page, contact page, and product/menu pages, as well as functionality to process cart items and calculate total costs. The application handles user input for product selection and updates the cart accordingly.

Uploaded by

vivianpunay6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

FLASK

This document contains a Flask application that serves a web interface for managing a shopping cart. It includes routes for the main page, contact page, and product/menu pages, as well as functionality to process cart items and calculate total costs. The application handles user input for product selection and updates the cart accordingly.

Uploaded by

vivianpunay6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

from flask import *

app = Flask(_name_, static_url_path="", static_folder="static", template_folder="templates")

list_carts = []

total_cost = 0

@app.route('/')

def index():

return render_template('index.html')

@app.route('/contact_us.html')

def contact_us():

return render_template('contact_us.html')

@app.route('/product.html')

def product():

return render_template('product.html')

@app.route('/carts', methods=['POST'])

def carts():

list_carts = [] # Store cart items

total_cost = 0 # Calculate total cost

# Loop through items 1 to 26 (for 26 product items)

for i in range(1, 27):


menu = request.form.get(f'menu{i}')

cost = request.form.get(f'cost{i}')

qty = request.form.get(f'qty{i}')

# Only process items that have a valid menu, cost, and quantity greater than 0

if menu and cost and qty:

try:

# Convert cost to float and qty to int

cost = float(cost)

qty = int(qty)

# If quantity is greater than 0, add to the cart

if qty > 0:

subtotal = cost * qty

# Add to cart list

list_carts.append([menu, cost, qty, subtotal])

# Update total cost

total_cost += subtotal

except ValueError:

# Skip item if conversion fails (invalid cost or qty)

continue

# If there are no items in the cart, total cost should be 0

if not list_carts:

total_cost = 0
# Render the template after processing all items

return render_template('carts.html', list_carts=list_carts, total_cost=total_cost)

if _name_ == '_main_':

app.run(debug=True)

--------------------------------------------------------------------------------------------------------------------

from flask import Flask, request, render_template

app = Flask(__name__, static_url_path="", static_folder="static", template_folder="templates")

@app.route('/')

def index():

return render_template('index.html')

@app.route('/contact_us.html')

def contact_us():

return render_template('contact_us.html')

@app.route('/menu.html')

def menu():

return render_template('menu.html')

@app.route('/carts', methods=['POST'])
def carts():

list_carts = [] # Store cart items

total_cost = 0 # Calculate total cost

# Loop through items 1 to 29 (for 29 menu items)

for i in range(1, 29):

name = request.form.get(f'name{i}')

cost = request.form.get(f'cost{i}')

qty = request.form.get(f'qty{i}')

# Only process items that have a valid name, cost, and quantity

if name and cost and qty:

try:

# Convert cost to float and qty to int

cost = float(cost)

qty = int(qty)

# Add to cart list

list_carts.append([name, cost, qty])

# Update total cost

total_cost += cost * qty

except ValueError:

# Skip item if conversion fails (invalid cost or qty)

continue

return render_template('carts.html', list_carts=list_carts, total_cost=total_cost)

if __name__ == '__main__':
app.run(debug=True)

You might also like