0
0
Flaskframework~30 mins

CRUD operations in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
CRUD Operations with Flask
📖 Scenario: You are building a simple web app to manage a list of books. Each book has a title and an author. You want to create a Flask app that can create, read, update, and delete books.
🎯 Goal: Build a Flask app with routes to add a book, view all books, update a book, and delete a book using an in-memory list as the data store.
📋 What You'll Learn
Create a list called books to store book dictionaries with keys title and author.
Add a configuration variable app as a Flask instance.
Create a route /books that returns the list of books in JSON format.
Add routes to add, update, and delete books using Flask route decorators.
💡 Why This Matters
🌍 Real World
Managing lists of items like books, tasks, or contacts is common in web apps. This project shows how to build basic CRUD operations with Flask, a popular Python web framework.
💼 Career
Understanding CRUD operations and Flask routing is essential for backend web development roles. This knowledge helps you build APIs and web services that handle data.
Progress0 / 4 steps
1
Data Setup: Create the books list
Create a list called books with these exact dictionaries: {'title': '1984', 'author': 'George Orwell'} and {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee'}.
Flask
Need a hint?

Use a list with two dictionaries exactly as shown.

2
Configuration: Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use app = Flask(__name__) to create the app.

3
Core Logic: Create a route to get all books
Use @app.route('/books') to create a function get_books() that returns the books list as JSON using from flask import jsonify.
Flask
Need a hint?

Use @app.route('/books') and return jsonify(books).

4
Completion: Add routes to create, update, and delete books
Add three routes: @app.route('/books', methods=['POST']) with function add_book() to add a book from JSON request; @app.route('/books/<int:index>', methods=['PUT']) with function update_book(index) to update a book at index; and @app.route('/books/<int:index>', methods=['DELETE']) with function delete_book(index) to remove a book at index. Use from flask import request and return updated books as JSON.
Flask
Need a hint?

Use request.get_json() to get data and modify the books list accordingly.