-
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:
createbd 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.
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.
GET '/categories'
- Returns: All categories
- URI:- http://127.0.0.1:5000/categories
- Response
{
"categories": {
"1": "history",
"2": "science",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
GET '/questions'
-
Fetches:
- A dictionary of all categories
- The current category
- A list of questions - 10 questions per page
- The number total of questions
-
Request arguments:
page
(integer) - The current page
-
Returns: An object with these keys:
categories
: A dictionary of categoriesquestions
: A list of questions - 10 questions per pagecurrent_category
: The current categorysuccess
: The success flagtotal_questions
: The total of questions
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"current_category": null,
"questions": [
{
"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?"
},
{
"answer": "Escher",
"category": 2,
"difficulty": 1,
"id": 16,
"question": "Which Dutch graphic artist–initials M C was a creator of optical illusions?"
},
{
"answer": "Mona Lisa",
"category": 2,
"difficulty": 3,
"id": 17,
"question": "La Giaconda is better known as what?"
}
],
"success": true,
"total_questions": 34
}
DELETE '/questions/<int:id>'
- Returns: Deleted question with given ID.
- URI:- http://127.0.0.1:5000/questions/12
- Response
{
"id": 12,
"message": "Question deleted successfully ",
"success": true
}
POST '/questions'
- Inserting a new question
- Request arguments:
answer
(string) - The answercategory
(string) - The question categorydifficulty
(string) - The question difficultyquestion
(string) - The question
- URI:- http://127.0.0.1:5000/questions
- JSON file format
{
"answer": "blue",
"category": "2",
"difficulty": 1,
"id": 10,
"question": "What is the colour of sky"
}
- Response
{
"question": {
"answer": "blue",
"category": "2",
"difficulty": 1,
"id": 17,
"question": "What is the colour of sky"
},
"success": true
}
POST '/questions/search'
- Search a question.
- Request arguments:
searchTerm
(string) - The term to search
- Returns: An object with these keys:
searchTerm
: The search term object containing the question searched for, its answer, category and difficultycurrent_category
: The current categorysuccess
: The success flagtotal_questions
: The total number of questions with the search term
- JSON file format
{
"searchTerm": "title"
}
- Response
{
"SearchTerm": [
{
"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?"
}
],
"current_category": null,
"success": true,
"total_questions": 1
}
GET '/categories/<int:category_id>/questions'
-
Fetches a list of questions based on category.
-
Request arguments:
category_id
(integer): The category id
-
Returns: An object with these keys:
current_category
: The current categoryquestions
: A list of questions in that categorysuccess
: The success flagtotal_questions
: The total of number questions in that category
-
Response for URI http://127.0.0.1:5000/categories/2/questions
{
"current_categories": 2,
"questions": [
{
"answer": "Escher",
"category": 2,
"difficulty": 1,
"id": 16,
"question": "Which Dutch graphic artist–initials M C was a creator of optical illusions?"
},
{
"answer": "Mona Lisa",
"category": 2,
"difficulty": 3,
"id": 17,
"question": "La Giaconda is better known as what?"
},
{
"answer": "One",
"category": 2,
"difficulty": 4,
"id": 18,
"question": "How many paintings did Van Gogh sell in his lifetime?"
},
{
"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?"
}
],
"succeed": true,
"total_questions": 4
}
POST '/quizzes'
- Fetches a question to play the quiz.
- Request arguments:
quiz_category
: A dictionary of the quiz category with thetype
and theid
.previous_ids
: A list of strings of the previous questions ids
- Returns: An object with these keys:
previousQuestion
- An array of previous questionsquestion
: A dictionary of the question to playsuccess
: The success flag
- JSON file format
{
"quiz_category": {
"type": "Science",
"id": 1
},
"previous_questions": []
}
- Response
{
"previousQuestions": [],
"question": {
"answer": "The Liver",
"category": 1,
"difficulty": 4,
"question": "What is the heaviest organ in the human body?"
},
"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
- Returns an object with these keys:
success
,error
andmessage
.
{
"success": false,
"error": 400,
"message": "bad request"
}
- Returns an object with these keys:
success
,error
andmessage
.
{
"success": false,
"error": 404,
"message": "resource not found"
}
- Returns an object with these keys:
success
,error
andmessage
.
{
"success": false,
"error": 405,
"message": "method not allowed"
}
- Returns an object with these keys:
success
,error
andmessage
.
{
"success": false,
"error": 422,
"message": "unprocessable"
}