Python

Flask hello world

Flask a web framework that can be used to build web apps with Python.

It’s very minimalist and lightweight, great for making a SaaS app.

Related course: Complete Python Web Course: Build 8 Python Web Apps

Flask Example

First install Flask, you can do so with pip.
Once installed copy the file below and save as web.py

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def helloIndex():
return 'Hello World from Python Flask!'

app.run(host='0.0.0.0', port= 81)

Start with:

python web.py

Python will show:

# python web.py 
* Running on http://0.0.0.0:81/ (Press CTRL+C to quit)

This means you have a web server running on port 81.

Open your web browser and type http://127.0.0.1:81/hello

This will show you the hello world message.

Flask hello world

Routes

We defined the route (‘/hello’) and mapped it to a function:

@app.route('/hello')
def helloIndex():
return 'Hello World from Python Flask!'

If you leave it blank you’ll have the index route.

Python hosting

The above example runs the app on your local computer, this may not be what you want.

You may want to run Flask with Nginx on a VPS.

It takes some time to setup, but then you’ve got a Python web app available to anyone.

Previous Post Next Post

Cookie policy | Privacy policy | ©

Web development

  • Python web application
  • Flask hello world