Building Your First Flask application

Building Your First Flask application

Objective

To get started by flask by running the server on localhost on building your first flask application.

Prerequisite

  • Python installed in your system.
  • VS Code or any other code editor

Getting Started

Setting up the environment

Create a folder and open it in VS Code and create a python file with name of your choice. I will be naming my python file app.py.

Install flask module

Install flask module by executing the following command in your terminal:

pip install flask

Main Code

Import Flask module

from flask import Flask

After importing the module, create an object of Flask class.

app = Flask(__name__)

Now create the default route. The default root is at '/'. The route is defined by the app.route() decorator. Here app is our object of flask.

@app.route('/') #decorator drfines the   
def home():  
    return "Congrats ! This is your first flask app";

The function will return the text which will be displayed on when the application is executed and the server is running. Now we can start the server

if __name__ == '__main__':
    app.run(debug = true)

In your browser, go to the address localhost:5000.

Screenshot (112).png