Skip to content

Commit 3be6858

Browse files
authored
Add files via upload
1 parent f06486b commit 3be6858

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed

PersonDB.sql

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
DROP DATABASE IF EXISTS PersonDB;
2+
CREATE DATABASE IF NOT EXISTS PersonDB
3+
CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
4+
USE PersonDB;
5+
6+
DROP TABLE IF EXISTS PersonList;
7+
CREATE TABLE IF NOT EXISTS PersonList(
8+
id INT PRIMARY KEY AUTO_INCREMENT,
9+
lastname VARCHAR(24) NOT NULL,
10+
firstname VARCHAR(24) NOT NULL,
11+
UNIQUE (firstname,lastname),
12+
gender CHAR(1) NOT NULL DEFAULT 'm',
13+
h INT NOT NULL DEFAULT 160,
14+
age INT NOT NULL DEFAULT 24,
15+
CONSTRAINT chk_gender CHECK (gender IN ('m', 'f')),
16+
CONSTRAINT chk_age CHECK (age>=17 AND age<=70),
17+
CONSTRAINT chk_h CHECK(h>=155 AND h<=220)
18+
) ENGINE=InnoDB;
19+
20+
INSERT INTO PersonList(firstname, lastname, gender, age, h) VALUES
21+
('Biagio Rosario', 'Greco', 'm', 40, 171),
22+
('Vincenzo', 'Ravaglioli', 'm', 34, 165),
23+
('Torquato', 'Camilleri', 'm', 21, 162),
24+
('Alberico', 'Vianello', 'm', 19, 176),
25+
('Arnaldo', 'Bianchi', 'm', 45, 164),
26+
('Antonio', 'Oliva', 'm', 43, 179),
27+
('Beatrice', 'Petrella', 'f', 52, 170),
28+
('Adelasia', 'Solari', 'f', 31, 179),
29+
('Marta', 'Bignardi', 'f', 39, 160),
30+
('Daria', 'Battimelli', 'f', 41, 165),
31+
('Fiamma', 'Scala', 'f', 21, 168),
32+
('Donatella', 'Rossi', 'f', 37, 196),
33+
('Daria', 'Tuzzolino', 'f', 52, 174),
34+
('Matilda', 'Catapano', 'f', 19, 199),
35+
('Edoardo', 'Tognazzi', 'm', 54, 184),
36+
('Stefano', 'Da Re', 'm', 22, 192),
37+
('Gemma', 'Cortese', 'f', 34, 184),
38+
('Bruno', 'Fusani', 'm', 34, 174),
39+
('Liana', 'Surian', 'f', 54, 191),
40+
('Amalia', 'Baresi', 'f', 42, 183),
41+
('Patrizio', 'Saraceno', 'm', 53, 163);

flaskstart.bat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@echo off
2+
REM file: flaskstart.bat
3+
cd %~dp0
4+
CLS
5+
REM *** Envairoment VAR SETTING ***
6+
REM app is file: 'webapp.py'. this file contains routing
7+
set FLASK_APP=webapp
8+
REM development mode (print PIN for edit with python on browser)
9+
set FLASK_ENV=development
10+
REM Host address setting
11+
set FLASK_RUN_HOST=localhost
12+
REM PORT setting
13+
set FLASK_RUN_PORT=8000
14+
REM Start web page (with Vivaldi Browser) after lauch this file
15+
"C:\Program Files\Vivaldi\Application\vivaldi.exe" "http://%FLASK_RUN_HOST%:%FLASK_RUN_PORT%"
16+
CLS
17+
REM start FLASK
18+
flask run --port=%FLASK_RUN_PORT%
19+
REM in alternative try to run
20+
REM python "%FLASK_APP%.py"

mysql_config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"host": "localhost",
3+
"port": 3306,
4+
"user": "root",
5+
"password": "",
6+
"db": "PersonDB"
7+
}

