-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIcall.py
More file actions
30 lines (22 loc) · 813 Bytes
/
APIcall.py
File metadata and controls
30 lines (22 loc) · 813 Bytes
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
from flask import Flask, request, redirect, url_for, render_template,jsonify
app = Flask(__name__)
cart = []
@app.route('/')
def index():
return render_template('index.html', cart=cart)
@app.route('/api/add', methods=['POST'])
def add_to_cart():
product = {
'name': request.form['name'],
}
cart.append(product)
return jsonify({'message': 'Product added to cart', 'cart': cart}), 201
return redirect(url_for('index'))
@app.route('/api/delete/<string:product_name>', methods=['DELETE'])
def delete_from_cart(product_name):
global cart
cart = [product for product in cart if product['name'] != product_name]
return jsonify({'message': 'Product deleted ', 'cart': cart}), 201
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)