Skip to main content
In this lesson, we will configure our project structure for a full-stack application that includes a Flask backend and a React frontend. Proper organization is essential to ensure smooth development and maintenance. In the previous lesson, we set up a virtual environment for the image optimizer. Now, we will remove that existing virtual environment and create a new structure with two primary directories: • imageoptimizer.app – for the Flask backend
• imageoptimizer.web – for the React frontend
Let’s start by reorganizing our application folder and setting up the Flask app.

Scaffolding the Flask Application

Begin by navigating to your application directory in the terminal:
If you are new to Flask or simply want a quick scaffold, you might consider using an AI-based tool to generate the setup instructions. After entering your project folder, you could prompt: “How do you scaffold a typical Flask application?” You could receive similar commands as output. As part of this setup, create a new virtual environment and install Flask:
Then install Flask:
You might see output similar to this:
Next, use an AI-based tool to provide instructions on setting up the directory structure and initializing your Flask application. Typically, an __init__.py file is created to serve as the application factory. For instance, here’s a snippet demonstrating a simple login form using Flask-WTF:
Additional package installation output may appear as follows:
This AI-assisted scaffolding approach can be very effective, and in our case, we are leveraging a custom AI model for setup instructions, although other models provide similar support.
The image shows a coding environment with a file structure for a Flask application and a terminal displaying package installation details.

Creating the Directory Structure

Now, create the following directory structure for the Flask application:
To generate this structure within imageoptimizer.app, follow these steps:
  1. Change to the app directory and create necessary files:
  2. Create the templates folder and add the base template:
  3. Create an instance folder and configuration file:
  4. Finally, create the run.py file in the project’s root and generate the requirements.txt file to capture the dependencies:
    Later, you can install these dependencies with:

Setting Up Git and .gitignore

Initialize a Git repository for the project from the root folder (which contains both imageoptimizer.app and imageoptimizer.web):
Next, add a remote origin that points to your GitHub repository:
Before committing your files, create a .gitignore file to exclude the virtual environment and other temporary files. An AI tool can help generate a typical .gitignore for a Python Flask application. A sample .gitignore might include:
After setting up .gitignore, add your files to the repository:
You can check the status with:
Commit your changes and push them to GitHub:
If you encounter a rejection due to remote changes, resolve it by pulling the latest updates and pushing again:
This setup ensures your repository remains clean, excluding unnecessary files such as the virtual environment.
The image shows a GitHub repository page for a project called "Super-Image-Optimizer," which is a web-based image optimizer. The repository has one branch and no tags, with an initial commit.

Building the Flask Application

Now that our environment is ready and Git is tracking our updates, we will create the Flask application using the application factory pattern. Below is an example of how to set up Flask:
If you are utilizing SQLAlchemy, you may define your models like this:
Similarly, your Flask-WTF forms can be set up as follows:
Keep in mind that some parts of this code might not function correctly on the first try. The intentional errors are meant to represent real-world troubleshooting scenarios when using AI-generated tools.

Next Steps

In the upcoming lesson, we will integrate the Flask API and perform testing to ensure the application operates as expected. We will also troubleshoot and refine the workflow to enhance our development process. Happy coding, and see you in the next lesson!

Watch Video