Thursday, 20 November 2025

How to run Lab - Manual

๐ŸŽ“ FASTAPI STUDENT LAB MANUAL + EXERCISE BOOK

For Undergraduate Students — 12 Modules

This manual contains:

  • Module-wise exercises

  • Lab tasks

  • Assessment questions

  • Mini-projects

  • Expected outputs

  • Teacher demonstration notes

Students can follow along using VS Code + FastAPI.


๐Ÿ“Œ Before Starting — Lab Environment Setup

Students must complete:

  1. Install Python 3.10+

  2. Install VS Code

  3. Install Python extension in VS Code

  4. In Terminal:

pip install fastapi uvicorn

Create project folder:

fastapi_course/

๐Ÿงช MODULE-WISE LAB EXERCISES & ACTIVITIES

Below are 12 modules with 3 types of tasks:

  1. Practice Exercise (Must do)

  2. Lab Task (Hands-on)

  3. Assessment Question (Graded)


⭐ MODULE 1 — FASTAPI BASICS

Practice Exercise

  • Create a FastAPI app with one route:
    /hello → returns "Hello Students"

  • Add a second route: /college → returns your college name.

Lab Task

  • Add a route: /square/{num} that returns the square of the number.

Example output:
{"number": 5, "square": 25}

Assessment Question

  • Create a route that returns the current date & time.


⭐ MODULE 2 — PATH PARAMETERS

Practice Exercise

  • Create /student/{id} → return "Student ID is {id}".

Lab Task

  • Create /book/{book_id} → returns a dictionary:
    { "book_id": 10, "title": "Some Title" }

Assessment Question

  • Create /calc/{a}/{b} → return sum, difference, product.


⭐ MODULE 3 — QUERY PARAMETERS

Practice Exercise

  • Create /search?item=pen&qty=10 → return both values.

Lab Task

  • Create /area?length=10&width=20 → return area calculation.

Assessment Question

  • Create /filter?category=electronics&limit=5 → return meaningful JSON.


⭐ MODULE 4 — REQUEST BODY (POST)

Practice Exercise

  • Accept a JSON body:

{ "name": "Student", "age": 20 }

Lab Task

  • Create /register-student accepting name, reg_no, department.

Assessment Question

  • Design a POST API /feedback accepting:

    • student name

    • course name

    • rating


⭐ MODULE 5 — CRUD (IN-MEMORY DB)

Practice Exercise

  • Add item with POST

  • List items with GET

Lab Task

  • Implement:

    • Create item

    • Get by ID

    • Update by ID

    • Delete by ID

Assessment Question

  • Build a mini-API for storing student records (name, id, dept).


⭐ MODULE 6 — PYDANTIC MODELS

Practice Exercise

  • Create a Pydantic model for Student(name, age, department).

Lab Task

  • Create a route validating:

    • Product(name, price, stock)

Assessment Question

  • Add validation:
    Price must be > 0
    Stock must be >= 0


⭐ MODULE 7 — ERROR HANDLING

Practice Exercise

  • Raise HTTP 404 manually if ID not found.

Lab Task

  • Create:
    /student/{id}
    If ID not in list → return custom JSON error.

Assessment Question

  • Create a custom error for invalid age input.


⭐ MODULE 8 — MIDDLEWARE

Practice Exercise

  • Print "Request Received" for every API call using middleware.

Lab Task

  • Log:

    • method

    • url

    • response time

Assessment Question

  • Add middleware that blocks any request containing a custom header "blocked: true".


⭐ MODULE 9 — AUTHENTICATION (Simple Token-Based)

Practice Exercise

  • Add /login route that returns a token "abc123".

Lab Task

  • Create /secure-data which requires token in header.

Assessment Question

  • Generate unique token per user using UUID.


⭐ MODULE 10 — BACKGROUND TASKS

Practice Exercise

  • Create a background task that prints "Task Completed".

Lab Task

  • Simulate sending email in background:
    /send-email?email=abc@gmail.com

Assessment Question

  • Store background log messages in a file.


⭐ MODULE 11 — CORS & STATIC FILES

Practice Exercise

  • Enable CORS for all origins.

Lab Task

  • Serve a hello.txt file from /static/hello.txt.

Assessment Question


⭐ MODULE 12 — SQLITE DATABASE CRUD

Practice Exercise

  • Create SQLite DB file: students.db

  • Table: id, name, department

Lab Task

  • Implement:

    • Add student

    • Get all

    • Update

    • Delete

Assessment Question

  • Add marks column

  • Return average marks of all students using a query.


๐Ÿ’ผ MINI PROJECTS (For Final Lab Assessment)

Choose any one:

  1. Student Information System
    Add, update, delete students (SQLite)

  2. Library Book Manager
    Add books, issue books, return books.

  3. Online Store API
    Products + Cart + Order simulation.

  4. College Attendance API
    Students → Subjects → Attendance marking.

  5. Feedback Collection API
    Save feedback to SQLite + list top feedback.


๐ŸŽฏ INTERNAL ASSESSMENT TEST QUESTIONS

  1. What is FastAPI and why is it fast?

  2. Explain Path vs Query parameters with examples.

  3. What are Pydantic models?

  4. What is middleware? Give an example.

  5. Explain background tasks with a use case.

  6. What is CORS and why is it needed?

  7. How do you connect FastAPI with SQLite?


๐Ÿงพ TEACHER DEMO IDEAS (Optional)

Teachers may demonstrate:

  • Running a module with uvicorn

  • Using /docs for live testing

  • Debugging validation errors

  • Observing middleware logs

  • Creating & reading SQLite DB files

  • Real-time CRUD operations


๐ŸŽ‰ END OF MANUAL


No comments:

Post a Comment

Diagnosis for running all Scripts

 Great — here is the complete ready-to-run folder structure for all 12 FastAPI modules , including: ✅ __init__.py (so Python treats folder...