Add Lesson 6 Task 2

This commit is contained in:
Stanislav Mykhailenko 2023-01-07 16:16:00 +02:00
parent ef62307e08
commit 1ea0c35f22
GPG key ID: 1E95E66A9C9D6A36
4 changed files with 103 additions and 0 deletions

1
Lesson_6/Task 2/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
data.db

View file

@ -0,0 +1,50 @@
import sqlite3
from sqlite3 import Error
def init_conn(path):
conn = None
try:
conn = sqlite3.connect(path)
print ("Connection established!")
except Error as e:
print (e)
print ("Connection failed!")
return conn
def init_tables(connection):
sql = "CREATE TABLE IF NOT EXISTS hardware( id integer PRIMARY KEY, unit text NOT NULL, value text NOT NULL);"
connection.execute(sql)
def prepareDb(name):
conn = init_conn(name)
init_tables(conn)
conn.close()
def getData(name):
connection = init_conn(name)
sql = "SELECT * FROM hardware;"
cursor = connection.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
connection.close()
return rows
def generateDataHTMLTable(rows):
dataTable = "<table border='1'>"
for row in rows:
dataTable += "<tr>"
for cell in row:
dataTable += "<td>" + str(cell) + "</td>"
dataTable += "</tr>"
dataTable += "</table>"
return dataTable
def sendData(db, unit, value):
connection = init_conn(db)
sql = "INSERT INTO hardware(`unit`, `value`) VALUES(?, ?);"
args = (unit, value)
cursor = connection.cursor()
cursor.execute(sql, args)
connection.commit()
connection.close()

View file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Computer info</title>
</head>
<body>
<form action="/send">
<label for="cpu">CPU: </label><br>
<input type="checkbox" id="cpu" name="cpu"><br>
<label for="ram">RAM: </label><br>
<input type="checkbox" id="ram" name="ram"><br>
<label for="architecture">Architecture: </label><br>
<input type="checkbox" id="architecture" name="architecture"><br>
<label for="family">Family: </label><br>
<input type="checkbox" id="family" name="family"><br>
<button id="submit" type="submit">Submit</button>
</form>
Collected data:
<hr>
<div id="messages">
{{data|safe}}
</div>
</body>
</html>

View file

@ -0,0 +1,28 @@
from databaseWorker import *
from flask import Flask, render_template, redirect, request
import platform, psutil, os
app = Flask("Computer info")
prepareDb("data.db")
@app.route('/')
def index():
rows = getData("data.db")
return render_template("index.html", data=generateDataHTMLTable(rows))
@app.route('/send')
def send():
os.remove("data.db")
prepareDb("data.db")
if request.args.get('cpu'):
sendData("data.db", "CPU", platform.processor())
if request.args.get('ram'):
sendData("data.db", "RAM", str(psutil.virtual_memory()))
if request.args.get('architecture'):
sendData("data.db", "Architecture", str(platform.architecture()))
if request.args.get('family'):
sendData("data.db", "Family", platform.machine())
return redirect('/')
app.run(host='0.0.0.0', port=8081)