Photo by Glenn Carstens-Peters on Unsplash
Simple Hello world progrom in flask | Mega flask tutorial
Mega flask tutorial
Table of contents
Introduction
Creating hello world program is very easy in Flask as compared to other web frameworks like Django, web2py, bottle etc.
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
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.
After that go to the url http://127.0.0.1:5000/ on your web browser to see the result.
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.
Read the full article :- Hello world in flask