Ad Code

Ticker

6/recent/ticker-posts

Simple Hello world progrom in flask | Mega flask tutorial

Creating hello world program is very easy in Flask as compared to other web frameworks like Django, web2py, bottle etc. 

Simple Hello world progrom in flask | Mega flask tutorial

Introduction 

To understand flask hello world example, here are some important terms that you should know.

  1. WSGI or web server gateway interface is used in python for the development of web application. For the universal interface, It is considered as the specification between the web application and web server.
  2. Jinja2 is a web template engine which combines a template with a certain data source to render the dynamic web pages.
  3. Werkzeug is a library can be used to create WSGI compatible web applications in Python.

Installation 

Firstly, if you haven't install the flask ,then install it by running the following command.

$ pip install Flask

Hello world

After completing the the installation, let'ssee the code of the flask hello world program example.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello world!"

if __name__=="__main__":
    app.run(host="0.0.0.0", port="5000")

After running this code, you will get the output like this.

Simple Hello world progrom in flask | Mega flask tutorial

After that go to the url http://127.0.0.1:5000/ on your web browser to see the result.

Simple Hello world progrom in flask | Mega flask tutorial

Let's understand the code

Firstly, we import the Flask constructor from flask module.

from flask import Flask

This flask object is our WSGI application.

After that, we create a app variable which stores Flask constructor. 

app = Flask (__name__)

After that, you need a route for calling a python function in flask. A route tells the application which function is associated with a specific url.

@app.route("/")
def hello_world():
    return "Hello world!"

Note that the function should return something to the browser.

Now, we need to run the server at host 0.0.0.0 and at port 5000.

app.run(host="0.0.0.0", port="5000")

Congratulations, you have created your first flask web application. 

Post a Comment

0 Comments