Skip to content

About

Session 20: Flask Web App ๐ŸŒถ๏ธ๐Ÿ

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒถ๏ธ๐Ÿ Session 20: Flask Web App

๐Ÿ“ธ Lesson Snapshot

Build your first Python web API using Flask - the tiny but mighty web framework! (Ages 8-18)

Session Date: Saturday, May 2, 2026 at 11:00 AM EST


๐ŸŽฏ Age-Tiered Learning Goals

๐Ÿง’ Grades 3-5 ๐Ÿง‘ Grades 6-8 ๐ŸŽ“ Grades 9-12
๐Ÿ Python Understand Python basics Write functions and use dictionaries Use classes and list comprehensions
๐ŸŒถ๏ธ Flask Run a Flask app, visit routes Create routes with parameters Build JSON APIs with validation
๐Ÿ”„ Compare See C# vs Python side-by-side Explain key differences Analyze trade-offs of each
๐Ÿ By End Run Flask app and see output Create 3+ API endpoints Build complete CRUD API

๐ŸŽจ Before You Code: Hands-On Activities (15 minutes)

๐ŸŽฎ Activity 1: The Restaurant Game (Whole Class)

๐ŸŽฌ Setup: Students act out a restaurant experience

    ๐Ÿฝ๏ธ THE RESTAURANT IS LIKE A WEB SERVER! ๐ŸŒ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                         โ”‚
    โ”‚   ๐Ÿ‘ค CUSTOMER          ๐Ÿง‘โ€๐Ÿณ CHEF           ๐Ÿ“‹ MENU        โ”‚
    โ”‚   (Browser)           (Flask)          (Routes)        โ”‚
    โ”‚                                                         โ”‚
    โ”‚   "I'd like the      "Coming right    /pizza ๐Ÿ•        โ”‚
    โ”‚    pizza please!"     up!"             /burger ๐Ÿ”       โ”‚
    โ”‚                                        /salad ๐Ÿฅ—        โ”‚
    โ”‚         โ”‚                  โ”‚                            โ”‚
    โ”‚         โ–ผ                  โ–ผ                            โ”‚
    โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                       โ”‚
    โ”‚   โ”‚ REQUEST โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚ PROCESS โ”‚                       โ”‚
    โ”‚   โ”‚  /pizza โ”‚        โ”‚  make   โ”‚                       โ”‚
    โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ”‚  pizza  โ”‚                       โ”‚
    โ”‚                      โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜                       โ”‚
    โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”‚                            โ”‚
    โ”‚         โ”‚RESPONSE โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                            โ”‚
    โ”‚         โ”‚  ๐Ÿ•     โ”‚                                    โ”‚
    โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ’ฌ Discussion:

"When you go to a restaurant, you ask for something from the menu (REQUEST), the chef makes it (PROCESS), and brings it back (RESPONSE). Web servers work the same way!"


๐Ÿ Activity 2: Python vs C# - Spot the Difference! (Groups)

๐Ÿ” Look at these two code snippets - what's different?

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  C# (ASP.NET Core) ๐Ÿ”ท                                   โ”‚
    โ”‚                                                         โ”‚
    โ”‚  [HttpGet]                                              โ”‚
    โ”‚  public ActionResult<List<Book>> GetAllBooks()          โ”‚
    โ”‚  {                                                      โ”‚
    โ”‚      return Ok(_books);                                 โ”‚
    โ”‚  }                                                      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Python (Flask) ๐Ÿ                                      โ”‚
    โ”‚                                                         โ”‚
    โ”‚  @app.route('/books')                                   โ”‚
    โ”‚  def get_all_books():                                   โ”‚
    โ”‚      return jsonify(books)                              โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Differences to spot:

  • ๐Ÿ”ท C# uses {} braces, ๐Ÿ Python uses indentation
  • ๐Ÿ”ท C# has type declarations, ๐Ÿ Python doesn't
  • ๐Ÿ”ท C# uses [HttpGet], ๐Ÿ Python uses @app.route()
  • ๐Ÿ Python is often shorter and simpler!

