Skip to content

Trivia API is an app that let's users play the trivia game. I developed this project as a part of Udacity's Full Stack Nanodegree Program.

Notifications You must be signed in to change notification settings

siddarthaThentu/Trivia_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Full Stack API Final Project

Home Page homepage

Full Stack Trivia

The Trivia API app has the following functionalities. It let's users to

  1. 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.
  2. Delete questions.
  3. Add questions and require that they include question and answer text.
  4. Search for questions based on a text query string.
  5. Play the quiz game, randomizing either all questions or within a specific category.

Full Stack Trivia API Backend

Getting Started

Installing Dependencies

Python 3.7

Follow instructions to install the latest version of python for your platform in the python docs

Virtual Enviornment

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

PIP Dependencies

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.

Key Dependencies
  • 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.

Database Setup

With Postgres running, restore a database using the trivia.psql file provided. From the backend folder in terminal run:

psql trivia < trivia.psql

Running the backend server

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.

Running the frontend server

From within the frontend directory execute

npm install
npm start

Tasks

  1. Use Flask-CORS to enable cross-domain requests and set response headers.
  2. 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.
  3. Create an endpoint to handle GET requests for all available categories.
  4. Create an endpoint to DELETE question using a question ID.
  5. Create an endpoint to POST a new question, which will require the question and answer text, category, and difficulty score.
  6. Create a POST endpoint to get questions based on category.
  7. 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.
  8. 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.
  9. Create error handlers for all expected errors including 400, 404, 422 and 500.

API

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
}

Testing

To run the tests, run

dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py

License

License: MIT

Author

Acknowledgements

  • Udacity The project was developed as a part of Udacity's Full Stack Nanodegree Program.

Screenshots

Playing trivia

play_trivia

play_trivia2

Search a question search

Add a question add

About

Trivia API is an app that let's users play the trivia game. I developed this project as a part of Udacity's Full Stack Nanodegree Program.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published