Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334from flask import Flask, request, render_templateapp = Flask(__name__)"print"@app.route('/')def index():return render_template('index.html')@app.route('/calculate', methods=['POST'])def calculate():try:num1 = float(request.form['num1'])num2 = float(request.form['num2'])operation = request.form['operation']if operation == 'add':result = num1 + num2elif operation == 'subtract':result = num1 - num2elif operation == 'multiply':result = num1 * num2elif operation == 'divide':result = num1 / num2else:result = 'Invalid operation'return render_template('index.html', result=result)except Exception as e:return render_template('index.html', result=f"Error: {str(e)}")if __name__ == '__main__':app.run(debug=True)"hi baby"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566from flask import Flask, render_template, request, redirect, url_forimport sqlite3app = Flask(__name__)# Database setupdef init_db():conn = sqlite3.connect('inventory.db')cursor = conn.cursor()cursor.execute('''CREATE TABLE IF NOT EXISTS inventory (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,quantity INTEGER NOT NULL,price REAL NOT NULL)''')conn.commit()conn.close()init_db()@app.route('/')def index():conn = sqlite3.connect('inventory.db')cursor = conn.cursor()cursor.execute("SELECT * FROM inventory")items = cursor.fetchall()conn.close()return render_template('index.html', items=items)@app.route('/add', methods=['POST'])def add_item():name = request.form['name']quantity = request.form['quantity']price = request.form['price']conn = sqlite3.connect('inventory.db')cursor = conn.cursor()cursor.execute("INSERT INTO inventory (name, quantity, price) VALUES (?, ?, ?)", (name, quantity, price))conn.commit()conn.close()return redirect(url_for('index'))@app.route('/delete/<int:item_id>')def delete_item(item_id):conn = sqlite3.connect('inventory.db')cursor = conn.cursor()cursor.execute("DELETE FROM inventory WHERE id = ?", (item_id,))conn.commit()conn.close()return redirect(url_for('index'))@app.route('/update/<int:item_id>', methods=['POST'])def update_item(item_id):name = request.form['name']quantity = request.form['quantity']price = request.form['price']conn = sqlite3.connect('inventory.db')cursor = conn.cursor()cursor.execute("UPDATE inventory SET name = ?, quantity = ?, price = ? WHERE id = ?", (name, quantity, price, item_id))conn.commit()conn.close()return redirect(url_for('index'))if __name__ == '__main__':app.run(debug=True)
app.py+59-27