Trivia App was created as an app for the Trivia project based on Udacity course, it has both backend endpoints to handle creation of questions and also returning random question for quizzes. Udacity is invested in creating bonding experiences for its employees and students. A bunch of team members got the idea to hold trivia on a regular basis and created a webpage to manage the trivia app and play the game, but their API experience is limited and still needs to be built out, hence the creation of this trivia API.
It was developed using Flask for backend which follows PEP8 guidelines for python, and ReactJS for the frontend.
You need to have python installed on your machine with Node and NPM (a package manager for node) as the backend uses Python Flask framework and the frontend uses ReactJS.
-
Install python if it's not installed from the official website
-
create a virtual environment in other to have an isolated system more info here
-
change directory to backend, install dependencies using pip
pip install -r requirements.txt
-
due to Flask-Cors depreciation some codes have to be corrected from the flask module, open
core.py
in{path to your env}/python/version/lib/python/site-packages/flask_cors/core.py
on line 322 and 342 change
collections.Iterable
tocollections.abc.Iterable
-
install a relational database management system (rdbms), recommended rdbms is postgresql you can download and get more info here
-
create a database for the backend
createdb trivia
-
you can choose to populated the trivia database with sample data provided or by yourself. to popluate the database change directory to
./backend
and runpsql trivia < trivia.psql
-
now to get the flask server running, if you intend to run it in a development environment, export the flask_env and flask_app variables first, change directory to backend
for window cmd
set FLASK_APP=flaskr set FLASK_ENV=development
for window powershell
$Env:FLASK_APP=flaskr $Env:FLASK_ENV=development
for linux
export FLASK_APP=flaskr export FLASK_ENV=development
-
you also need to set environment variables for the database user and the database password, the variables are
DATABASE_USER
andDATABASE_PASS
respectively.for windows more info here
for linux open your ~/.bashrc and add the following line of code to set the variables
export DATABASE_USER="yourDatabaseUsername" export DATABASE_PASS="yourDatabasePassword"
-
finally run the server with
flask run
-
install nodejs from the official website here
-
install dependencies, change directory to frontend and run
npm install
-
this app was created with react which provides a development server, change the script settings in
package.json
to your convenience if you are familiar with npm, run the development server with :npm run start
-
a production build can also be created using
npm run build
tests are located in the backend directory in test_flaskr.py file, to run tests:
-
change directory to backend
-
create a database for the tests
createdb trivia_test
-
populate the database with sample data
psql trivia_test < trivia.psql
-
then run the script with python
python test_flaskr.py
the documentation for the api is in the backend folder, link here