In this blog, we would know about the Python Micro Web Framework Flask. It is written in Python. Flask is a small framework by best standards. It is categorized as a micro framework as it does not need specific tools or libraries. It has no database concept layer or any other mechanisms where pre-standing third-party libraries deliver common functions.
Being small does not mean that it fixes less than other frameworks. Flask was planned by way of an extensible framework. Flask helps extensions that may enhance application structures such as if they were applied in Flask the aforementioned. Extensions are present for object-relational mappers. They show up from validation, upload conduct, many open authentication technologies and some joint framework associated tools.
Flask offers a solid core with basic services. Extensions make available the rest. Because we may pick and choose the extension packages that we need. We finish up with a thin stack that has no bloat and does accurately what we require.
Flask was first designed and developed by Armin Ronacher in 2010. The Flask framework developed enthusiastically widespread in its place of Django projects through their monolithic configuration and dependencies.
Flask’s achievement shaped a whole heap of further work in issue tickets and pull requests. Armin had been managing Flask under his own Github account for many years. He finally made The Pallets Projects a collection of open-source code libraries. The Pallets Project nowadays helps as a community-driven organization. That handles Flask and other associated Python libraries for example Lektor, Jinja and some others.
There are two key dependencies of Flask.
- Web Server Gateway Interface (WSGI) subsystems, routing and debugging originate from Werkzeug.
- Whereas template help is provided by Jinja2.
Werkzeug and Jinja2 are written by the core developer of Flask.
There is no built-in support in Flask for retrieving databases, authenticating web forms, verifying users, or additional high-level tasks. These and various other important services utmost web applications need are available overextensions. That fit in with the core packages. We have the power to select the extensions as a developer. That does work best for the project or straight write the own if we feel motivated to. This is the indifference with a bigger framework. In a larger framework, most picks have been made for us. They are unbreakable or occasionally terrible to change.
Why choose a Flask web framework?
Flask is well-thought-out more Pythonic than the Django web framework. This is as in common circumstances the same Flask web application is extra explicit. Flask is also easy-going, to begin with as a beginner. As there is a small boilerplate code for receiving a simple app up and running.
We have already discussed that micro-framework Flask is part of the Pallets Projects. Therefore, it is based on many other of them.
Werkzeug word is used in German for the tool. For the Python programming language, it is a utility library. We can also say it is a toolkit for Web Server Gateway Interface (WSGI) applications. It is approved under a BSD License. Werkzeug may understand software objects for application, reply, and utility functions. It may be used to shape a convention software framework on top of it and helps Python.
Jinja was created by Armin Ronacher. The Python programming language is a web template engine. It is approved under a BSD License. Jinja is alike to the Django template engine. It delivers Python-like words though making sure that the templates are assessed in a sandbox. It is a text-based template language. It may be used to make any markup along with the source code.
The Jinja template engine enables customization. That is of labels, filters, tests, and globals. It permits the template designer to call functions with arguments on objects.
MarkupSafe is a string control library for the Python programming language. It is registered under a BSD license. The eponymous MarkupSafe type covers the Python string type. It scripts its contents as safe. That combines the MarkupSafe with fixed strings automatically escapes the unmarked strings. Though, it evades double escaping of previously marked strings.
ItsDangerous is a safe data series library for the Python programming language. It is certified under a BSD license. It is used to store the session of a Flask application in a cookie without permitting users to interfere with the session contents.
We require the only computer with Python installed for the installation of the Flask.
The most suitable method to install Flask is to use a virtual environment. That is a private copy of the Python interpreter onto which we may install packages confidentially. That is done without upsetting the global Python interpreter installed in our system. Virtual environments are very valuable as they stop package clutter and version clashes in the system’s Python interpreter.
Making a virtual environment for every application make sure that applications have contact with only the packages that they use. Though, the global interpreter remains well-ordered and clean. It helps only as a source from which more virtual environments may be shaped. The virtual environments don’t need administrator rights as an added advantage.
Type the below command to check whether we have installed it in our system:
$ virtualenv --version
We will have to install the utility if we get an error.
Furthermost Linux distributions give a package for virtualenv. For instance, Ubuntu users may install it with this command:
$ sudo apt-get install python-virtualenv
For Mac OS X, we can install virtualenv using easy_install:
$ sudo easy_install virtualenv
We have a slightly more complicated install procedure for Microsoft Windows or any operating system that does not make available an official virtualenv package.
- Navigate to https://bitbucket.org/pypa/setuptools using the web browser, the home of the setuptools installer.
- Look for a link to download the installer script on that page.
- Save the file of the script called ez_setup.py.to a temporary folder on the computer.
- Run the following commands in that folder:
$ python ez_setup.py $ easy_install virtualenv
- We have to need to create the folder that would host the example code that is obtainable from a GitHub repository.
- Download the example code from GitHub with the below commands.
$ git clone https://github.com/miguelgrinberg/flasky.git $ cd flasky $ git checkout 1a
- Initialize the application folder to version 1a, the initial version of the application.
- Next, create the Python virtual environment inside the flasky folder using the virtualenv command.
- The name of the virtual environment is a single argument for this command.
- A folder by the selected name would be created in the current directory.
- All files related to the virtual environment will be inside.
- A usually used naming pact for virtual environments is to call them venv:
$ virtualenv venv
New python executable in venv/bin/python2.7
Also creating executable in venv/bin/python
- We have a venv folder inside the flasky folder with a brand-new virtual environment. That holds a private Python interpreter.
- We have to activate it to start using the virtual environment.
- For Linux and Mac OS X users, we may activate the virtual environment with this command if we are using a bash command line :
$ source venv/bin/activate
- For Microsoft Windows, the activation command is:
- The location of the Python interpreter is added to the PATH when a virtual environment is activated.
- This change is not permanent. It upsets only the current command session.
- As we have activated a virtual environment, the activation command adapts the command prompt to take in the name of the environment:
- Type deactivate at the command prompt after working with the virtual environment and returning to the global Python interpreter.
- Use the below command to install Flask into the virtual environment:
(venv) $ pip install flask
- Flask and its dependencies are installed in the virtual environment with this command.
- We can verify that Flask was installed correctly by starting the Python interpreter and trying to import it:
(venv) $ python >>> import flask >>>
- Congratulation, if no errors appear.