-
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 backend
directory first ensure you are working using your created virtual environment.
To run the server, execute:
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
remember to check example.env file to see variables you need in your .env file because it is mandatory for the app to run properly
setting the FLASK_ENV
variable to development
will detect file changes and restart the server automatically.
setting the FLASK_APP
directs flask to use the flaskr
directory and the __init__.py
file to find the application.
###Testing
dropdb trivia_test
createdb trivia_test
psql trivia_test > trivia.psql
python test_flaskr.py
At present this app can only be run locally and is not hosted
- Base URL for backend:
http://127.0.0.1:5000/
, which is set as a proxy in the frontend configuration. - Base URL for frontend:
http://127.0.0.1:3000/
- Authentication: This version of the application does not require authentication or API keys.
Errors are returned as JSON objects in the following format:
{
"success": False,
"error": 500,
"message": "server error"
}
The API will return three error types when requests fail:
- 400: Bad Request
- 404: Resource Not Found
- 422: Not Processable
- 500: server error
- General:
- Returns a list of question objects, success value, and total number of questions
- Results are paginated in groups of 10.
- Sample:
curl http://127.0.0.1:5000/questions
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"current_categories": [
4,
5,
5,
6,
6,
4,
3,
3,
3,
2
],
"questions": [
{
"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": "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": "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?"
}
],
"success": true,
"totalQuestions": 17
}
- General:
- Returns a list of categories objects, success value.
- Sample:
curl http://127.0.0.1:5000/categories
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
- General:
- Returns a list of questions objects which includes the submitted searchTerm in question entity,total questions,list of categories of questions with the search term, success value.
- Sample:
curl http://127.0.0.1:5000/questions/search -X POST -H "Content-Type: application/json" -d '{"searchTerm": "palace"}'
{
"current_category": [
3
],
"questions": [
{
"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": 1
}
- General:
- Creates a new question using the submitted question, answer,difficulty and category. Returns the id of the created book, success value.
- Sample:
curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{"question": "when was javascript invented", "answer": "1995", "difficulty": "1", "category": "1"}'
{
"created": 24,
"success": true
}
- General:
- Returns a list of the question with the category with the id received from parameter category_id in the url, success value,current category which is the id in url and total questons.
- Sample:
curl http://127.0.0.1:5000/categories/1/questions
{
"current_category": 1,
"questions": [
{
"answer": "The Liver",
"category": 1,
"difficulty": 4,
"id": 20,
"question": "What is the heaviest organ in the human body?"
},
{
"answer": "Alexander Fleming",
"category": 1,
"difficulty": 3,
"id": 21,
"question": "Who discovered penicillin?"
},
{
"answer": "Blood",
"category": 1,
"difficulty": 4,
"id": 22,
"question": "Hematology is a branch of medicine involving the study of what?"
},
{
"answer": "1995",
"category": 1,
"difficulty": 1,
"id": 24,
"question": "when was javascript invented"
}
],
"success": true,
"total_questions": 4
}
- General:
- returns a new question to play a quiz using the submitted previous_questions to not return the same question in the same quiz,quiz_category which is a dictionary with category id and type. Returns the id of the created book, success value.
- Sample:
curl http://127.0.0.1:5000/quizzes -X POST -H "Content-Type: application/json" -d '{"previous_questions": [] ,"quiz_category": {"id": 1, "type": "Science"}}'
{
"question": {
"answer": "The Liver",
"category": 1,
"difficulty": 4,
"id": 20,
"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 your virtual environment and, run
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py