๐ŸŒถ๏ธ Activity 3: Flask Logo Hunt (Fun Fact!)

    WHY IS IT CALLED FLASK? ๐Ÿงช

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                         โ”‚
    โ”‚         ๐Ÿงช                                              โ”‚
    โ”‚        /  \      Flask is named after a                 โ”‚
    โ”‚       /    \     LABORATORY FLASK!                      โ”‚
    โ”‚      /      \                                           โ”‚
    โ”‚     |        |   It's a "micro" framework -             โ”‚
    โ”‚     |        |   small and simple, like a               โ”‚
    โ”‚     |________|   tiny flask in a science lab.           โ”‚
    โ”‚                                                         โ”‚
    โ”‚   The pepper emoji ๐ŸŒถ๏ธ is because "Flask"               โ”‚
    โ”‚   sounds like "Flasche" (bottle) in German!             โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Š Visual: Flask Request/Response Cycle

    HOW FLASK HANDLES A REQUEST ๐Ÿ”„

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                                  โ”‚
    โ”‚   1๏ธโƒฃ BROWSER                    2๏ธโƒฃ FLASK APP                    โ”‚
    โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
    โ”‚   โ”‚             โ”‚   HTTP GET    โ”‚                             โ”‚ โ”‚
    โ”‚   โ”‚  Chrome ๐ŸŒ  โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚  @app.route('/books')       โ”‚ โ”‚
    โ”‚   โ”‚             โ”‚  /api/books   โ”‚  def get_all_books():       โ”‚ โ”‚
    โ”‚   โ”‚             โ”‚               โ”‚      return jsonify(books)  โ”‚ โ”‚
    โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
    โ”‚         โ–ฒ                                      โ”‚                โ”‚
    โ”‚         โ”‚                                      โ–ผ                โ”‚
    โ”‚         โ”‚                       3๏ธโƒฃ PYTHON RUNS THE FUNCTION    โ”‚
    โ”‚         โ”‚                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
    โ”‚         โ”‚       HTTP 200 OK     โ”‚  books = [                  โ”‚ โ”‚
    โ”‚         โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚    {"title": "Harry Potter"}โ”‚ โ”‚
    โ”‚         โ”‚       + JSON data     โ”‚  ]                          โ”‚ โ”‚
    โ”‚         โ”‚                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
    โ”‚                                                                  โ”‚
    โ”‚   4๏ธโƒฃ BROWSER DISPLAYS THE DATA                                  โ”‚
    โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
    โ”‚   โ”‚  [{"id": 1, "title": "Harry Potter", "author": "J.K...  โ”‚   โ”‚
    โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
    โ”‚                                                                  โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”„ ASP.NET Core vs Flask Comparison

    SIDE-BY-SIDE COMPARISON ๐Ÿ”ท vs ๐Ÿ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  ASP.NET Core (C#) ๐Ÿ”ท       โ”‚  Flask (Python) ๐Ÿ          โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                             โ”‚
    โ”‚  Statically typed           โ”‚  Dynamically typed          โ”‚
    โ”‚  (must declare types)       โ”‚  (types are automatic)      โ”‚
    โ”‚                             โ”‚                             โ”‚
    โ”‚  string name = "Sam";       โ”‚  name = "Sam"               โ”‚
    โ”‚                             โ”‚                             โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                             โ”‚
    โ”‚  Uses Controllers           โ”‚  Uses route decorators      โ”‚
    โ”‚                             โ”‚                             โ”‚
    โ”‚  [HttpGet]                  โ”‚  @app.route('/books')       โ”‚
    โ”‚  public ActionResult Get()  โ”‚  def get_books():           โ”‚
    โ”‚  { return Ok(data); }       โ”‚      return jsonify(data)   โ”‚
    โ”‚                             โ”‚                             โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                             โ”‚
    โ”‚  Curly braces { }           โ”‚  Indentation (spaces)       โ”‚
    โ”‚  Semicolons ;               โ”‚  No semicolons!             โ”‚
    โ”‚                             โ”‚                             โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                             โ”‚
    โ”‚  dotnet run                 โ”‚  flask run                  โ”‚
    โ”‚  or python -m flask run    or  python app.py              โ”‚
    โ”‚                             โ”‚                             โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    BOTH CAN:
    โœ… Create REST APIs
    โœ… Handle GET, POST, PUT, DELETE
    โœ… Return JSON
    โœ… Render HTML templates
    โœ… Connect to databases

๐Ÿ“ Folder & File Map

Session20/
โ”œโ”€โ”€ README.md           โ† ๐Ÿ“– You are here!
โ”œโ”€โ”€ app.py              โ† ๐Ÿ Our Flask application
โ”œโ”€โ”€ requirements.txt    โ† ๐Ÿ“ฆ Python packages we need
โ”œโ”€โ”€ .gitignore          โ† ๐Ÿ™ˆ Files Git should ignore
โ””โ”€โ”€ templates/
    โ””โ”€โ”€ index.html      โ† ๐ŸŽจ HTML template for the homepage

โœ… Prerequisites

  • โœ… Completed Session 19 (Python Basics)
  • โœ… Python 3.8+ installed
  • โœ… VS Code or PyCharm installed
  • โœ… Understanding of HTTP from Sessions 11-13

๐Ÿ› ๏ธ Step-by-Step Build Guide

1๏ธโƒฃ Set Up Your Project

๐Ÿ“‚ Create a new folder and open it:

# Create folder
mkdir flask-library
cd flask-library

# Create a virtual environment (keeps packages organized)
python -m venv venv

# Activate it!
# On Mac/Linux:
source venv/bin/activate

# On Windows:
venv\Scripts\activate

# You'll see (venv) in your terminal - that means it's working! โœ…

๐ŸŽจ What's a virtual environment?

    VIRTUAL ENVIRONMENT = YOUR OWN PYTHON SANDBOX ๐Ÿ–๏ธ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                         โ”‚
    โ”‚   Without venv:           With venv:                    โ”‚
    โ”‚                                                         โ”‚
    โ”‚   ๐ŸŒ System Python        ๐Ÿ“ฆ Project Python             โ”‚
    โ”‚   All packages mixed      Only YOUR packages            โ”‚
    โ”‚   Might break things!     Safe and organized!           โ”‚
    โ”‚                                                         โ”‚
    โ”‚   Think of it like having your own locker at school -   โ”‚
    โ”‚   your stuff stays separate from everyone else's! ๐ŸŽ’    โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2๏ธโƒฃ Install Flask

๐Ÿ“ฆ Install Flask using pip:

pip install flask

๐Ÿ“ Create requirements.txt:

Flask==3.0.0

๐Ÿ’ก Why requirements.txt?

It's like a shopping list for your project! Anyone can run pip install -r requirements.txt to get all the same packages you're using.


3๏ธโƒฃ Create Your First Flask App

๐Ÿ“„ Create file: app.py

# ====================================
# ๐ŸŒถ๏ธ Flask Library API
# Our first Python web application!
# ====================================

from flask import Flask, jsonify, request, render_template

# Create our Flask app ๐ŸŽ‰
app = Flask(__name__)

# ====================================
# ๐Ÿ“š Our Book Data (in-memory storage)
# Just like our C# List<Book>!
# ====================================
books = [
    {"id": 1, "title": "Harry Potter", "author": "J.K. Rowling", "pages": 309, "available": True},
    {"id": 2, "title": "The Hobbit", "author": "J.R.R. Tolkien", "pages": 310, "available": True},
    {"id": 3, "title": "Diary of a Wimpy Kid", "author": "Jeff Kinney", "pages": 217, "available": False}
]

# ====================================
# ๐Ÿ  HOME ROUTE - Returns HTML page
# ====================================
@app.route('/')
def home():
    """
    This is like our home page!
    render_template loads the HTML file from /templates folder
    """
    return render_template('index.html', book_count=len(books))

# ====================================
# ๐Ÿ“š GET ALL BOOKS
# Same as our C# GetAllBooks()!
# ====================================
@app.route('/api/books', methods=['GET'])
def get_all_books():
    """
    Returns all books as JSON

    C# equivalent:
    [HttpGet]
    public ActionResult<List<Book>> GetAllBooks()
    {
        return Ok(_books);
    }
    """
    return jsonify(books)

# ====================================
# ๐Ÿ“– GET ONE BOOK BY ID
# Same as our C# GetBook(int id)!
# ====================================
@app.route('/api/books/<int:book_id>', methods=['GET'])
def get_book(book_id):
    """
    Returns a single book by ID
    <int:book_id> is like {id} in C# routes

    C# equivalent:
    [HttpGet("{id}")]
    public ActionResult<Book> GetBook(int id)
    """
    # Find the book with matching ID
    book = next((b for b in books if b['id'] == book_id), None)

    if book is None:
        return jsonify({"error": f"Book with ID {book_id} not found"}), 404

    return jsonify(book)

# ====================================
# โž• CREATE A NEW BOOK
# Same as our C# CreateBook()!
# ====================================
@app.route('/api/books', methods=['POST'])
def create_book():
    """
    Creates a new book from JSON data

    C# equivalent:
    [HttpPost]
    public ActionResult<Book> CreateBook(Book newBook)
    """
    # Get JSON data from request
    data = request.get_json()

    # Validate required fields
    if not data or 'title' not in data or 'author' not in data:
        return jsonify({"error": "Title and author are required!"}), 400

    # Create new book with auto-generated ID
    new_book = {
        "id": max(b['id'] for b in books) + 1 if books else 1,
        "title": data['title'],
        "author": data['author'],
        "pages": data.get('pages', 0),  # Default to 0 if not provided
        "available": True
    }

    books.append(new_book)

    # Return 201 Created with the new book
    return jsonify(new_book), 201

# ====================================
# ๐Ÿ“ค CHECKOUT A BOOK
# ====================================
@app.route('/api/books/<int:book_id>/checkout', methods=['POST'])
def checkout_book(book_id):
    """Checkout a book - mark it as unavailable"""
    book = next((b for b in books if b['id'] == book_id), None)

    if book is None:
        return jsonify({"error": f"Book with ID {book_id} not found"}), 404

    if not book['available']:
        return jsonify({"error": f"'{book['title']}' is already checked out!"}), 400

    book['available'] = False
    return jsonify({"message": f"Successfully checked out '{book['title']}'! ๐Ÿ“š"})

# ====================================
# ๐Ÿ“ฅ RETURN A BOOK
# ====================================
@app.route('/api/books/<int:book_id>/return', methods=['POST'])
def return_book(book_id):
    """Return a book - mark it as available"""
    book = next((b for b in books if b['id'] == book_id), None)

    if book is None:
        return jsonify({"error": f"Book with ID {book_id} not found"}), 404

    if book['available']:
        return jsonify({"error": f"'{book['title']}' wasn't checked out!"}), 400

    book['available'] = True
    return jsonify({"message": f"Thank you for returning '{book['title']}'! ๐Ÿ“–"})

# ====================================
# ๐Ÿš€ RUN THE APP
# ====================================
if __name__ == '__main__':
    # debug=True means it auto-reloads when you save! ๐Ÿ”„
    print("๐ŸŒถ๏ธ Starting Flask Library API...")
    print("๐Ÿ“š Visit http://localhost:5000 in your browser!")
    app.run(debug=True, port=5000)

4๏ธโƒฃ Create the HTML Template

๐Ÿ“‚ Create folder: templates/

๐Ÿ“„ Create file: templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>๐ŸŒถ๏ธ Flask Library API</title>
    <style>
        /* Fun, kid-friendly styling! */
        body {
            font-family: 'Comic Sans MS', 'Segoe UI', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
        }
        .container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 30px;
            color: #333;
            box-shadow: 0 10px 40px rgba(0,0,0,0.3);
        }
        h1 {
            color: #667eea;
            text-align: center;
        }
        .emoji-big {
            font-size: 4rem;
            text-align: center;
            display: block;
        }
        .endpoint {
            background: #f8f9fa;
            border-left: 4px solid #667eea;
            padding: 15px;
            margin: 10px 0;
            border-radius: 0 10px 10px 0;
        }
        .method {
            display: inline-block;
            padding: 3px 10px;
            border-radius: 5px;
            font-weight: bold;
            margin-right: 10px;
        }
        .get { background: #28a745; color: white; }
        .post { background: #ffc107; color: black; }
        code {
            background: #e9ecef;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: monospace;
        }
        .book-count {
            font-size: 2rem;
            text-align: center;
            color: #667eea;
        }
    </style>
</head>
<body>
    <div class="container">
        <span class="emoji-big">๐ŸŒถ๏ธ๐Ÿ“š๐Ÿ</span>
        <h1>Flask Library API</h1>

        <p class="book-count">๐Ÿ“š {{ book_count }} books in the library!</p>

        <h2>๐ŸŽฏ API Endpoints</h2>

        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/books</code>
            <p>Get all books in the library</p>
        </div>

        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/books/&lt;id&gt;</code>
            <p>Get a specific book by ID</p>
        </div>

        <div class="endpoint">
            <span class="method post">POST</span>
            <code>/api/books</code>
            <p>Add a new book (send JSON with title, author, pages)</p>
        </div>

        <div class="endpoint">
            <span class="method post">POST</span>
            <code>/api/books/&lt;id&gt;/checkout</code>
            <p>Check out a book</p>
        </div>

        <div class="endpoint">
            <span class="method post">POST</span>
            <code>/api/books/&lt;id&gt;/return</code>
            <p>Return a book</p>
        </div>

        <h2>๐Ÿงช Try It!</h2>
        <p>Open your browser to: <code>http://localhost:5000/api/books</code></p>
        <p>Or use Postman/curl to test the POST endpoints!</p>

        <hr>
        <p style="text-align: center; color: #666;">
            Built with โค๏ธ at CoderDojo | Session 20
        </p>
    </div>
</body>
</html>

5๏ธโƒฃ Run Your Flask App!

# Make sure you're in your project folder with venv activated
python app.py

๐ŸŽ‰ You should see:

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  ๐ŸŒถ๏ธ Starting Flask Library API...                       โ”‚
    โ”‚  ๐Ÿ“š Visit http://localhost:5000 in your browser!        โ”‚
    โ”‚   * Running on http://127.0.0.1:5000                    โ”‚
    โ”‚   * Debug mode: on                                      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŒ Open your browser and visit:

  • http://localhost:5000 - See the homepage!
  • http://localhost:5000/api/books - See all books as JSON!
  • http://localhost:5000/api/books/1 - See just Harry Potter!

6๏ธโƒฃ Test with Postman or curl

๐Ÿงช Test GET all books:

curl http://localhost:5000/api/books

๐Ÿงช Test POST new book:

curl -X POST http://localhost:5000/api/books \
  -H "Content-Type: application/json" \
  -d '{"title": "Percy Jackson", "author": "Rick Riordan", "pages": 375}'

๐Ÿงช Test checkout:

curl -X POST http://localhost:5000/api/books/1/checkout

๐Ÿ“Š Visual: Code Comparison

    GETTING A BOOK BY ID - C# vs Python ๐Ÿ”ท๐Ÿ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  C# (ASP.NET Core) - 12 lines ๐Ÿ”ท                        โ”‚
    โ”‚                                                         โ”‚
    โ”‚  [HttpGet("{id}")]                                      โ”‚
    โ”‚  public ActionResult<Book> GetBook(int id)              โ”‚
    โ”‚  {                                                      โ”‚
    โ”‚      var book = _books.FirstOrDefault(b => b.Id == id); โ”‚
    โ”‚                                                         โ”‚
    โ”‚      if (book == null)                                  โ”‚
    โ”‚      {                                                  โ”‚
    โ”‚          return NotFound($"Book {id} not found");       โ”‚
    โ”‚      }                                                  โ”‚
    โ”‚                                                         โ”‚
    โ”‚      return Ok(book);                                   โ”‚
    โ”‚  }                                                      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Python (Flask) - 8 lines ๐Ÿ                            โ”‚
    โ”‚                                                         โ”‚
    โ”‚  @app.route('/api/books/<int:book_id>')                 โ”‚
    โ”‚  def get_book(book_id):                                 โ”‚
    โ”‚      book = next((b for b in books                      โ”‚
    โ”‚                   if b['id'] == book_id), None)         โ”‚
    โ”‚                                                         โ”‚
    โ”‚      if book is None:                                   โ”‚
    โ”‚          return jsonify({"error": "Not found"}), 404    โ”‚
    โ”‚                                                         โ”‚
    โ”‚      return jsonify(book)                               โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    Both do the SAME thing! Python is just more concise. ๐Ÿ“

๐Ÿ’ฌ Group Reflection (5 minutes)

๐Ÿ”„ Circle Share - Answer one:

  1. "One thing I like about Flask compared to ASP.NET Core is..."
  2. "Python feels different from C# because..."
  3. "The hardest part about learning a new framework is..."
  4. "I want to build ___ with Flask..."

โœ… Run & Test Checklist

  • ๐ŸŒถ๏ธ Flask app starts without errors
  • ๐Ÿ  Homepage loads at localhost:5000
  • ๐Ÿ“š GET /api/books returns all books
  • ๐Ÿ“– GET /api/books/1 returns Harry Potter
  • โŒ GET /api/books/999 returns 404 error
  • โž• POST /api/books creates a new book
  • ๐Ÿ“ค Checkout endpoint works
  • ๐Ÿ“ฅ Return endpoint works
  • ๐ŸŽจ Template displays book count correctly

๐Ÿš€ Bonus Challenges

โญ Challenge 1: Add a Search Route (Grades 3-5)

Add a route that searches for books by title:

@app.route('/api/books/search/<title>')
def search_books(title):
    # Find books where title contains the search term
    pass

โญโญ Challenge 2: Add PUT and DELETE (Grades 6-8)

Add routes to update and delete books:

  • PUT /api/books/<id> - Update a book
  • DELETE /api/books/<id> - Delete a book

โญโญโญ Challenge 3: Connect to Frontend (Grades 9-12)

Use your JavaScript knowledge from Sessions 16-18 to create a frontend that calls your Flask API!

โญโญโญ Challenge 4: Add a Database (Grades 9-12)

Replace the in-memory list with SQLite:

from flask_sqlalchemy import SQLAlchemy

๐Ÿ“– Vocabulary Wall

Term ๐Ÿ“ Kid-Friendly Definition ๐Ÿ’ป Example
๐ŸŒถ๏ธ Flask A small Python web framework from flask import Flask
๐ŸŽฏ Route A URL path that triggers a function @app.route('/books')
๐Ÿท๏ธ Decorator Special @ symbol that adds features @app.route()
๐Ÿ“ฆ jsonify Converts Python data to JSON return jsonify(books)
๐Ÿ“ Template HTML file that Flask can fill in render_template('index.html')
๐Ÿงช Virtual Environment Isolated Python package space python -m venv venv

๐Ÿ  Take-Home Connection

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง Talk to a family member:

"Today I learned Flask, a Python web framework! It's like ASP.NET Core that we learned in C#, but with Python. Python uses less code to do the same things - it's like texting in shorthand instead of writing full sentences!"

๐ŸŒ Real-world examples of Flask:

  • ๐ŸŽต Netflix uses Flask for some services
  • ๐Ÿ“ฑ Uber started with Flask
  • ๐Ÿ”— LinkedIn uses Flask for some features
  • ๐Ÿค– Many AI/ML projects use Flask for APIs

๐Ÿ“ Mini-Project Idea: Build a "Movie Rating" API with Flask:

  • Store movies with titles and ratings
  • Add routes to list, add, and rate movies
  • Create a simple frontend to display them

๐ŸŽฏ Key Takeaways

    WHAT WE LEARNED TODAY ๐Ÿง 

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                         โ”‚
    โ”‚  1. ๐Ÿ Flask is Python's lightweight web framework      โ”‚
    โ”‚                                                         โ”‚
    โ”‚  2. ๐ŸŽฏ Routes map URLs to Python functions              โ”‚
    โ”‚                                                         โ”‚
    โ”‚  3. ๐Ÿ“ฆ jsonify() converts Python dicts to JSON          โ”‚
    โ”‚                                                         โ”‚
    โ”‚  4. ๐Ÿ”„ Flask and ASP.NET Core do similar things         โ”‚
    โ”‚        but Flask uses less code                         โ”‚
    โ”‚                                                         โ”‚
    โ”‚  5. ๐Ÿงช Virtual environments keep packages organized     โ”‚
    โ”‚                                                         โ”‚
    โ”‚  6. ๐Ÿ“ Templates let us return HTML pages               โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ฎ What's Next?

    COMING UP IN SESSION 21: FINAL SHOWCASE! ๐ŸŽญ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                         โ”‚
    โ”‚  ๐ŸŽ‰ Present your complete project!                      โ”‚
    โ”‚                                                         โ”‚
    โ”‚  You've learned:                                        โ”‚
    โ”‚  โ”œโ”€โ”€ C# fundamentals (Sessions 1-4)                    โ”‚
    โ”‚  โ”œโ”€โ”€ Object-Oriented Programming (Sessions 5-10)       โ”‚
    โ”‚  โ”œโ”€โ”€ ASP.NET Core Web APIs (Sessions 11-15)            โ”‚
    โ”‚  โ”œโ”€โ”€ Frontend (HTML/CSS/JS) (Sessions 16-18)           โ”‚
    โ”‚  โ””โ”€โ”€ Python & Flask (Sessions 19-20)                   โ”‚
    โ”‚                                                         โ”‚
    โ”‚  Time to show off what you've built! ๐Ÿš€                 โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

You're almost at the finish line! Keep coding! ๐ŸŽ‰๐Ÿ๐ŸŒถ๏ธ

About

Session 20: Flask Web App ๐ŸŒถ๏ธ๐Ÿ

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages