install psycopg2 https://pypi.org/project/psycopg2/
Psycopg is the most popular PostgreSQL database adapter for the Python programming language.
(venv) PS C:\flaskmyproject> pip install psycopg2
this post a user select multiple records using checkbox fields and click the delete button a loading image gif show and a successfully message display and animate effect every row selected without refresh the page
https://github.com/jquery-form/form
The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allow you to have full control over how the data is submitted.
CREATE TABLE employee (
id serial PRIMARY KEY,
name VARCHAR ( 100 ) NOT NULL,
position VARCHAR ( 100 ) NOT NULL,
office VARCHAR ( 100 ) NOT NULL,
age INT NOT NULL,
salary INT NOT NULL,
photo VARCHAR ( 150 ) NOT NULL,
);
INSERT INTO
employee(name, position, office, age, salary, photo)
VALUES
('Tiger Wood', 'Accountant', 'Tokyo', 36, 5689, '01.jpg'),
('Mark Oto Ednalan', 'Chief Executive Officer (CEO)', 'London', 56, 5648, '02.jpg'),
('Jacob thompson', 'Junior Technical Author', 'San Francisco', 23, 5689, '03.jpg'),
('cylde Ednalan', 'Software Engineer', 'Olongapo', 23, 54654, '04.jpg'),
('Rhona Davidson', 'Software Engineer', 'San Francisco', 26, 5465, '05.jpg'),
('Quinn Flynn', 'Integration Specialist', 'New York', 53, 56465, '06.jpg'),
('Tiger Nixon', 'Software Engineer', 'London', 45, 456, '07.jpg'),
('Airi Satou', 'Pre-Sales Support', 'New York', 25, 4568, '08.jpg'),
('Angelica Ramos', 'Sales Assistant', 'New York', 45, 456, '09.jpg'),
('Ashton updated', 'Senior Javascript Developer', 'Olongapo', 45, 54565, '01.jpg'),
('Bradley Greer', 'Regional Director', 'San Francisco', 27, 5485, '02.jpg'),
('Brenden Wagner', 'Javascript Developer', 'San Francisco', 38, 65468, '03.jpg'),
('Brielle Williamson', 'Personnel Lead', 'Olongapo', 56, 354685, '04.jpg'),
('Bruno Nash', 'Customer Support', 'New York', 36, 65465, '05.jpg'),
('cairocoders', 'Sales Assistant', 'Sydney', 45, 56465, '06.jpg'),
('Zorita Serrano', 'Support Engineer', 'San Francisco', 38, 6548, '07.jpg'),
('Zenaida Frank', 'Chief Operating Officer (COO)', 'San Francisco', 39, 545, '08.jpg'),
('Sakura Yamamoto', 'Support Engineer', 'Tokyo', 48, 5468, '05.jpg'),
('Serge Baldwin', 'Data Coordinator', 'Singapore', 85, 5646, '05.jpg'),
('Shad Decker', 'Regional Director', 'Tokyo', 45, 4545, '05.jpg');
#app.py from flask import Flask, render_template, request, jsonify import psycopg2 #pip install psycopg2 import psycopg2.extras app = Flask(__name__) app.secret_key = "caircocoders-ednalan" DB_HOST = "localhost" DB_NAME = "sampledb" DB_USER = "postgres" DB_PASS = "admin" conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST) @app.route('/') def index(): cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute("SELECT * FROM employee ORDER BY id DESC") employeelist = cur.fetchall() return render_template('index.html', employeelist=employeelist) @app.route("/delete",methods=["POST","GET"]) def delete(): cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) for getid in request.form.getlist('checkdelete'): print(getid) cur.execute('DELETE FROM employee WHERE id = {0}'.format(getid)) conn.commit() cur.close() return jsonify('Records deleted successfully') if __name__ == "__main__": app.run(debug=True)templates/index.html
//templates/index.html <!DOCTYPE html> <html> <head> <title>Delete Multiple Records using Jquey Ajax Python Flask PostgreSQL</title> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" integrity="sha384-qlmct0AOBiA2VPZkMY3+2WqkHtIQ9lSdAsAn5RUJD/3vA5MKDgSGcdmIv4ycVxyn" crossorigin="anonymous"></script> </head> <body> <div class="container"> <br /> <div class="table-responsive"> <h3 align="center">Delete Multiple Records using Jquey Ajax Python Flask PostgreSQL</h3><br /> <div class="table-responsive"> <div id="targetLayer" class="btn btn-success" style="display:none;width:100%;margin-bottom: 10px;"></div> <div id="loader-icon" style="display:none;"><img src="/static/img/loader.gif" /></div> <form id="deleteall" action="/delete" method="post"> <table class="table table-bordered"> <thead> <tr> <th width="5%"> <input type="submit" value="Delete" name="delete_all" id="delete_all" class="btn btn-danger btn-xs" /> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Salary</th> </tr> </thead> {% for row in employeelist %} <tr> <td> <input type="checkbox" name="checkdelete" id="checkdelete" class="delete_checkbox" value="{{row.id}}" /> </td> <td>{{row.name}}</td> <td>{{row.position}}</td> <td>{{row.office}}</td> <td>{{row.age}}</td> <td>$ {{ "%.2f"|format(row.salary) }}</td> </tr> {% endfor %} </table> </form> </div> </div> </div> <style> .removeRow {background-color: #64b418;color:#FFFFFF;} </style> <script> $(document).ready(function(){ $('.delete_checkbox').click(function(){ if($(this).is(':checked')) { $(this).closest('tr').addClass('removeRow'); }else{ $(this).closest('tr').removeClass('removeRow'); } }); $('#deleteall').submit(function(event){ if($('#checkdelete').val()){ event.preventDefault(); $('#loader-icon').show(); $('#targetLayer').hide(); $(this).ajaxSubmit({ target: '#targetLayer', success:function(data){ $('.removeRow').fadeOut(1500); $('#loader-icon').hide(); $('#targetLayer').show(); $('#targetLayer').html(data); }, resetForm: true }); } return false; }); }); </script> </body> </html>