0
0
Flaskframework~30 mins

Database query optimization in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Database Query Optimization with Flask
📖 Scenario: You are building a simple Flask web app that shows a list of books from a database. The database has many books, but you want to show only the books with more than 300 pages to users.
🎯 Goal: Build a Flask app that queries the database efficiently to get only books with more than 300 pages and displays their titles.
📋 What You'll Learn
Create a list of book dictionaries with title and pages
Add a variable to set the minimum page count filter
Use a list comprehension to select books with pages greater than the filter
Create a Flask route to display the filtered book titles
💡 Why This Matters
🌍 Real World
Filtering data before sending it to users helps apps run faster and use less memory.
💼 Career
Knowing how to optimize queries and filter data efficiently is important for backend web development jobs.
Progress0 / 4 steps
1
Create the book data list
Create a list called books with these exact dictionaries: {'title': 'Python Basics', 'pages': 250}, {'title': 'Flask Web Development', 'pages': 350}, {'title': 'Advanced Python', 'pages': 400}
Flask
Need a hint?

Use a list with dictionaries. Each dictionary has keys 'title' and 'pages'.

2
Set the minimum page count filter
Create a variable called min_pages and set it to 300 to filter books with more than 300 pages
Flask
Need a hint?

Just create a variable named min_pages and assign 300.

3
Filter books with list comprehension
Create a list called filtered_books using a list comprehension that includes only books from books where the pages value is greater than min_pages
Flask
Need a hint?

Use a list comprehension with book for book in books if book['pages'] > min_pages.

4
Create Flask route to display filtered books
Import Flask and create an app. Then add a route /books that returns a string with the titles of filtered_books joined by commas
Flask
Need a hint?

Import Flask, create app, define route /books, and return joined titles.