Flask


Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons. Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins. In the case of Flask, its dependencies are:

Advantages of Flask:

  • Higher compatibility with latest technologies.
  • Technical experimentation.
  • Easier to use for simple cases.
  • Codebase size is relatively smaller.
  • High scalability for simple applications,
  • Easy to build a quick prototype.
  • Routing URL is easy.
  • Easy to develop and maintain applications.

Disadvantages of Flask:

  • Not suitable for big applications.
  • Community.
  • Full-Stack experience.
  • No admin site.
  • No login or authentication.
  • ORM.
  • Migrations can be difficult.

Flask is a web application framework written in Python. It is developed by Armin Ronacher, who leads an international group of Python enthusiasts named Pocco. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.

  • Classifications: Web framework
  • Programming languages used: Python
  • Developer: Armin Ronacher

Python Version


We recommend using the latest version of Python. Flask supports Python 3.6 and newer. async support in Flask requires Python 3.7+ for .contextvars.ContextVar

Dependencies


These distributions will be installed automatically when installing Flask.

  • Werkzeug implements WSGI, the standard Python interface between applications and servers.
  • Jinja is a template language that renders the pages your application serves.
  • MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
  • ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
  • Click is a framework for writing command line applications. It provides the command and allows adding custom management commands.flask


Optional dependencies


These distributions will not be installed automatically. Flask will detect and use them if you install them.

  • Blinker provides support for Signals.
  • python-dotenv enables support for Environment Variables From dotenv when running commands.flask
  • Watchdog provides a faster, more efficient reloader for the development server.


Virtual environments


Use a virtual environment to manage the dependencies for your project, both in development and in production.

What problem does a virtual environment solve? The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.

Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.

Python comes bundled with the venv module to create virtual environments.


Create an environment


Create a project folder and a venv folder within:

Windows

                            
                                > mkdir myproject
                                > cd myproject
                                > py -3 -m venv venv
                            
                            

macOS/Linux

                            
                                $ mkdir myproject
                                $ cd myproject
                                $ python3 -m venv venv
                            
                            


Activate the environment


Before you work on your project, activate the corresponding environment:

Windows

                            
                                > venv\Scripts\activate
                            
                            

macOS/Linux

                            
                                $ . venv/bin/activate
                            
                            

Your shell prompt will change to show the name of the activated environment.


Install Flask


Within the activated environment, use the following command to install Flask:

                            
                                $ pip install Flask
                            
                            

Flask is now installed. Check out the Quickstart or go to the Documentation Overview.

Getting Started

Programming using the Flask engine is fairly simple. After importing the Flask object from the flask module, you will need to set this object equal to '__main__' to get it to run. Within the Flask object, you can use the config property to set any important variables to the application, such as file paths or other constant variables.

                
                    
                
            

Creating a Layout

Webpages in Flask use a standard '.html' extension, unlike other popular web frameworks these days. However, it is simple to give your website a common look and feel using Flask. Start out with a base layout that you want your web pages to inherit. This could be named anything. In our case, we called ours 'base.html'. Then inside the main div, you can use the { % block content %} { % endblock % } to populate content inside that div from your child pages.

                
                    
                
            

Creating a webpage

Webpages in Flask need the line { % extends "base.html" %} to be able to use a base layout page. They also need the content to be contained within the { % block content %} and { % endblock %} tags. Otherwise, building a webpage in Flask is differs very little from traditional web development.

In your Python Flask script, you can also define custom routing for your webpages. To do this, place the annotation @app.route(yourpage) above a function, and then within that function, return the desired webpage using render_template().

                
                    
                
            

Routing and Request Methods

In your Python Flask script, you can also define custom routing for your webpages. To do this, place the annotation @app.route(yourpage) above a function, and then within that function, return the desired webpage using render_template(). You can also use the @app.route annotation to define if the page should accept any request methods like GET, POST, PUT, or DELETE.