Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 4, 2023 06:21
Show Gist options
  • Save code-boxx/5d99b43c608da31b1952dabbd8bc37a0 to your computer and use it in GitHub Desktop.
Save code-boxx/5d99b43c608da31b1952dabbd8bc37a0 to your computer and use it in GitHub Desktop.
Python Flask Feedback System

SIMPLE PYTHON FLASK FEEDBACK SYSTEM

https://code-boxx.com/python-online-feedback-system/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create a templates folder, move S4_feedback.html inside.
    • Create a virtual environment - virtualenv venv
    • Activate the virtual environment - venv\scripts\activate (Windows) venv/bin/activate (Mac/Linux)
    • Install Flask pip install flask
    • Run python S1B_create.py to create the database.
    • Start the server python S3_server.py
  2. Access http://localhost in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) FEEDBACK
CREATE TABLE `feedback` (
`feedback_id` INTEGER NOT NULL,
`feedback_title` TEXT NOT NULL,
`feedback_desc` TEXT DEFAULT NULL,
PRIMARY KEY("feedback_id" AUTOINCREMENT)
);
-- (B) FEEDBACK QUESTIONS
CREATE TABLE `feedback_questions` (
`feedback_id` INTEGER NOT NULL,
`question_id` INTEGER NOT NULL,
`question_text` TEXT NOT NULL,
`question_type` TEXT NOT NULL DEFAULT 'R',
PRIMARY KEY ("feedback_id","question_id")
);
-- (C) FEEDBACK FROM USERS
CREATE TABLE `feedback_users` (
`user_id` INTEGER NOT NULL,
`feedback_id` INTEGER NOT NULL,
`question_id` INTEGER NOT NULL,
`feedback_value` TEXT NOT NULL,
PRIMARY KEY ("user_id","feedback_id","question_id")
);
-- (D) DUMMY FEEDBACK FORM
INSERT INTO `feedback`
(`feedback_title`, `feedback_desc`)
VALUES
("XYZ Course Feedback", "Optional Description.");
INSERT INTO `feedback_questions`
(`feedback_id`, `question_id`, `question_text`, `question_type`)
VALUES
(1, 1, "Are the course materials sufficient?", "R"),
(1, 2, "How likely are you to recommend this course to friends?", "R"),
(1, 3, "Any other feedback on the course?", "O");
# (A) LOAD PACKAGES
import sqlite3, os
from sqlite3 import Error
# (B) DATABASE + SQL FILE
DBFILE = "feedback.db"
SQLFILE = "S1A_feedback.sql"
# (C) DELETE OLD DATABASE IF EXIST
if os.path.exists(DBFILE):
os.remove(DBFILE)
# (D) IMPORT SQL
conn = sqlite3.connect(DBFILE)
with open(SQLFILE) as f:
conn.executescript(f.read())
conn.commit()
conn.close()
print("Database created!")
# (A) LOAD SQLITE MODULE
import sqlite3
DBFILE = "feedback.db"
# (B) HELPER - RUN SQL QUERY
def query(sql, data):
conn = sqlite3.connect(DBFILE)
cursor = conn.cursor()
cursor.execute(sql, data)
conn.commit()
conn.close()
# (C) HELPER - FETCH ALL
def select(sql, data=[]):
conn = sqlite3.connect(DBFILE)
cursor = conn.cursor()
cursor.execute(sql, data)
results = cursor.fetchall()
conn.close()
return results
# (D) GET QUESTIONS
def get(id):
return select("SELECT * FROM `feedback_questions` WHERE `feedback_id`=?", [id])
# (E) SAVE USER FEEDBACK
# uid : user id
# fid : feedback id
# feed : array of feedback data
def save(uid, fid, feed):
sql = "REPLACE INTO `feedback_users` (`user_id`, `feedback_id`, `question_id`, `feedback_value`) VALUES "
data = []
for qid, ans in feed.items():
sql = sql + "(?,?,?,?),"
data.extend([uid, fid, qid, ans])
sql = sql[:-1] + ";"
query(sql, data)
return True
# (A) INIT
# (A1) LOAD MODULES
from flask import Flask, render_template, request, make_response
import S2_lib as feedback
# (A2) FLASK SETTINGS + INIT
HOST_NAME = "localhost"
HOST_PORT = 80
app = Flask(__name__)
# app.debug = True
# (A3) FIXED USER ID & FEEDBACK ID FOR THIS DEMO
uid = 999
fid = 1
# (B) FEEDBACK HTML PAGE
@app.route("/")
def index():
# (B1) GET FEEDBACK QUESTIONS
questions = feedback.get(fid)
# (B2) RENDER HTML PAGE
return render_template("S4_feedback.html", qns=questions)
# (C) SAVE FEEDBACK FORM
@app.route("/save/", methods=["POST"])
def save():
feedback.save(uid, fid, request.form)
return make_response("Saved - Create your own thank you page...", 200)
# (D) START
if __name__ == "__main__":
app.run(HOST_NAME, HOST_PORT)
<!DOCTYPE html>
<html>
<head>
<title>Feedback Page</title>
<meta charset="utf-8">
<style>
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
.feed-form {
max-width: 500px;
padding: 20px;
border: 1px solid #ddd;
background: #f2f2f2;
}
.feed-qn {
padding: 10px 0;
font-weight: 700;
}
.feed-r, .feed-o {
width: 100%;
margin-bottom: 20px;
}
.feed-o {
padding: 10px;
border: 0;
}
.feed-go {
padding: 10px 20px;
font-weight: 700;
border: 0;
color: #fff;
background: #006aee;
cursor: pointer;
}
</style>
</head>
<body>
<form method="post" class="feed-form" action="save/" target="_blank">
{% for q in qns %}
<!-- (A) QUESTION -->
<div class="feed-qn">{{ q[2] }}</div>
<!-- (B) ANSWER -->
{% if q[3] == "R" %}
<div class="feed-r">
{% for i in range(5) %}
<input type="radio" name="{{ q[1] }}" value="{{ i+1 }}"{{ " checked" if i==2 else "" }}>
{% endfor %}
</div>
{% else %}
<input type="text" name="{{ q[1] }}" class="feed-o" required>
{% endif %}
{% endfor %}
<!-- (C) SUBMIT -->
<input type="submit" value="Save" class="feed-go">
</form>
</body>
</html>
md templates
move S4_feedback.html templates
virtualenv venv
call venv\Scripts\activate
pip install flask
python S1B_create.py
python S3_server.py
mkdir -m 777 templates
mv ./S4_feedback.html ./templates
virtualenv venv
source "venv/bin/activate"
pip install flask
python S1B_create.py
python S3_server.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment