Skip to content

Commit 1bf7abd

Browse files
committed
update
1 parent c14ccb6 commit 1bf7abd

File tree

43 files changed

+1374
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1374
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:app
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from flask import Flask, render_template, request, redirect, url_for, flash
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
5+
app = Flask(__name__)
6+
app.secret_key = "Secret Key"
7+
8+
path = os.path.abspath( os.path.dirname(__file__))
9+
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///" + os.path.join(path , 'database.sqlite')
10+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
11+
12+
db = SQLAlchemy(app)
13+
14+
class Crud(db.Model):
15+
id = db.Column(db.Integer , primary_key = True)
16+
name = db.Column(db.String(100))
17+
email = db.Column(db.String(100))
18+
phone = db.Column(db.String(100))
19+
20+
def __init__(self, name, email, phone):
21+
self.name = name
22+
self.email = email
23+
self.phone = phone
24+
25+
26+
27+
@app.route('/')
28+
def index():
29+
all_data = Crud.query.all()
30+
return render_template("index.html", all_data = all_data)
31+
32+
@app.route('/insert', methods = ['POST'])
33+
def insert():
34+
if request.method == 'POST':
35+
name = request.form['name']
36+
email = request.form['email']
37+
phone = request.form['phone']
38+
39+
my_data = Crud(name, email, phone)
40+
db.session.add(my_data)
41+
db.session.commit()
42+
43+
flash("student Inserted Successfully")
44+
return redirect(url_for('index'))
45+
46+
@app.route('/update', methods = ['POST'])
47+
def update():
48+
if request.method == "POST":
49+
my_date = Crud.query.get(request.form.get('id'))
50+
my_date.name = request.form['name']
51+
my_date.email = request.form['email']
52+
my_date.phone = request.form['phone']
53+
54+
db.session.commit()
55+
flash("student Updated Successfully")
56+
return redirect(url_for('index'))
57+
58+
@app.route('/delete/<id>/')
59+
def delete(id):
60+
my_data = Crud.query.get(id)
61+
db.session.delete(my_data)
62+
db.session.commit()
63+
64+
flash("student Data Deleted Successfully")
65+
return redirect(url_for('index'))
66+
67+
if __name__ == "__main__":
68+
app.run(debug = True)
69+
8 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Flask_SQLAlchemy==2.3.2
2+
Flask==1.0.2
3+
gunicorn==19.9.0
4+
itsdangerous==1.1.0
5+
Jinja2==2.11.2
6+
MarkupSafe==1.1.1
7+
Werkzeug==1.0.1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
6+
<meta charset="UTF-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
10+
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
11+
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
12+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
13+
14+
<title> {% block title %} {% endblock %} </title>
15+
16+
</head>
17+
18+
<body>
19+
20+
{% block body %} {% endblock %}
21+
22+
</body>
23+
24+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends 'base.html' %} {% block title %} Flask CRUD operation {% endblock %} {% block body %}
2+
3+
<div class="jumbotron p-3 bg-dark">
4+
<div class="well text-center text-light">
5+
<h2>AIML6m- Python Flask CRUD Web Application </h2>
6+
</div>
7+
</div>
8+
9+
{% endblock %}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<!-- Inheriting all linked files -->
2+
{% extends 'base.html' %} {% include 'header.html' %}
3+
4+
<!-- head section -->
5+
{% block title %} Flask CRUD operation {% endblock %}
6+
7+
8+
<!-- Body section -->
9+
{% block body %}
10+
11+
<div class="container">
12+
<div class="row">
13+
<div class="col-md-12">
14+
<div class="jumbotron p-3">
15+
16+
<h3>Manage <b>Students</b> <button type="button" class="btn btn-success float-right" data-toggle="modal" data-target="#mymodal">Add new Student</button> </h3>
17+
18+
{% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %}
19+
20+
<div class="alert alert-success alert-dismissable" role="alert">
21+
<button type="button" class="close" data-dismiss="alert" aria-label="close">
22+
<span aria-hidden="true">
23+
x
24+
</span>
25+
</button> {{message}}
26+
</div>
27+
28+
{% endfor %} {% endif %} {%endwith%}
29+
30+
<table class="table table-hover table-dark">
31+
32+
<tr>
33+
<th>ID</th>
34+
<th>Name</th>
35+
<th>Email</th>
36+
<th>Phone</th>
37+
<th>Action</th>
38+
</tr>
39+
40+
{% for row in all_data %}
41+
42+
<tr>
43+
<td>{{row.id}}</td>
44+
<td>{{row.name}}</td>
45+
<td>{{row.email}}</td>
46+
<td>{{row.phone}}</td>
47+
<td>
48+
<a href="/update/{{row.id}}" class="btn btn-info btn-xs" data-toggle="modal" data-target="#modaledit{{row.id}}">Edit</a>
49+
<a href="/delete/{{row.id}}" class="btn btn-danger btn-xs" onclick="return confirm('Are You sure To Delete ?')">Delete</a>
50+
</td>
51+
</tr>
52+
53+
<!-- Model for updating student data -->
54+
<div id="modaledit{{row.id}}" class="modal fade" role="dialog">
55+
<div class="modal-dialog">
56+
<div class="modal-content">
57+
<div class="modal-header">
58+
<h4 class="modal-title"> Update student Data </h4>
59+
</div>
60+
<div class="modal-body">
61+
<form action="{{url_for('update')}}" method="POST">
62+
63+
<div class="form-group">
64+
<label>Name</label>
65+
<input type="hidden" name="id" value="{{row.id}}">
66+
<input type="text" class="form-control" name="name" value="{{row.name}}" required="1">
67+
</div>
68+
69+
<div class="form-group">
70+
<label>Email</label>
71+
<input type="email" class="form-control" name="email" value="{{row.email}}" required="1">
72+
</div>
73+
74+
<div class="form-group">
75+
<label>Phone</label>
76+
<input type="number" class="form-control" name="phone" value="{{row.phone}}" required="1">
77+
</div>
78+
79+
<div class="modal-footer">
80+
<button class="btn btn-info" type="submit"> Update</button>
81+
<button class="btn btn-secondary" data-dismiss="modal">Close</button>
82+
</div>
83+
84+
</form>
85+
</div>
86+
</div>
87+
</div>
88+
</div>
89+
90+
{% endfor %}
91+
92+
</table>
93+
94+
</div>
95+
96+
<!-- Model for adding student -->
97+
<div id="mymodal" class="modal fade" role="dialog">
98+
<div class="modal-dialog">
99+
<div class="modal-content">
100+
<div class="modal-header">
101+
<h4 class="modal-title"> Add student </h4>
102+
</div>
103+
<div class="modal-body">
104+
<form action="{{url_for('insert')}}" method="POST">
105+
106+
<div class="form-group">
107+
<label>Name</label>
108+
<input type="text" class="form-control" name="name" required="1">
109+
</div>
110+
111+
<div class="form-group">
112+
<label>Email</label>
113+
<input type="email" class="form-control" name="email" required="1">
114+
</div>
115+
116+
<div class="form-group">
117+
<label>Phone</label>
118+
<input type="number" class="form-control" name="phone" required="1">
119+
</div>
120+
121+
<div class="modal-footer">
122+
<button class="btn btn-success" type="submit"> Add student </button>
123+
<button class="btn btn-secondary" data-dismiss="modal">Close</button>
124+
</div>
125+
126+
</form>
127+
</div>
128+
</div>
129+
</div>
130+
</div>
131+
132+
</div>
133+
</div>
134+
</div>
135+
136+
{% endblock %}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flask-sqlite
2+
Basic Flask User Signup and Login Template with SQLite Database
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ===========
2+
# Imports
3+
# ===========
4+
import sqlite3 as sql
5+
6+
# ========================
7+
# Connecting to database
8+
# ========================
9+
conn = sql.connect("test.db")
10+
cur = conn.cursor()
11+
12+
# =================
13+
# Query execution
14+
# =================
15+
query = ('''CREATE TABLE USERS2
16+
(username TEXT NOT NULL,
17+
email TEXT Primary key,
18+
password TEXT NOT NULL,
19+
contact INT);''')
20+
cur.execute(query)
21+
22+
# ================================
23+
# Closing connection to database
24+
# ================================
25+
conn.close()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# ===========
2+
# Imports
3+
# ===========
4+
from flask import Flask, render_template, request, redirect, url_for, jsonify
5+
import sqlite3 as sql
6+
from flask_cors import CORS, cross_origin
7+
8+
# ===================
9+
# Flask instance
10+
# ===================
11+
app = Flask(__name__)
12+
13+
# ======================
14+
# Allow Cross Origin
15+
# ======================
16+
@app.after_request # blueprint can also be app~~
17+
def after_request(response):
18+
header = response.headers
19+
header['Access-Control-Allow-Origin'] = '*'
20+
return response
21+
22+
23+
# ==================================
24+
# Insert data in database (SIGNUP)
25+
# ==================================
26+
def insertUser(username, email, password, contact):
27+
con = sql.connect("test.db")
28+
cur = con.cursor()
29+
phone = int(contact)
30+
query = ("""INSERT INTO USERS
31+
(username,email,password,contact)
32+
VALUES ('%s','%s','%s',%d)""" %
33+
(username, email, password, phone))
34+
cur.execute(query)
35+
con.commit()
36+
con.close()
37+
38+
39+
# =====================================
40+
# Validating data in database (LOGIN)
41+
# =====================================
42+
def validUser(email, password):
43+
con = sql.connect("test.db")
44+
cur = con.cursor()
45+
query = ("""SELECT * FROM USERS
46+
where email = '%s' and password = '%s'
47+
""" %
48+
(email, password))
49+
cur.execute(query)
50+
data = cur.fetchall()
51+
con.close()
52+
return data
53+
54+
55+
# ===================
56+
# Flask Routing
57+
# ===================
58+
59+
@app.route('/')
60+
def home():
61+
return "go at /signin or /signup"
62+
63+
# Login page
64+
@app.route('/signin/', methods=['GET', 'POST'])
65+
def login():
66+
if request.method == 'POST':
67+
rd = validUser(request.form['email'], request.form['password'])
68+
if rd:
69+
return "Sucessful Login"
70+
else:
71+
return "UnSucessful login"
72+
else:
73+
return render_template('index2.html')
74+
75+
76+
# Signup page
77+
@app.route('/signup/', methods=['GET', 'POST'])
78+
def signup():
79+
if request.method == 'POST':
80+
username = request.form['username']
81+
email = request.form['email']
82+
password = request.form['password']
83+
contact = request.form['contact']
84+
insertUser(username, email, password, contact)
85+
return redirect(url_for('login'))
86+
else:
87+
return render_template('index.html')
88+
89+
# api json
90+
@app.route('/sum', methods=['GET','POST'])
91+
def sum():
92+
sum = 0
93+
a = int(request.args.get('a'))
94+
b = int(request.args.get('b'))
95+
sum = a+b
96+
return jsonify(sum)
97+
98+
99+
# Always at end of file !Important!
100+
if __name__ == '__main__':
101+
app.secret_key = 'SURAJ_SECRET_KEY'
102+
app.debug = True
103+
app.run(host='0.0.0.0', port=5000)
104+

0 commit comments

Comments
 (0)