How to create a web application using Flask in Python 3
Flask is a lightweight and flexible web framework for Python that allows you to build web applications quickly and easily. It focuses on simplicity and modularity, making it easy to learn and use for small and large projects. It requires no additional tools or libraries, which means you can start building your web application with just a few lines of code.
Here is a detailed manual for creating a web application using Flask in Python 3:
1. Preparing the development environment
Before you start writing code for your web application, it is important to have a suitable development environment. Make sure you have Python 3.x
and pip
(the Python package manager) installed on your system. Then, follow the steps below:
-
Create a new directory for the web application on your system.
-
Open a terminal or command console in the newly created directory.
-
Create a Python virtual environment using the python3 -m venv venv
command in the terminal.
-
Activate the virtual environment with the command source venv/bin/activate
on UNIX systems or venv\Scripts\activate
on Windows systems.
-
Install Flask with the command pip install flask
.
2. Creating the web application
Now that you have prepared the development environment, you can start writing the code for the web application. Follow the steps below:
-
Create an app.py
file in the web application directory.
-
Import Flask and create an instance of the web application. You can do this in the following way:
file app.py
from flask import Flask
app = Flask(__name__)
- Create a basic route that displays a welcome message. You can do this in the following way:
file app.py
@app.route('/')
def index():
return '¡Bienvenido a mi aplicación web!'
- Add an
if __name__ == '__main__':
condition to the end of the file to ensure that the web application only runs if the file is executed directly and not if it is imported into another file. You can do this in the following way:
file app.py
if __name__ == '__main__':
app.run()
- Save the file and close it.
3. Running the web application
To run the web application, follow the steps below:
-
Open a terminal or command console in the web application directory.
-
Activate the virtual environment with the command source venv/bin/activate
on UNIX systems or venv\Scripts\activate
on Windows systems.
-
Run the web application with the python app.py
command.
-
Open a web browser and visit http://localhost:5000/. You should see the welcome message you added earlier.
4. Adding more functionality to the web application
Now that you have created a basic web application, you can start adding more functionality to it. You can do this by adding more routes and functions to the app.py
file. For example, you can add a path to display a contact page and a function that processes a contact form. You can do this in the following way:
file app.py
@app.route('/contacto')
def contacto():
return 'Página de contacto'
@app.route('/enviar', methods=['POST'])
def enviar():
nombre = request.form['nombre']
email = request.form['email']
mensaje = request.form['mensaje']
# Procesar el formulario y enviar el correo electrónico
return 'Mensaje enviado correctamente'
In the example above, the /contacto
path displays a contact page and the /enviar
path processes a form submitted by the user using the HTTP POST method.
To process the form, the request
object that comes with Flask is used. The information submitted by the user can be obtained from the request.form
object, which is a dictionary containing the form data. In this example, the values of nombre
, email
and mensaje
are obtained from the form.
Then, the form is processed and a confirmation message is sent to the user.
5. Using templates
When building more complex web applications, it can be useful to use templates to create the HTML content of the pages. Flask uses the Jinja2 template engine to do this.
To use templates in your web application, follow these steps:
-
Create a templates
folder in the web application directory.
-
Create a template file called base.html
in the templates
folder. This file will contain the basic structure of all the pages of your website.
file base.html
<!doctype html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
- Create a template file called
contact.html
in the templates
folder. This file will contain the specific content of the contact page.
file contacto.html
{% extends 'base.html' %}
{% block title %}Contacto{% endblock %}
{% block content %}
Contacto