Skip to content

What is Flask in Python?

Flask is a python web framework using which you can build web applications. It was developed by Armin Ronacher and is based on the Jinja2 template engine and Werkzeug WSGI (Web Server Gateway Interface) toolkit.

Using flask, the python scripts are implemented as backend service, and the output is displayed on the webpage. You can even create a rest API endpoint using a flask. Companies such as Netflix, Uber, Mozilla, and Airbnb, among others, use Flask.

Here are the advantages and disadvantages of using Flask.

Flask advantagesFlask disadvantages
Lightweight, small, and easy to understand.Higher maintenance cost
ScalableTough to maintain the application if it is a large project.
Compatible with the latest technologiesSmaller community. Thus, you will not find too extensive resources.

Also read: How does import work in Python?


How to create a basic Flask application?

You should use python 3.0 or higher to create a flask application. The example code given below shows a very basic example of a python flask application.

from flask import Flask  
  

#creating the Flask class object 
app = Flask(__name__)   
 
@app.route('/')
def home():  
    return "Hello, this is a flask application.";  

@app.route('/greetings')
def route():
    return "Welcome, hope you have a good day!";
  
if __name__ =='__main__':  
    app.run(debug = True)  

Fir, we import the flask library and then create a flask object. In the next step, you create some functions and map them to a route using ‘@app.route(‘/’)’. In the end, we are passing the module name to the flask constructor. The run() method runs the flask application.

If you set the value of ‘debug’ as ‘True’, the debugger is turned on else, it is off by default. You can also specify the port number by providing ‘port = <port number>’ inside your app.run() method.

When you will run the code written above, you will be able to see the message “Hello, this is a flask application.” in your browser (localhost:5000). Add ‘/greetings’ to your URL, and then you will be able to see the message “Welcome, hope you have a good day!”.

The output of the code written above for the URL ‘http://localhost:5000’ is below.

What is Flask in Python?

The output of the code written for URL ‘http://localhost:5000/greetings’ is shown below.

What is Flask in Python?

This is a very basic example of a flask application. You can also create websites using the flask framework.

Also read: What is an Abstract class in Python?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

We may earn a commission if you buy something from a link on this page. Thanks for your support.







>