How to create a web application using Flask in Python 3
Flask is a lightweight and flexible web framework for Python that allows building web applications quickly and easily. It focuses on simplicity and modularity, making it easy to learn and use for both small and large projects. It does not require additional tools or libraries, meaning you can start building your web application with just a few lines of code.
Below we provide a detailed manual for creating a web application using Flask in Python 3:
1. Preparation of the development environment
Before you start writing code for the web application, it is important to have an appropriate development environment. Make sure you have Python 3.x and pip (the Python package manager) installed on your system. Then, follow these steps:
-
Create a new directory for the web application on your system.
-
Open a terminal or command prompt in the newly created directory.
-
Create a Python virtual environment using the command
python3 -m venv venvin the terminal. -
Activate the virtual environment with the command
source venv/bin/activateon UNIX systems orvenv\Scripts\activateon Windows systems. -
Install Flask with the command
pip install flask.
2. Creation of the web application
Now that you have prepared the development environment, you can start writing the code for the web application. Follow these steps:
-
Create an
app.pyfile in the web application directory. -
Import Flask and create an instance of the web application. You can do it as follows:
file app.py
from flask import Flask
app = Flask(__name__)
- Create a basic route that displays a welcome message. You can do it as follows:
file app.py
@app.route('/')
def index():
return 'Welcome to my web application!'
- Add an
if __name__ == '__main__':condition at 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 it as follows:
file app.py
if __name__ == '__main__':
app.run()
- Save and close the file.
3. Running the web application
To run the web application, follow these steps:
-
Open a terminal or command prompt in the web application directory.
-
Activate the virtual environment with the command
source venv/bin/activateon UNIX systems orvenv\Scripts\activateon Windows systems. -
Run the web application with the command
python app.py. -
Open a web browser and visit http://localhost:5000/. You should see the welcome message you added previously.
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 route to show a contact page and a function that processes a contact form. You can do it as follows:
file app.py
@app.route('/contacto')
def contacto():
return 'Contact page'
@app.route('/enviar', methods=['POST'])
def enviar():
nombre = request.form['nombre']
email = request.form['email']
mensaje = request.form['mensaje']
# Process the form and send the email
return 'Message sent successfully'
In the previous example, the /contacto route shows a contact page and the /enviar route processes a form submitted by the user using the HTTP POST method.
To process the form, the request object provided by Flask is used. Information sent 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
templatesfolder in the web application directory. -
Create a template file named
base.htmlin thetemplatesfolder. This file will contain the basic structure of all the pages on 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 named
contacto.htmlin thetemplatesfolder. This file will contain the specific content of the contact page.
file contacto.html
{% extends 'base.html' %}
{% block title %}Contact{% endblock %}
{% block content %}