Introducci贸n a Flask
驴Qu茅 es Flask?
Creando tu primer "Hello, World" en Flask
Rutas y Plantillas con Jinja
Rutas y vistas en Flask
Plantillas con Jinja en Flask
Manejo de Formularios y Datos
Manejo de formularios en Flask
Bases de datos: SQLite y SQLAlchemy
CRUD: Crear y leer datos en Flask
CRUD: Actualizar datos en Flask
CRUD: Eliminar datos en Flask
Organizaci贸n del Proyecto y Mejoras
Estructura de proyectos en Flask
Blueprints en Flask
Notificaciones con Flash Messages
Integraci贸n de TailwindCSS en Flask
Seguridad y Testing
Gesti贸n de sesiones en Flask
Manejo de cierre de sesi贸n en Flask
Validaci贸n de formularios en Flask
Pruebas unitarias en Flask
Pr贸ximos pasos en Flask
You don't have access to this class
Keep learning! Join and start boosting your career
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.
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.
When we work with virtual environments and local databases, they generate files that should not be part of our repository. To solve this:
.gitignore
in the root of your project.*.sqlite*
to ignore SQLite database files.This step is essential to keep your repository clean and avoid unnecessary conflicts when working in a team.
PEP 8 is the style standard for Python code that helps us maintain a consistent and readable structure. To implement it:
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.
A well-structured Flask application must clearly separate its components. Let's see how we can refactor our application to achieve this.
It is advisable to keep the application configuration in a separate file:
config.py
file.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
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).
Data models should be in a separate file or module:
models.py
file.from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Note: # Model definition
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.
Although not fully covered in the class, a good practice is to organize views using Flask Blueprints:
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
Want to see more contributions, questions and answers from the community?