Flask is one of the best Python web frameworks. It is the second most starred Python web framework (Django is the most starred Python web framework). Flask is also known as a Python micro-framework; the prefix “micro” specifies that it does not require particular libraries or modules. Like Django which is a full-stack web framework, Flask does not include batteries such as database abstraction layer, form validation, built-in authentications, and any other pre-built libraries.
As a micro-framework Flask come with minimal dependency, however, it supports many extensions that can be added further to any Flask project to increase the functionality of the web application.
What is a Python Web Framework?
In General, a web Framework is a collection of modules and libraries that can be used to create web applications or dynamic websites. The same goes for the Python web frameworks, in Python web frameworks we use Python as a programming language to build the web applications and write the back-end for a website. With the Framework, we do not have to worry about the nitty-gritty workings of the web terminologies such as to request, protocols, multi-threading, and server running of instances, with the framework we can focus on the logic of the application rather than writing script for all the web terminologies and protocols.
There are many Python web frameworks, here is the list of most popular Python frameworks.
Components of Flask
In 2004 Armin Ronacher created Flask as a Python framework under Pocoo project. The Flask project started as an April fool joke but end up with a serious application. Under the same Pocoo projects two dependencies were also created as a base for the Flask.
Werkzeug is a Web Server Gateway Interface (WSGI) toolkit library for the Python programming language. It acts as an interface between the Flask application and server and handles the request-response between the client and the server. Because of Werkzeug, we do not have to worry about the working mechanism of web applications and protocol, Werkzeug handles all for us.
jinja2 is a templating engine that can embed some dynamic data on static pages like HTML and XML. With jinja2 we can render all the data from the backend python script to the front end HTML or XML pages.
Why use Flask
Flask is a lightweight framework that has few dependencies which allow its developers to write flexible code. Flask is widely used to write RESTful APIs and small web applications. Due to its low learning curve, many Python developers learn Flask before any other full-stack Python web framework.
There are many popular companies that use Flask for their back end scripting
- Red Hat
Features of Flask
- It comes with a built-in development server.
- It also supports a debugger to debug the code.
- It also comes with integrated unit testing.
- It uses Jinja2 for templating.
- It also provides secure cookies for client-side sessions.
- It has a low learning curve.
- As compared to Django it is easy to learn.
- It also has Google App engine compatibility.
- It supports many third-party extensions for better functionalities.
Flask vs Django
Django is the biggest rival of Flask; it’s always a hard choice for a new Python web developer to choose between these two popular Python web frameworks.
Django is a Full Stack Web Framework that comes with many dependencies and built-in features, on the other Flask is a microframework with few dependencies and not many features.
Django is batteries included means it comes with many built-in features like Object Relational Mapping (ORM), built-in authentication, context pre-processor, built-in user model, template engine, etc, but with Flask we have to write code for all these features or we can install additional Python extensions or libraries.
So which one is better, for a beginner Django can be overwhelming so it’s always suggested to learn Flask first. Django follows a strict path and manipulating core code can be a daunting task, but Flask is flexible and we can write and code whatever we want.
There is no doubt that Django is a powerful tool and the first choice for big web applications, but Flask also has its domain and market and widely used for small applications and RESTful APIs.
To know more about the difference between flask and Django click here.
Getting Started With Flask
Before we write our first Python Flask Hello World program we need to install Flask for our Python environment.
To install flask we can run the pip install command on our terminal or command prompt
pip install flask
Now let’s create a Python file app.py for our first Python web Hello World program.
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello World!' if __name__ =='__main__': app.run()
Now run the file on the terminal or command prompt.
$ python app.py * Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now open the http://127.0.0.1:5000/ on your web browser, you will see a similar Hello World output.
By default the debug mode is off, we can set it on, by setting the debug=True.
People are also reading:
- What is CherryPy Python?
- How to Make Port Scanner in Python?
- Download all Images from a Web Page in Python
- How to Manage Files in Python?
- How to Translate Languages in Python?
- Face Detection in Python
- Image Transformations in Python
- Assembly, Disassembly, and Emulation Using Python
- Automated Browser Testing in Python
- Google Custom Search Engine API in Python