python flask crud


https://github.com/safakozdek/Crud-App-Using-Flask-and-MySQL

https://github.com/fatematzuhora/Flask-SQLAlchemy-RESTful-CRUD

https://github.com/itsimplified/slick-crud-app

https://github.com/gurkanakdeniz/example-flask-crud

pip install --upgrade pip

pip install -r requirements.txt

export FLASK_APP=crudapp.py

flask db init

flask db migrate -m "entries table"

flask db upgrade

flask run

Build a Simple CRUD App with Flask and Python

https://developer.okta.com/blog/2018/07/23/build-a-simple-crud-app-with-flask-and-python

https://www.askpython.com/python-modules/flask/flask-crud-application

Full code for the CRUD Application

The models.py:

from flask_sqlalchemy import SQLAlchemy db =SQLAlchemy() class EmployeeModel(db.Model): __tablename__ = "table" id = db.Column(db.Integer, primary_key=True) employee_id = db.Column(db.Integer(),unique = True) name = db.Column(db.String()) age = db.Column(db.Integer()) position = db.Column(db.String(80)) def __init__(self, employee_id,name,age,position): self.employee_id = employee_id self.name = name self.age = age self.position = position def __repr__(self): return f"{self.name}:{self.employee_id}"

The main flask application:

from flask import Flask,render_template,request,redirect from models import db,EmployeeModel app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) @app.before_first_request def create_table(): db.create_all() @app.route('/data/create' , methods = ['GET','POST']) def create(): if request.method == 'GET': return render_template('createpage.html') if request.method == 'POST': employee_id = request.form['employee_id'] name = request.form['name'] age = request.form['age'] position = request.form['position'] employee = EmployeeModel(employee_id=employee_id, name=name, age=age, position = position) db.session.add(employee) db.session.commit() return redirect('/data') @app.route('/data') def RetrieveList(): employees = EmployeeModel.query.all() return render_template('datalist.html',employees = employees) @app.route('/data/<int:id>') def RetrieveEmployee(id): employee = EmployeeModel.query.filter_by(employee_id=id).first() if employee: return render_template('data.html', employee = employee) return f"Employee with id ={id} Doenst exist" @app.route('/data/<int:id>/update',methods = ['GET','POST']) def update(id): employee = EmployeeModel.query.filter_by(employee_id=id).first() if request.method == 'POST': if employee: db.session.delete(employee) db.session.commit() name = request.form['name'] age = request.form['age'] position = request.form['position'] employee = EmployeeModel(employee_id=id, name=name, age=age, position = position) db.session.add(employee) db.session.commit() return redirect(f'/data/{id}') return f"Employee with id = {id} Does nit exist" return render_template('update.html', employee = employee) @app.route('/data/<int:id>/delete', methods=['GET','POST']) def delete(id): employee = EmployeeModel.query.filter_by(employee_id=id).first() if request.method == 'POST': if employee: db.session.delete(employee) db.session.commit() return redirect('/data') abort(404) return render_template('delete.html') app.run(host='localhost', port=5000)