You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
14 Hrs
33 Min
51 Seg
Curso de Flask

Curso de Flask

Luis Mart铆nez

Luis Mart铆nez

Estructura de proyectos en Flask

10/18
Resources

Code refactoring is an essential practice for any developer looking to keep their projects scalable and maintainable. When working with frameworks like Flask, properly organizing our code not only improves readability, but also facilitates teamwork and the implementation of unit tests. In this article, we will explore how to transform a basic Flask application into a more professional and maintainable framework.

How to prepare our repository for professional development?

Before we start refactoring our code, it is important to make sure that our repository is properly configured. One of the first steps is to create a .gitignore file to avoid uploading unnecessary files to the repository.

Why is the .gitignore file important?

When we work with virtual environments and local databases, they generate files that should not be part of our repository. To solve this:

  1. Create a file called .gitignore in the root of your project.
  2. You can use predefined templates from GitHub for Python.
  3. Add extensions specific to your project, such as *.sqlite* to ignore SQLite database files.

This step is essential to keep your repository clean and avoid unnecessary conflicts when working in a team.

How to implement the PEP 8 standard in our code?

PEP 8 is the style standard for Python code that helps us maintain a consistent and readable structure. To implement it:

  1. Install tools like Ruff, which integrates PEP 8 and other utilities.
  2. Organize your imports at the beginning of the file.
  3. Avoid long lines by splitting them properly.
from flask import ( Flask, render_template, request, url_for, flash, redirect)

Code readability is crucial when working in development teams, as it makes it easier for other developers to understand and modify.

How to structure a Flask application to make it scalable?

A well-structured Flask application must clearly separate its components. Let's see how we can refactor our application to achieve this.

How to separate the application configuration?

It is advisable to keep the application configuration in a separate file:

  1. Create a config.py file.
  2. Define a Config class that contains all the configuration parameters.
import os
class Config: SECRET_KEY = 'your_secret_key' SQLALCHEMY_DATABASE_URI = f "sqlite:///{os.path.join(os.path.dirname(__file__ ), 'database.db')}" # Other configurations
  1. In your main file, load the configuration:
from config import Config
app = Flask(__name__)app.config.from_object(Config).

This separation allows you to modify the configuration without having to touch the main application code, which is especially useful when you have different environments (development, test, production).

How to organize the data models?

Data models should be in a separate file or module:

  1. Create a models.py file.
  2. Move your model definitions to this file.
from flask_sqlalchemy import SQLAlchemy
 db = SQLAlchemy()
class Note: # Model definition
  1. Import the models into your main file:
from models import Note, db
 # Initialize the database with the applicationdb.init_app(app).

If your application grows and you have many models, consider creating a models module with separate files for each logical domain.

What to do with views and routes?

Although not fully covered in the class, a good practice is to organize views using Flask Blueprints:

  1. Group related views together (e.g., all note operations).
  2. Remove test views that you no longer need.
  3. Organize the code so that each view function is clear and has a single responsibility.

Blueprints are a great way to modularize your Flask application, allowing you to break your application into smaller, more manageable components.

Code refactoring is an ongoing process that improves the quality of your application. By following these practices, you will not only make your code more maintainable, but also make it easier to collaborate with other developers and implement automated tests. Have you tried refactoring any of your applications? Share your experience in the comments.

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?