Did you know that just a few lines of code can turn your idea into a working web application? If you are curious about web development with Python, Flask may be the perfect tool for you. Before exploring its simplicity and power, however, you need to know how to configure it correctly, which practices work best, and what will help your application stand out. Keep reading and, by the end, you will have what you need to start building web applications with confidence.
What is Flask and why use it?
Flask is a minimalist Python web framework that lets you create web applications in a simple and scalable way. Its “batteries not included” philosophy gives you the freedom to choose the tools and libraries that best fit your project. It is suitable for beginners and experts alike, combining flexibility with power.
Some of its main advantages are:
-
Simplicity: A gentle learning curve for beginners.
-
Extensibility: Add functionality as you need it through extensions.
-
Lightweight design: It does not impose a rigid structure or predefined tools.
Basic Flask setup
1. Installing Flask
To get started, you need to install Flask in your Python environment. We recommend creating a virtual environment to avoid conflicts between projects.
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
# Windows:
venv\Scripts\activate
# Mac/Linux:
source venv/bin/activate
# Install Flask
pip install flask
2. Your first Flask application
In a file named app.py, write the following:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
What does each part mean?
-
Flask(__name__): Creates an application instance.__name__helps Flask locate resources. -
@app.route("/"): Defines a route for your application. It tells Flask that thehomefunction should run when someone visits the main URL (/). -
app.run(debug=True): Starts the server in development mode and displays detailed error messages.
Run your application with:
python app.py
Open your browser and go to http://127.0.0.1:5000. Your first Flask application is running.
Extending your application’s functionality
1. Dynamic routes
You can capture URL parameters to customize responses.
@app.route("/user/<name>")
def user_greeting(name):
return f"Hello, {name.capitalize()}!"
Visit /user/john to see a personalized message.
2. Handling forms
If you want to receive data from users, Flask makes form handling easy with methods such as GET and POST. For this, use the render_template function and work with HTML templates.
Create a basic HTML form
First, create a folder named templates in your project. Inside that folder, create a file named form.html with the following content:
Create an HTML form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form action="/process" method="post">
<input type="text" name="name" placeholder="Your name" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
Update your Flask application to handle the form:
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route("/form")
def form():
return render_template("form.html")
@app.route("/process", methods=["POST"])
def process():
name = request.form.get("name")
return f"Thanks for submitting your name, {name.capitalize()}!"
if __name__ == "__main__":
app.run(debug=True)
Explanation
-
render_template("form.html"): Looks for an HTML file inside thetemplatesfolder and renders it. -
request.form.get("name"): Retrieves the value of the form field submitted through thePOSTmethod.
With this, you can access the form at http://127.0.0.1:5000/form.
A short guide to Flask’s file structure
As your application grows, organizing it properly becomes essential. A typical structure for small or medium-sized Flask applications looks like this:
my_project/
│
├── app.py # Main file containing the Flask application
├── static/ # Static files such as CSS, JavaScript, and images
│ ├── css/
│ ├── js/
│ └── img/
├── templates/ # HTML templates
│ ├── base.html # Base template for inheritance
│ ├── form.html
│ └── another_template.html
└── requirements.txt # Dependency list
Folder description
-
static/: Store static resources such as CSS stylesheets, JavaScript files, and images here. -
templates/: The folder for HTML templates rendered withrender_template. -
Main file (
app.py): Contains the application’s main logic.
Advantages of this structure
-
Easy maintenance: Keeps files organized, especially in larger applications.
-
Modularity: Clearly separates resources, templates, and logic.
For more complex projects, you can split the application into modules using an application factory pattern, but this structure is ideal for getting started.
With this structure, your Flask application will be easier to manage and scale.
Flask best practices
-
Organize your project: Split your application into multiple files as it grows.
-
Create folders for routes, models, and templates.
-
Use the “Factory” pattern to manage configuration in larger projects.
-
-
Keep configuration separate: Use environment variables for sensitive information such as API keys.
import os
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "clave-predeterminada")
- Use extensions as needed:
-
Flask-SQLAlchemy for databases.
-
Flask-Migrate for migrations.
-
Flask-WTF for more advanced forms.
- Optimize performance:
-
Use a server such as Gunicorn in production.
-
Enable caching with tools such as Flask-Caching.
Flask: A first step toward professional web development
As you have seen, Flask is a powerful and simple tool for getting started with Python web development. From creating a basic application to implementing forms and handling dynamic routes, this framework guides you without unnecessary complexity.
Ready for the next step? Experiment with HTML templates using Jinja2, connect Flask to a database, or implement a REST API. The only limit is your imagination.
What will you build with Flask today?

