- Full Stack API Trivia App
- This is a web app, which tries to check the General Knowledge of the participants across six major categories, given below:
- Science
- Art
- Geography
- History
- Entertainment
- Sports
- To help in developing the API for the underlying web app, so that it gets connected to a database, and can be used by the front end for various purposes such as:
- Showing all the questions
- Segregating questions by categories
- Adding new questions
- Deleting questions
- Showing Questions by Categories
- Searching for Questions
- At the end of the API development , the web application should be able 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.
- Play the quiz game, randomizing either all questions or within a specific category.
Though this is a simple project, I have used a lot of code style conventions, espcially the PEP 8 guidelines and Google Python Style Guide.
- Intermediate level understanding of Python
- Understanding of PostgreSQL database
- Understanding of RESTful APIs
- Understanding of HTML, CSS, and JavaScript
- Understanding of Flask
- Understanding of SQLAlchemy
The major dependencies of this project are:
- Python version 3.10.2
- FLask 2.x.x - a lightweight backend microservices framework. Flask is required to handle requests and responses.
- PostgreSQL version 13.5 as the database.
- SQLAlchemy version 1.4.31, which is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
- Python virtual environment for managing package installations, a great primer for these can be found in this short realpython's course.
- FLask-CORS - a Flask extension that allows cross-origin resource sharing, thus , making cross-origin AJAX possible.
- Since this project's main focus is on APIs, the frontend has already been constructed and we can use the frontend dependencies by going to the
frontend
directory and running the command
npm install
- To install the backend dependencies, we need to go to the
backend
directory and run the command
pip install -r requirements.txt
We can install the database by first going to the terminal psql
shell and creating a database called trivia
. Then we can run the following comand from the backend
directory:
psql trivia < trivia.sql
- To run the server, we need to go to the
backend
directory and run the command
export FLASK_APP=flaskr
export FLASK_ENV=development
export FLASK_DEBUG=true
flask run
The app runs on localhost:5000
and the FLASK_DEBUG
environment variable is set to true
.
-
The API returns the following error messages:
400
- Bad Request404
- Not Found405
- Method Not Allowed422
- Unapproachable
-
Errors are returned in the json format, a sample of which is given below:
{
"success": false,
"error": 404,
"message": "Not Found"
}
This endpoint returns a list of all the categories in the dictionary format, where category id is the key and category type is the value.
Example request format:
GET /categories
Example json response format:
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
This endpoint returns questions stored in the database in the paginated format, where a page size is of 10 questions. this has been done so as to avoid the large number of questions that are stored in the database and to make the API more efficient and frontend more responsive.
Example request format:
GET /questions
Example json response format:
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"questions": [
{
"category": 4,
"category_type": "History",
"difficulty": 2,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"category": 4,
"category_type": "History",
"difficulty": 1,
"question": "What boxer's original name is Cassius Clay?"
},
{
"category": 5,
"category_type": "Entertainment",
"difficulty": 4,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?"
},
{
"category": 5,
"category_type": "Entertainment",
"difficulty": 4,
"question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
},
{
"category": 5,
"category_type": "Entertainment",
"difficulty": 3,
"question": "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?"
},
{
"category": 6,
"category_type": "Sports",
"difficulty": 3,
"question": "Which is the only team to play in every soccer World Cup tournament?"
},
{
"category": 6,
"category_type": "Sports",
"difficulty": 4,
"question": "Which country won the first ever soccer World Cup in 1930?"
},
{
"category": 4,
"category_type": "History",
"difficulty": 2,
"question": "Who invented Peanut Butter?"
},
{
"category": 3,
"category_type": "Geography",
"difficulty": 2,
"question": "What is the largest lake in Africa?"
},
{
"category": 3,
"category_type": "Geography",
"difficulty": 3,
"question": "In which royal palace would you find the Hall of Mirrors?"
}
],
"success": true,
"total_questions": 21
}
This endpoint deletes a question from the database, which was specified by its id in the url. It returns the success message in the json format and then repaginates the questions.
Example request format:
DELETE /questions/<int:question_id>
Example json response format:
{
"deleted": 23,
"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": "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?"
},
{
"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
}
This endpoint does two things
- Posts a new question if there is no
searchTerm
in the request body . - Searchs for a question if there is
searchTerm
in the request body.
This endpoint adds a question to the database. It returns the success message in the json format and then repaginates the questions.
Example request format:
POST /questions
Example json resquest body in json format:
{
"category": 1,
"question": "What is the Newton's third law",
"answer": "To every action, there is an equal and opposite reaction",
"difficulty": 3
}
Example json response format:
{
"created": 26,
"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": "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?"
},
{
"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": 21
}
Example json resquest body in json format:
{
"searchTerm": "India"
}
Example json response format:
{
"questions": [
{
"answer": "Agra",
"category": 3,
"difficulty": 2,
"id": 15,
"question": "The Taj Mahal is located in which Indian city?"
}
],
"success": true,
"total_questions": 1
}
This endpoint returns a list of questions based on the category id.It returns an array of questions under that particular category id and totla number of questions in that category. Example request format:
GET /categories/1/questions
Example json response format:
{
"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": "To every action, there is an equal and opposite reaction",
"category": 1,
"difficulty": 3,
"id": 26,
"question": "What is the Newton's third law"
}
],
"success": true,
"total_questions": 4
}
This endpoint allows user to play a quix game, either by a category or by all.
The application can be either hosted on the remote server and/or cloud or can be hosted locally. If no domain is configured then the app runs in the local environment.
- The base URRs are:
- for backend
http://127.0.0.1:5000/
- for frontend
http://127.0.0.1:3000/
- for backend
Nikhil Pandey is the main author of this project. he has contributed to the project by writing the backend code in the following two code files
backend/flasr/__init__.py
backend/test_flaskr.py
Apart from that he has written the documentation which is given in theREADME.md
file, which you are reading now.
This project is a part of the Udacity Full Stack Web Developer Nanodegree program. So obviously it has used a lots of resources from Udacity, espcailly the lecture notes and exercise codes. Apart from the these two, the Q&A forum for this trivia project is also a source of information as well as codes for this project.