-
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 virual environment for your platform can be found in the python docs
-
PIP 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
-
Flask is a lightweight backend microservices framework. Flask is required to handle requests and responses.
-
SQLAlchemy is the Python SQL toolkit and ORM we'll use to handle the lightweight SQL database. You'll primarily work in
app.py
and can referencemodels.py
. -
Flask-CORS is the extension we'll use to handle cross-origin requests from our frontend server.
With Postgres running, create a trivia
database:
createdb trivia
Populate the database using the trivia.psql
file provided. From the backend
folder in terminal run:
psql trivia < trivia.psql
From within the ./src
directory first ensure you are working using your created virtual environment.
To run the server, execute:
flask run --reload
The --reload
flag will detect file changes and restart the server automatically.
These are the files you'd want to edit in the backend:
backend/flaskr/__init__.py
backend/test_flaskr.py
One note before you delve into your tasks: for each endpoint, you are expected to define the endpoint and response data. The frontend will be a plentiful resource because it is set up to expect certain endpoints and response data formats already. You should feel free to specify endpoints in your own way; if you do so, make sure to update the frontend or you will get some unexpected behavior.
- Use Flask-CORS to enable cross-domain requests and set response headers.
- Create an endpoint to handle
GET
requests for questions, including pagination (every 10 questions). This endpoint should return a list of questions, number of total questions, current category, categories. - Create an endpoint to handle
GET
requests for all available categories. - Create an endpoint to
DELETE
a question using a questionID
. - Create an endpoint to
POST
a new question, which will require the question and answer text, category, and difficulty score. - Create a
POST
endpoint to get questions based on category. - Create a
POST
endpoint to get questions based on a search term. It should return any questions for whom the search term is a substring of the question. - Create a
POST
endpoint to get questions to play the quiz. This endpoint should take a category and previous question parameters and return a random questions within the given category, if provided, and that is not one of the previous questions. - Create error handlers for all expected errors including 400, 404, 422, and 500.
You will need to provide detailed documentation of your API endpoints including the URL, request parameters, and the response body. Use the example below as a reference.
GET '/api/v1.0/categories'
- Fetches a dictionary of categories in which the keys are the ids and the value is the corresponding string of the category
- Request Arguments: None
- Returns: An object with a single key,
categories
, that contains an object ofid: category_string
key: value pairs.
{
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
}
GET '/api/v1.0/questions'
- Fetches a dictionary of questions in which the keys are the ids and there are columns of values for the questions, the answers, the difficulties and the categories
- Request Arguments: None
- Returns: An object with 5 key-value pairs for question, answer, category, difficulty and id.
{
"answer": "Maya Angelou",
"category": 4,
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"answer": "Muhammad Ali",
"category": 4,
"difficulty": 1,
"id": 9,
"question": "What boxer's original name is Cassius Clay?"
},
{
"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?"
}
DELETE '/api/v1.0/questions/<question_id>'
- Deletes a question of a specific id
- Request Arguments: the id of the question that is asked to be deleted
- Returns: An object with the result of the deletion and the id of the deleted question
{
"deleted": 26,
"success": true
}
POST '/api/v1.0/questions'
- Creates a new question
- Request Arguments: question, answer, difficulty, and category
- Returns: An object with the result of the creation and the id of the created question
{
"created": 28,
"success": true
}
POST '/api/v1.0/questions/search'
- Searches for the questions that include in them a specific term (inside question itself)
- Request Arguments: searchterm
- Returns: Returns the objects of key-value pairs for answer, category, difficulty, id and question for all the questions that include the searchterm. Also returns the status of the search result and the number of the questions found to satisfy the condition set.
{
"current_category": null,
"questions": [
{
"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?"
}
],
"success": true,
"total_questions": 2
}
GET '/api/v1.0/categories/<category_id>/questions'
- Gets all the questions of a specific category
- Request Arguments: None
- Returns: An object with the current category name as well as all the questions that are in this category. For each question included it gives the key-value pairs of answer,category, difficulty, id and question
{
"current_category": "History",
"questions": [
{
"answer": "Maya Angelou",
"category": 4,
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"answer": "Muhammad Ali",
"category": 4,
"difficulty": 1,
"id": 9,
"question": "What boxer's original name is Cassius Clay?"
},
{
"answer": "George Washington Carver",
"category": 4,
"difficulty": 2,
"id": 12,
"question": "Who invented Peanut Butter?"
},
{
"answer": "Scarab",
"category": 4,
"difficulty": 4,
"id": 23,
"question": "Which dung beetle was worshipped by the ancient Egyptians?"
}
],
"success": true,
"total_questions": 4
}
POST '/api/v1.0/quizzes'
- Returns a random question of a specific category that hasn't been already answered
- Request Arguments: previous_questions and category
- Returns: A question with all its info (answer, category, difficulty, id, question) of a specific category that wasn't already answered in the previous questions asked.
{
"question": {
"answer": "Jackson Pollock",
"category": 2,
"difficulty": 2,
"id": 19,
"question": "Which American artist was a pioneer of Abstract Expressionism, and a leading exponent of action painting?"
},
"success": true
}
Write at least one test for the success and at least one error behavior of each endpoint using the unittest library.
To deploy the tests, run
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py