Flask is a web framework of python programming language. It is mainly used in the back-end to manage server side response for a website. In 2004, Armin Ronacher created Flask as a project that unexpectedly became popular over time. It was developed by the Pocoo teams who also produced Werkzeug and Jinja frameworks. Let’s dig little deeper and explore how flask works, components of flask, its uses, how to be web developer by learning flask, can you get a job by learning flask.
What is flask?
‘flask’ is a python based web application developement framework which is used to develop websites using python programming language. Flask is mainly used to control the behaviour of the site i.e. backend or serverside. It manages requests on the site and process them. For example – If you want to show ‘welcome to my site’ text in the body area of the home page of the site and a contact form in the body section when user clicks on the ‘contact us’ page of the site without changing anything else, so it all can be done very easily using Flask.
‘flask’ is a python library that includes lot of classes and functions that helps to develop back-end part of a website.
from flask import Flask
app = Flask(__name__)
flask = 'Module name'
Flask = 'Class name'
What is Web server gateway interface (WSGI)?
WSGI (Web Server Gateway Interface) serves as a bridge connecting web servers, with Python based web applications and frameworks to ensure communication between them across various servers and frameworks.It acts as a layer that enables any application compliant, with WSGI to operate on any server that also adheres to the WSGI standards.
Simply WSGI permits your Python script (like Flask or Django or any web framework of your choice) to communicate with a web server (such as Gunicorn, Apache etc.). It guarantees the transmission of web requests, from the server to your application and in turn ensures that your application responds in a manner.
from flask import Flask, render_template
app = Flask(__name__) #WSGI
@app.route('/home') #WSGI route decorator
def index():
print('<h1>welcome to my site</h1>')
Explanation of app = Flask(name)
This line is the foundation of a Flask web app.
Flask: It’s the framework’s main class, doing the heavy lifting of handling web requests and responses.
name: A special Python variable that represents the name of the current file. Flask uses this to figure out the app’s location and where to find resources like templates.
app: The variable that holds your app’s instance, which you use to define routes and start the server.
In short, app = Flask(__name__) sets up the Flask app and lets Flask know where everything is.
What is Werkzeug?
Werkzeug is python library designed to handle requests on the server. The WSGI discussed above is made using Werkzeug library and uses its functionality in flask. Werkzueg is comprehensive and provides tools for handling requests, responses and routing in web frameworks. It was also developed by Pocoo, the team who developed flask. Werkzueg also has security features like safe cookie handling and request validation.
What is Jinja?
Jinja is a library in python. Jinja is a templating engine. It adds a functionality in python through which you can associate html files to a url of your website. The function used for this is ‘render_template()’. Let’s understand this through the pic and code below. It is also used tointerchange variables between html and python. Jinja templating enables to code in html using python syntax.
Python file - app.py
app = Flask(__name__) #WSGI
@app.route('/home') #WSGI route decorator
def index():
render_template('home.html')
html file - home.html
<html>
<head>
<title>
home
</title>
</head>
<body>
<h1>welcome to my site</h1>
</body>
</html>
Now, when anyone will visit ‘yoursite.com/home‘ then he will be shown ‘home.html’ file on the browser.
Popular uses of Flask
As we already discused that flask is a web application or website development framework used for web development using python and that’s the main use of flask. flask handles the server side responses for a website.
Some popular websites that uses flask in back-end processes, mainly to handle microservices or APIs:
- Netflix
- MIT OpenCourseWare
- Airbnb
- Uber
Popular Libraries and Modules used in Flask
- Jinja (Templating Engine)
- Werkzeug (WSGI Toolkit)
- Flask-SQLAlchemy (ORM Integration)
- Flask-Migrate (Database Migrations)
- Flask-WTF (Forms Handling)
- Flask-Login (User Authentication)
- Flask-Mail (Email Integration)
- Flask-Bcrypt (Password Hashing)
- Flask-CORS (Cross-Origin Resource Sharing)
- Flask-RESTful (Building REST APIs)
- Flask-SocketIO (WebSockets)
Website Developer Roadmap through flask & Python
I will keep this simple and brief. Follow following steps to become a python flask web developer:
- Learn fundamentals of HTML, CSS
- Learn Fundamentals of Python
- Learn Flask
- Start creating websites
- Learn Javascript (optional)
How to get job after learning Flask?
You have to look for jobs which demands specifically Flask.
Is Flask and Python sufficient to get a job?
No, Flask and Python are not enough to get a job as a website developer in today’s scenario. there is lot of competition in website development field today.