In this article, you will learn about flask, why you should use it, its features, and how to get started with it.
Introduction
Flask is a python framework that provides tools for building web applications quickly and more efficiently.
Reasons for using flask
- It is lightweight, with minimal dependencies.
- It is simple and easy to learn and use.
- It is flexible.
- It has very good documentation.
Getting started
Create a virtual environment for your project using the venv command.
python -m venv virtual-name
Install flask into your project using the pip command,after creating the virtual environment.
pip install flask
Create a new flask application. Create a new file called main.py and add code to start application. It is important to note that to create a new flask application, import Flask and use:
app = Flask(__name__)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
print('Lets learn flask!')
Flask Routes
To access the different pages, use the @app.route decorator. Routing accepts the different HTTP methods, GET is the default flask route. This is how routes look like:
@app.route('/login',methods=['GET','POST']
def login():
Flask Templates
Flask uses the Jinja2 as its template engine. Create a template directory to store your html files. To import use the render_template. With templates you can reuse common elements eg layouts.
@app.route('/')
def index():
return render_template('index.html')
Conditional Formatting
Flask templates allow us to loop through different variables and also use conditional formatting.
Flask code
The sample code below passes a variable frameworks to the template and using the conditional formatting and html lists, it prints out the data on an unordered list.
@app.route('/')
def index():
frameworks = ['Django','Flask']
return render_template('index.html',frameworks=frameworks)
HTML code
<p>Different python modules: </p>
<ul>
{% for framework in frameworks %}
<li>{{framework}}</li>
{% endfor %}
</ul>
Reusing Components
To reuse component eg layouts, use extends
keyword. This ensures a consistent look and feel across all your web pages. Lets do this.
Create a layout.html
file and put this code:
<!DOCTYPE html>
<html>
<head>
<title>Flask Lesson</title>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
<div class="content">R
{% block content %}{% endblock %}
</div>
</body>
</html>
On the index.html
file, add the extends keyword and put the containt into the{% block content %}{% endblock %}
{% extends 'layout.html' %}
{% block content %}
<h1>Lets talk flask</h1>
<p>Different python modules: </p>
<ul>
{% for framework in frameworks %}
<li>{{framework}}</li>
{% endfor %}
</ul>
{% endblock %}
To sum it up, Flask provides many useful features, including routing, templates, and support for various extensions, making it easy to build and maintain web applications.
In upcoming blogs, we will deep dive into some of the topics we covered in this blog.
Stay tuned to more flask content!
Top comments (0)