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. That was the motivation for this project. The project builds functionalities and test cases for the following:
- Display questions - both all questions and by category. Questions should show the question, category and difficulty rating by default and can show/hide the answer.
- Delete questions.
- Add questions and require that they include question and answer text.
- Search for questions based on a text query string.
- Play the quiz game, randomizing either all questions or within a specific category.
The project is a full stack web application with all the server and client functionalities in the backend and frontend folders.
- Install Required Software Python 3.7 - Follow instructions to install the latest version of python for your platform in the python docs
Virtual Environment - We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organized. Instructions for setting up a virtual environment for your platform can be found in the python docs
Postgres - If you don't already have it installed, see installation instructions for your operating system here:PostgreSQL Downloads
- Set up and Populate the Database With Postgres running, create a trivia database: createdb trivia From the backend folder in terminal, Populate the database using the trivia.psql file provided.run: psql trivia < trivia.psql
- Install Dependencies Once your virtual environment is setup and running, install the required dependencies by navigating to the /backend directory and running: pip install -r requirements.txt
- Start the Server Start the Server
In the backend directory, start the Flask server by running:
export FLASK_APP=flaskr export FLASK_ENV=development flask run
Once you have your virtual environment setup and running, install dependencies by naviging to the /backend directory and running:
pip install -r requirements.txt
The frontend directory contains a complete React frontend to consume the data from the Flask server. If you have prior experience building a frontend application, you should feel free to edit the endpoints as you see fit for the backend you design.
This project uses NPM to manage software dependencies. NPM Relies on the package.json file located in the frontend directory of this repository. After cloning, open your terminal and run:
npm install
View the Frontend README for more details.
To deploy the tests, run
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py
- Base URL: This application is currently hosted locally and runs at http://127.0.0.1:5000/ at the backend and http://127.0.0.1:3000 in the frontend
- Authentication: No API keys are required for this version, it may be required for subsequent versions.
GET /categories
Expectation:
- Returns a list of all the categories and a success value
CURL example: curl http://127.0.0.1:5000/categories
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
GET /questions
Expectations:
- Returns a list of all the categories and questions in the database, a success value, and the total number of questions in the database.
- Results are returned in groups of 10 (pagination).
CURL example: curl http://127.0.0.1:5000/questions
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"questions": [
{
"answer": "Apollo 13",
"category": 5,
"difficulty": 4,
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?"
},
{
"answer": "Tom Cruise",
"category": 5,
"difficulty": 4,
"id": 4,
"question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
},
{
"answer": "Maya Angelou",
"category": 4,
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"answer": "Edward Scissorhands",
"category": 5,
"difficulty": 3,
"id": 6,
"question": "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?"
},
{
"answer": "Muhammad Ali",
"category": 4,
"difficulty": 1,
"id": 9,
"question": "What boxer's original name is Cassius Clay?"
},
{
"answer": "Brazil",
"category": 6,
"difficulty": 3,
"id": 10,
"question": "Which is the only team to play in every soccer World Cup tournament?"
},
{
"answer": "Uruguay",
"category": 6,
"difficulty": 4,
"id": 11,
"question": "Which country won the first ever soccer World Cup in 1930?"
},
{
"answer": "George Washington Carver",
"category": 4,
"difficulty": 2,
"id": 12,
"question": "Who invented Peanut Butter?"
},
{
"answer": "Lake Victoria",
"category": 3,
"difficulty": 2,
"id": 13,
"question": "What is the largest lake in Africa?"
},
{
"answer": "The Palace of Versailles",
"category": 3,
"difficulty": 3,
"id": 14,
"question": "In which royal palace would you find the Hall of Mirrors?"
}
],
"success": true,
"total_questions": 20
}
DELETE /questions/int:id
Expectation:
- Deletes the question with the requested ID. Returns a success value and the ID of the deleted question.
CURL example:curl -X DELETE http://127.0.0.1:5000/questions/10
{
"deleted": 10,
"success": true
}
POST /questions
Expectations:
- Creates a new question using the parameters submitted by the client.
- Returns a success value, the id of the created question, the paginated list of questions to be updated in the frontend, and total questions number available in the database.
CURL example: curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{"question":"Who was the first woman computer programmer?", "answer": "Ada Lovelace","category" :"1", "difficulty":"2"}'
{
"created": 69,
"question_created": "Who was the first woman computer programmer?",
"questions": [
{
"answer": "Tom Cruise",
"category": "5",
"difficulty": 4,
"id": 4,
"question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
},
{
"answer": "Maya Angelou",
"category": "4",
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"answer": "Edward Scissorhands",
"category": "5",
"difficulty": 3,
"id": 6,
"question": "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?"
},
{
"answer": "Muhammad Ali",
"category": "4",
"difficulty": 1,
"id": 9,
"question": "What boxer's original name is Cassius Clay?"
},
{
"answer": "Brazil",
"category": "6",
"difficulty": 3,
"id": 10,
"question": "Which is the only team to play in every soccer World Cup tournament?"
},
{
"answer": "Uruguay",
"category": "6",
"difficulty": 4,
"id": 11,
"question": "Which country won the first ever soccer World Cup in 1930?"
},
{
"answer": "George Washington Carver",
"category": "4",
"difficulty": 2,
"id": 12,
"question": "Who invented Peanut Butter?"
},
{
"answer": "Lake Victoria",
"category": "3",
"difficulty": 2,
"id": 13,
"question": "What is the largest lake in Africa?"
},
{
"answer": "The Palace of Versailles",
"category": "3",
"difficulty": 3,
"id": 14,
"question": "In which royal palace would you find the Hall of Mirrors?"
},
{
"answer": "Agra",
"category": "3",
"difficulty": 2,
"id": 15,
"question": "The Taj Mahal is located in which Indian city?"
}
],
"success": true,
"total_questions": 61
}
POST /search
Expectations:
- Performs search for the requested search query.
- Returns the results, success value, total questions.
CURL Example: curl http://127.0.0.1:5000/search -X POST -H "Content-Type: application/json" -d '{"searchTerm":"whose"}'
{
"questions": [
{
"answer": "Maya Angelou",
"category": "4",
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
}
],
"success": true,
"total_questions": 3
}
GET /categories/int:id/questions
Expectation:
- Returns a list of questions for the requested category paginated in groups of 10, category, total_questions and success value
CURL example: curl http://127.0.0.1:5000/categories/5/questions
{
"current_category": "Entertainment",
"questions": [
{
"answer": "Apollo 13",
"category": 5,
"difficulty": 4,
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?"
},
{
"answer": "Tom Cruise",
"category": 5,
"difficulty": 4,
"id": 4,
"question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
},
{
"answer": "Edward Scissorhands",
"category": 5,
"difficulty": 3,
"id": 6,
"question": "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?"
}
],
"success": true,
"total_questions": 3
}
POST /quizzes
Expectation:
- Takes the question and the category and returns the next question in the same category
CURL example curl http://127.0.0.1:5000/quizzes -X POST -H "Content-Type: application/json" -d '{"quiz_category":{"type":"Entertainment", "id":"5"}, "previous_questions":[2, 15]}'
{
"question": {
"answer": "Tom Cruise",
"category": 5,
"difficulty": 4,
"id": 4,
"question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
},
"success": true
}
The API returns errors as JSON objects in the following format:
{
"success": False,
"error": 404,
"message": "Resource not found"
}
Below are some of the error types returned when the requests fail:
- 400: Bad Request
- 404: Resource Not Found
- 405: Method Not Allowed
- 422: Unprocessable resource
- 500: Internal Server Error