Saturday, May 11, 2019

Python - Flask (Introduction)

While working with Python , we really need a web application framework where we can display our data.Flask was developed by a group of enthusiasts called Pocco.

Flask is a third party library used for developing web application.It has a collection of libraries and modules that enables the developer to create the web pages.

The flask installation is quite simple , we need to do simple :

pip install flask

once the flask has been installed , we will write our first code .

from flask import Flask

app = Flask(__name__)



@app.route('/')

def hello_world():

return "Hello World"



if __name__ == '__main__':

app.run()

Let us try to understand the code

from flask import Flask


-> We are importing the flask library.

app = Flask(__name__)


-> we create an instance of this class. The first argument is the name of the application’s module or package.

@app.route('/')

def hello_world():

return "Hello World"


->The route() decorator to tell Flask what URL should trigger our function.The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.

if __name__ == '__main__':

app.run()


-> The app.run() will execute the flask program

Once the program has been executed , the output will be available on the below mentioned url , normally for the local mode it will be 127.0.0.1:5000







No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...