templates/mainView.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{# Main View Page #}
2+
<!doctype html>
3+
<html lang="it">
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, shrink-to-fit=no, user-scalable=yes, initial-scale=1.0, maximum-scale=4.0, minimum-scale=0.5" />
7+
<link
8+
type="image/png"
9+
rel="icon"
10+
href="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/joypixels/291/rocket_1f680.png"
11+
/>
12+
<title>{{titolo}}</title>
13+
<link
14+
rel="stylesheet"
15+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
16+
integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor"
17+
crossorigin="anonymous"
18+
/>
19+
{% block headOfPage %}
20+
{% endblock headOfPage %}
21+
</head>
22+
<body>
23+
{% block bodyOfPage %}
24+
{% endblock bodyOfPage %}
25+
</body>
26+
</html>

templates/showQuery.html

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{# Show Query #}
2+
{% extends 'mainView.html' %}
3+
4+
{% block headOfPage %}
5+
<script async>
6+
const deletePerson = async (id) => {
7+
const affected = (confirm(`Delete {{sql_fields[0]}}: ${id}?`))
8+
? await fetch(
9+
`./api/${id}`, {
10+
method: 'DELETE'
11+
}
12+
).then(
13+
response => response.json()
14+
).then(
15+
j => parseInt(j.affected_rows)
16+
)
17+
: 0;
18+
if(affected === 0)
19+
alert('Problema Eliminazione');
20+
else
21+
location.reload();
22+
}
23+
</script>
24+
{% endblock headOfPage %}
25+
26+
{% block bodyOfPage %}
27+
<table class="table table-striped">
28+
<thead class="table-dark">
29+
<tr>
30+
<td>&#x1f6e0;</td>
31+
{% for cell in sql_fields[1:] %}
32+
<th scope="col">{{ cell }}</th>
33+
{% endfor %}
34+
</tr>
35+
</thead class="table-warring">
36+
<form>
37+
<tr>
38+
<td>
39+
<input type="submit" value="&#x2714;" class="btn btn-outline-success" />
40+
</td>
41+
{% for cell in sql_fields[1:] %}
42+
<th scope="col">
43+
<input
44+
type="text"
45+
name="{{cell}}"
46+
required
47+
class="form-control"
48+
placeholder="insert {{cell}}"
49+
/>
50+
</th>
51+
{% endfor %}
52+
</tr>
53+
</form>
54+
<thead>
55+
<tbody>
56+
{% for row in sql_tab %}
57+
<tr>
58+
<th>
59+
<input
60+
type="button"
61+
onclick="javascript:deletePerson({{row[0]}})"
62+
class="btn btn-outline-danger"
63+
value="&#x274c;"
64+
/>
65+
</th>
66+
{% for cell in row[1:] %}
67+
<td>{{ cell }}</td>
68+
{% endfor %}
69+
</tr>
70+
{% endfor %}
71+
</tbody>
72+
</table>
73+
{% endblock bodyOfPage %}

webapp.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from flask import Flask, render_template, request
2+
from flask_mysqldb import MySQL
3+
import json
4+
5+
app = Flask(__name__)
6+
7+
'''
8+
Load from json file the configuration for MySQL R-DBMS data
9+
'''
10+
for prop, val in json.load(open('mysql_config.json')).items():
11+
app.config[f'''MYSQL_{prop.upper()}'''] = val
12+
13+
mysql = MySQL(app)
14+
15+
'''
16+
execute the query and return Result-Set
17+
'''
18+
def findPersons(fields):
19+
_q = f'''
20+
SELECT
21+
{', '.join(fields)}
22+
FROM
23+
PersonList
24+
WHERE
25+
1
26+
ORDER BY
27+
h ASC;
28+
'''
29+
_cur = mysql.connection.cursor()
30+
_num_rows = _cur.execute(_q)
31+
_rs = _cur.fetchall()
32+
_cur.close()
33+
return _rs
34+
35+
'''
36+
NB: the temlate files must be into ./templates dir
37+
'''
38+
@app.route('/')
39+
def home():
40+
fields = (
41+
'id',
42+
'firstname',
43+
'lastname',
44+
'gender',
45+
'h',
46+
'age'
47+
)
48+
return render_template(
49+
'showQuery.html',
50+
titolo='Show Person List',
51+
sql_tab=findPersons(fields),
52+
sql_fields=fields
53+
)
54+
55+
@app.route('/api/<int:id>', methods=['DELETE','GET'])
56+
def deletePerson(id):
57+
#_id = request.values.get('id', default=-1, type=int)
58+
_q = f'''DELETE FROM PersonList WHERE id={id};'''
59+
_cur = mysql.connection.cursor()
60+
_affected_rows = _cur.execute(_q)
61+
mysql.connection.commit()
62+
_cur.close()
63+
return app.response_class(
64+
status=200,
65+
mimetype='application/json',
66+
response = json.dumps( { 'affected_rows' : _affected_rows } )
67+
)
68+
69+
if __name__ == '__main__':
70+
app.run(debug=True)
71+
'''
72+
app.run(
73+
debug=True,
74+
host='localhost'
75+
port=8000
76+
)
77+
'''

0 commit comments

Comments
 (0)