The Trivia API app has the following functionalities. It let's users to
- 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.
Follow instructions to install the latest version of python for your platform in the python docs
We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organaized. Instructions for setting up a virual enviornment for your platform can be found in the python docs
Once you have your virtual environment setup and running, install dependencies by naviging to the /backend
directory and running:
pip install -r requirements.txt
This will install all of the required packages we selected within the requirements.txt
file.
-
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 handle the lightweight sqlite database. You'll primarily work in app.py and can reference models.py.
-
Flask-CORS is the extension we'll use to handle cross origin requests from our frontend server.
With Postgres running, restore a 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
Setting the FLASK_ENV
variable to development
will detect file changes and restart the server automatically.
Setting the FLASK_APP
variable to flaskr
directs flask to use the flaskr
directory and the __init__.py
file to find the application.
From within the frontend
directory execute
npm install
npm start
- 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 question using a question ID.
- 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 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
Fetches a dictionary of all available categories
- Request parameters: none
- Example response:
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
GET \questions?page=<page_number>
Fetches a paginated dictionary of questions of all available categories
- Request parameters (optional): page:int
- Example response:
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"current_category": null,
"questions": [
{
"answer": "Maya Angelou",
"category": 4,
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"answer": "Escher",
"category": 2,
"difficulty": 1,
"id": 16,
"question": "Which Dutch graphic artist\u2013initials M C was a creator of optical illusions?"
}
],
"success": true,
"total_questions": 2
}
DELETE /questions/<question_id>
Delete an existing questions from the repository of available questions
- Request arguments: question_id:int
- Example response:
{
"deleted": "28",
"success": true
}
POST /questions
Add a new question to the repository of available questions
- Request body: {question:string, answer:string, difficulty:int, category:string}
- Example response:
{
"created": 29,
"success": true
}
POST /questions/search
Fetches all questions where a substring matches the search term (not case-sensitive)
- Request body: {searchTerm:string}
- Example response:
{
"current_category": null,
"questions": [
{
"answer": "Lisbon",
"category": 2,
"difficulty": 1,
"id": 29,
"question": "What is the capital of Portugal?"
}
],
"success": true,
"total_questions": 1
}
GET /categories/<int:category_id>/questions
Fetches a dictionary of questions for the specified category
- Request argument: category_id:int
- Example response:
{
"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?"
},
],
"success": true,
"total_questions": 2
}
POST /quizzes
Fetches one random question within a specified category. Previously asked questions are not asked again.
- Request body: {previous_questions: arr, quiz_category: {id:int, type:string}}
- Example response:
{
"question": {
"answer": "The Liver",
"category": 1,
"difficulty": 4,
"id": 20,
"question": "What is the heaviest organ in the human body?"
},
"success": true
}
To run the tests, run
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py
- Udacity The project was developed as a part of Udacity's Full Stack Nanodegree Program.
Playing trivia