Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Restructuring Your Project for Scalability

00:00 As your application grows, it is crucial to structure it in a way that remains maintainable and scalable. Think of the difference between a small house and a large skyscraper.

00:10 The structural requirements are vastly different. Similarly, even a simple Flask app can become difficult to manage as you add features. To address this, you will begin by reorganizing your project into a modular structure.

00:23 To make these concepts concrete, you will lay the foundation for the message board application that we discussed in earlier lessons, and it’ll serve as an example throughout this tutorial.

00:33 First, you’ll create a folder named board/. This folder will house the core functionality of the message board app, providing a modular design that makes it easier to expand and maintain as the application grows.

00:46 Next, you will move your app.py file into the newly created board/ folder. This begins the process of organizing your code into a more modular structure.

00:56 Then you need to rename app.py to __init__.py. In Python, the __init__ file inside a directory signals that the directory should be treated as a package.

01:07 This will allow you to import modules from within this directory later on, which is critical for organizing a larger Flask project.

01:16 Then in the __init__ file, you need to edit the previous code to introduce an application factory function. Using an application factory is essential for scalability because it allows for more complex configurations and better management as a project expands. It also makes testing much easier.

01:33 Now let’s go ahead and make the discussed changes in your project.

01:38 Let’s create a board folder and move our app.py inside the board folder.

01:47 Let’s rename app.py to __init__.py.

01:55 And now let’s make some changes to introduce an application factory.

01:59 Creating the Flask app instance directly in the global scope limits flexibility. Instead, define a function typically called create_app() that creates and configures the Flask app.

02:11 This function then returns the app instance. This seemingly small change to use an application factory pattern is a simple, yet powerful way to improve the structure and flexibility of your Flask applications.

02:23 It is a best practice for larger projects and a great habit to adopt even in smaller ones. In this lesson, you restructured your project to make it more modular and scalable by creating a board folder to organize the project, moving and renaming app.py to __init__.py, and defining an application factory to initialize the app.

02:43 In the next lesson, you’ll dive deeper into this structure by adding more functionality to your board application.

Become a Member to join the conversation.