Python Flask quiz app optimized for memorization training
- Based on Python Flask web framework
- Session and question management via PostgreSQL (using psycopg)
- Keeps asking questions until answered correctly
- Or use Finish button ends session with score summary
- Run web app and DB locally or in cloud
Set up the app running locally by installing an environment for PostgreSQL and Python Flask. Once it's working and customized, you can choose to upload it to a web app service for remote connection.
Install latest Python, Flask, and psycopg library (for PostgreSQL). Tested with a Debian image in WSL2.
E.g. on Debian:
sudo apt install python3
sudo apt install python3-flask
sudo apt install python3-psycopg
Install PostgreSQL locally or set up a cloud service.
E.g. on Debian (in WSL):
sudo apt install postgresql postgresql-contrib
# create a postgres user and give it a password
sudo passwd postgres
sudo systemctl enable postgresql
sudo service postgresql start
Create a PostgreSQL user for the quiz app. Enter the psql shell with "sudo -u postgres psql" and run create a SQL user for the database:
CREATE USER quizzer with PASSWORD 'your password here'
From here you can use the .sql files to create the database, e.g.
sudo -i -u postgres psql < create_db.sql
sudo -i -u postgres psql -d quiz_db < create_schema.sql
Create a local .env file for the Python programs to use to get PostgreSQL connection details:
HOST='localhost'
DATABASE='quiz_db'
DB_USERNAME='quizzer'
DB_PASSWORD='your password here'
If your PostgreSQL server is remote or a managed cloud server replace 'localhost' with the hostname (e.g. 'mydbserver.postgres.database.azure.com')
- If using Azure Database for PostgreSQL flexible server for the database, set a firewall rule to allow local development if needed, and allow public access from any Azure service with Azure. E.g.
To make a quiz, create a .csv file of the following format:
question|answer(s)|<number of answers>
If there is more than one possible answer, represent the answer column like a Python list "[valid answer 1,valid answer 2, valid answer 3]". If the user needs to enter more than one answer, increment the number of answers column. Answer validation is case-insensitive.
Examples:
Alabama|Montgomery|1
Alaska|Juneau|1
What is the supreme law of the land?|[the Constitution,Constitution]|1
What does the Constitution do?|[sets up the government,defines the government,protects basic rights of Americans]|1
What are two rights in the Declaration of Independence?|[life,liberty,pursuit of happiness]|2
Once you have a quiz, save it as a .csv file in the subjects folder. Set the file name to be the quiz title. Then load the question using load_quiz.py quiz\ title. E.g.
./load_quiz.py World\ Capitals
The Python script will find the file in the subjects folder and add the .csv extension.
cd to flask directory and run:
flask run
E.g. In Azure App Service. Make sure you have a remotely accessible PostgreSQL server running (e.g. Azure Database for PostgreSQL flexible server) with the schema, database user, in place and one or more quizzes loaded.
You can deploy from a local machine if you have Azure CLI installed, though I find it easier to deploy from Azure Cloud Shell which is an Azure Linux instance with CLI already set up.
Clone the repo and cd to the flask folder. Decide which sku and data center location to use. Create an Azure resource group. Then create the app with az webapp up. E.g.:
az group create --name my-quiz-rg --location westus3
az webapp up --runtime PYTHON:3.9 --sku B1 --logs -g my-quiz-rg -l westus3 --name my-quiz-app
Notes:
- Use the -v argument to set the environment variables for PostgreSQL (HOST, DATABASE, DB_USERNAME, DB_PASSWORD), or if you don't want to specify them on the command line, the app will deploy, but won't run successfully in App Service until you set the environment variables to the correct values to connect to the PostgreSQL database and restart the app. You can set this for the web in the Azure Portal:
- The sku you select will affect the monthly cost. Use F1 for the free plan. See App Service on Linux pricing for details.