This app allows users to play a trivia quiz game based on questions and categories specified. Users can post new questions, rate existing questions, and play a trivia quiz from a given category or from all categories. This app uses a flask-based api in the backend, a postgresql database, and a react frontend. You will need python3, nodejs, and postgresql installed to run the app.
Navigate to the backend folder
Set-up a virtual environment and activate it:
python3 -m venv env
source env/bin/activateYou should see (env) before your command prompt now. (You can type deactivate to exit the virtual environment any time.)
Install the requirements:
pip install -U pip
pip install -r requirements.txtSet up your environment variables:
touch .env
echo FLASK_APP=flaskr >> .envInitialize and set up the database:
dropdb trivia
createdb trivia
psql trivia < trivia.psqlNavigate to the frontend folder
Install the requirements:
npm installTo start the backend, navigate to the backend folder and make sure you are in the virtual environment (you should see (env) before your command prompt). If not source /env/bin/activate to enter it.
Usage: flask runTo start the frontend, run the following command in another terminal from the frontend folder:
Usage: npm startNavigate to http://127.0.0.1:3000/ to see the app in action!
When running locally with the built in flask server, the base URL is as follows:
http://127.0.0.1:5000/Below are a list of errors that may be raised as part of the API
This is returned when the request is malformed in some way. (i.e. Required info is missing)
This is returned when the requested resource does not exist. (i.e. Attempting to view a page of questions that don't exist)
This is returned when the incorrect request method is specified at an endpoint. (i.e. Attempting to delete without specifying a specific question to delete)
This is returned when the request is unable to be fulfilled in some way. (i.e. Attempting to update a question that has previously been deleted)
This is returned when something there is a problem with the server.
Questions:
Retrieve a list of paginated questions
Example Request:
curl http://127.0.0.1:5000/questions?page=1Parameters:
- page (int) [optional]: Each page returns the next 10 results (default: 1)
Example Response:
{
"success": true,
"questions": [
{
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?",
"answer": "Apollo 13",
"category_id": 5,
"rating": 3,
"difficulty": 4
}
],
"total_questions": 1,
"current_category_id": null,
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
}
}Create a new question or search all questions
Example Request:
curl -X POST -H "Content-Type: application/json" -d '{"question": "What is the answer to life the universe and everything?", "answer": "42", "category_id": "1", "rating": "3", "difficulty": "5"}' http://127.0.0.1:5000/questionsParameters:
- question (str): The content of the question
- answer (str): The answer to the question
- category_id (int): The id of the category that the question belongs to
- rating (int): The rating of the question
- difficulty (int): The difficulty of the question
Example Response:
{
"success": true,
"created_question_id": 24
}Example Request:
curl -X POST -H "Content-Type: application/json" -d '{"search_term": "hanks"}' http://127.0.0.1:5000/questionsParameters:
- search_term (str): The string to search for in the questions
Example Response:
{
"success": true,
"questions": [
{
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?",
"answer": "Apollo 13",
"category_id": 5,
"rating": 3,
"difficulty": 4
}
],
"total_questions": 1,
"current_category_id": null,
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
}
}Update a question's rating
Example Request:
curl -X PATCH -H "Content-Type: application/json" -d '{"rating": "5"}' http://127.0.0.1:5000/questions/2Parameters:
- rating (int): The rating of the question
Example Response:
{
"success": true,
"updated_question_id": 2,
"old_rating": 3,
"new_rating": 5
}Delete a question
Example Request:
curl -X DELETE http://127.0.0.1:5000/questions/2Example Response:
{
"success": true,
"deleted_question_id": 2
}Categories:
Retrieve all categories
Example Request:
curl http://127.0.0.1:5000/categoriesExample Response:
{
"success": true,
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
}
}Create a new category
Example Request:
curl -X POST -F "name=People" -F "icon=@path/to/icon.svg" http://127.0.0.1:5000/categoriesNote: This request takes form data rather than json in order to accommodate the file upload for the category icon
Parameters:
- name (str): The name of the category
- icon: An svg file that is the icon for the category
Example Response:
{
"success": true,
"created_category_id": 7
}Retrieve all questions belonging to a specific category
Example Request:
curl http://127.0.0.1:5000/categories/5/questionsExample Response:
{
"success": true,
"questions": [
{
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?",
"answer": "Apollo 13",
"category_id": 5,
"rating": 3,
"difficulty": 4
}
],
"total_questions": 1,
"current_category_id": 5,
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
}
}Quizzes:
Create a new quiz in a given category
Example Request:
curl -X POST -H "Content-Type: application/json" -d '{"quiz_category_id": "5", "previous_question_ids": []}' http://127.0.0.1:5000/quizzesParameters:
- quiz_category_id (int): The id of the category that the question belongs to (0 represents all categories)
- previous_question_ids: A list of ints representing the ids of previous questions
Example Response:
{
"success": true,
"question": {
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?",
"answer": "Apollo 13",
"category_id": 5,
"rating": 3,
"difficulty": 4
}
}Note: This endpoint returns a single question representing a random question in the given category, rather than a list of questions
Users:
Retrieve all users
Example Request:
curl http://127.0.0.1:5000/usersExample Response:
{
"success": true,
"users": {
"1": "Alice",
"2": "Bob",
"3": "Charlie"
}
}Create a new user
Example Request:
curl -X POST -H "Content-Type: application/json" -d '{"username": "David"}' http://127.0.0.1:5000/usersParameters:
- username (str): The name of the user
Example Response:
{
"success": true
"created_user_id": 4
}Update a user's score
Example Request:
curl -X PATCH -H "Content-Type: application/json" -d '{"score": "2"}' http://127.0.0.1:5000/users/1Parameters:
- score (int): The score to update a user's score to
Example Response:
{
"success": true,
"updated_user_id": 1,
"old_score": 1,
"new_score": 3
}The backend has a testing suite to test all of the API endpoints
To set up the test database:
cd backend
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psqlTo run all the tests:
Usage: test_flaskr.pyUdacity's Full Stack Web Developer Nanodegree Program
Trivia API is licensed under the MIT license.


