Quickstarters

How to Develop a CRUD Application with Flask?

32min

Overview

This tutorial will guide you through building a CRUD (create, read, update, delete) application using Flask.

We'll integrate Back4app as our backend service to simplify data management. You will learn to set up a Back4app project, design a flexible data structure, and implement CRUD functionalities using Flask.

In this walkthrough, you'll first create a Back4app project named Basic-CRUD-App-Flask that offers a robust, non-relational data storage solution.

You'll define your data schema with classes and fields either manually or using Back4app’s AI Agent.

After configuring your backend with Back4app’s Admin interface, you will connect your Flask application to the service via REST API calls. This tutorial also covers secure access management for your data.

By the end, you will have a production-ready Flask application capable of performing CRUD operations along with secure user authentication and data handling.

What You Will Learn

  • How to create a Flask-based CRUD app with a non-relational backend.
  • How to structure your backend on Back4app and integrate it with a Flask project.
  • How to utilize the Back4app Admin interface to manage CRUD operations effortlessly.
  • How to deploy your Flask application, including using Docker for containerization.


Prerequisites

Before you begin, ensure you have:

  • A Back4app account with a configured project. Need help? Refer to Getting Started with Back4app.
  • A Python development environment set up. Use an editor like VSCode or PyCharm and install Python 3.8 (or later).
  • Basic knowledge of Python, Flask, and REST APIs. Consult the Python documentation if necessary.


Step 1 – Initial Project Setup

Creating Your Back4app Project

  1. Log in to your Back4app account.
  2. Click on “New App” from your dashboard.
  3. Enter the project name: Basic-CRUD-App-Flask and complete the setup process.
Create New Project
Create New Project


After creation, your project appears on the dashboard, serving as the base for your backend configuration.



Step 2 – Building the Data Schema

Defining Your Data Structures

For this application, you'll establish several classes in your Back4app project. Below are examples of the core classes and their fields essential for CRUD operations.

1. Items Class

Field

Type

Description

_id

ObjectId

Auto-generated unique identifier.

title

String

The name or title of the item.

description

String

A brief overview of the item.

createdAt

Date

Timestamp when the item was created.

updatedAt

Date

Timestamp for the latest update.

2. Users Class

Field

Type

Description

_id

ObjectId

Auto-generated unique identifier.

username

String

Unique username for the user.

email

String

Unique email address.

passwordHash

String

Securely hashed password for authentication.

createdAt

Date

Timestamp when the user account was created.

updatedAt

Date

Timestamp for the last account update.

You can create these classes and add fields directly in your Back4app dashboard.

Create New Class
Create New Class


You can add columns by choosing a data type, naming the field, setting default values, and marking it as required.

Create Column
Create Column


Utilizing Back4app’s AI Agent for Schema Creation

The AI Agent in your dashboard can automatically establish the schema based on your instructions. This tool simplifies the process and ensures your data model is optimal for CRUD tasks.

How to Use the AI Agent:

  1. Open the AI Agent: Sign in to your Back4app dashboard and locate the AI Agent under project settings.
  2. Describe Your Schema: Provide a detailed prompt listing the classes and fields.
  3. Confirm the Setup: Review the proposed schema and approve the changes.

Example Prompt

Text


This AI-driven process saves time and guarantees a consistent, effective data model.



Step 3 – Enabling the Admin Interface & Handling CRUD Operations

Overview of the Admin Interface

Back4app’s Admin interface provides a no-code solution to manage your backend data. With its drag and drop features, you can easily perform CRUD operations—such as adding, editing, or deleting records.

Enabling the Admin Interface

  1. Go to the “More” menu on your Back4app dashboard.
  2. Select “Admin App” and click “Enable Admin App.”
  3. Create your admin account, which also sets up initial roles like B4aAdminUser.
Enable Admin App
Enable Admin App


After activation, sign in to the Admin interface to manage your project data.

Admin App Dashboard
Admin App Dashboard


Using the Admin Interface for CRUD

Within the Admin interface, you can:

  • Insert New Entries: Click “Add Record” within a class (e.g., Items) to create new data.
  • View and Modify Entries: Select any record to see details or update fields.
  • Delete Records: Remove entries that are no longer required.

This easy-to-use interface improves productivity by streamlining backend management.



Step 4 – Connecting Your Flask Application to Back4app

With your backend set up, the next step is to link your Flask application to Back4app.

Making API Calls from Flask

Since there isn’t a dedicated Parse SDK for Flask, you will use REST API calls. In your Flask app, you can utilize the requests library to perform CRUD operations.

Example: Setting Up API Communication

  1. Install the Requests Library: Run the following command in your terminal:

    Bash
    
  2. Configure a Python Module for API Calls: Create a file called back4app_api.py:

    Python
    
  3. Integrate API Functions in Your Flask Routes: For example, in app.py:

    Python
    

This example illustrates how to use REST calls within Flask to interact with your Back4app backend.



Step 5 – Protecting Your Backend

Implementing Access Controls

Safeguard your data by configuring access controls. For instance, when creating a new item, you might restrict access to its creator.

Below is an example using API calls with controlled access:

Python


Configuring Class-Level Permissions

Adjust the Class-Level Permissions (CLPs) directly in the Back4app dashboard to ensure only authenticated users can access specific classes.



Step 6 – Implementing User Authentication

Setting Up User Management

Back4app offers built-in user management via its Users class. In your Flask app, you can create endpoints for registration and login.

Example: User Registration and Login

Python


You can create corresponding Flask routes to handle user registration and login using these functions.



Step 7 – Deploying Your Flask Application

Back4app simplifies deployment. You can deploy your Flask app via several methods, including Docker.

7.1 Packaging Your Flask Application

  1. Prepare Your Application: Ensure all required files and dependencies are included.
  2. Test Locally: Run your app locally with:

    Bash
    

7.2 Organizing Your Project Structure

A typical structure might be:

Text


7.3 Containerizing with Docker

Include a Dockerfile at the project root:

Dockerfile


7.4 Deploying via Back4app Web Deployment

  1. Connect Your GitHub Repository: Ensure your code is pushed to GitHub.
  2. Configure Deployment Settings: In the Back4app dashboard, use the Web Deployment feature to link your repository (e.g., Basic-CRUD-App-Flask) and select your branch.
  3. Define Build Commands: Specify the build command (e.g., pip install -r requirements.txt) and the location of your application.
  4. Deploy: Click Deploy and monitor the logs until your application is live.


Step 8 – Wrapping Up and Future Directions

Great job! You have successfully created a Flask-based CRUD application integrated with Back4app.

You established a project named Basic-CRUD-App-Flask, defined data models for Items and Users, and managed your backend via the Back4app Admin interface.

Additionally, you connected your Flask application using REST API calls and implemented security measures.

What’s Next?

  • Expand Functionality: Consider adding features like advanced search, detailed item views, or real-time updates.
  • Enhance Backend Services: Explore cloud functions, integrate third-party APIs, or implement role-based access control.
  • Deepen Your Skills: Visit the Back4app documentation and other resources to further refine your application.

Happy coding and best of luck with your Flask CRUD application!