DEV Community

Cover image for Getting Started with Flask
Victoria Mutai
Victoria Mutai

Posted on • Edited on

Getting Started with Flask

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

  1. It is lightweight, with minimal dependencies.
  2. It is simple and easy to learn and use.
  3. It is flexible.
  4. It has very good documentation.

Getting started

  1. Create a virtual environment for your project using the venv command.
    python -m venv virtual-name

  2. Install flask into your project using the pip command,after creating the virtual environment.
    pip install flask

  3. 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!')
Enter fullscreen mode Exit fullscreen mode

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():
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

HTML code

<p>Different python modules: </p>
<ul>
    {% for framework in frameworks %}
    <li>{{framework}}</li>
    {% endfor %}
</ul>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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 %}
Enter fullscreen mode Exit fullscreen mode

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)