DEV Community

Cover image for Simple Hello world progrom in flask | Mega flask tutorial
Raman Bansal
Raman Bansal

Posted on

Simple Hello world progrom in flask | Mega flask tutorial

Introduction

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

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

  • 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.
  • Jinja2 is a web template engine which combines a template with a certain data source to render the dynamic web pages.
  • 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
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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

Hello world in flask mega flask tutorial techwithpie

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

Running hello world program in flask

Let's understand the code

Firstly, we import the Flask constructor from flask module.

from flask import Flask
Enter fullscreen mode Exit fullscreen mode

This flask object is our WSGI application.

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

app = Flask (__name__)
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Congratulations, you have created your first flask web application.

Read the full article :- Hello world in flask

Top comments (0)