Getting started with Flask - "Hello World Wide Web!"

chrislutz

Christian Lutz

Posted on April 7, 2019

Getting started with Flask - "Hello World Wide Web!"

Creating web apps is possibly one of the coolest things you could do in your lifetime. ;)
The reasons are obvious.
You can create something meaningful, everyone can use.

With some work, you may be creating something like this:
Picture of ServerMonitor project

Follow me to get more information about the ServerMonitor in the near future.

Let's get started!


Introduction

Flask is a lightweight Python web framework based on Werkzeug and Jinja2.
Lightweight means, Flask only has the most basic features to create your web application on your own.
Everything other like admin panels, for example, have to be built from scratch, unlike in Django.

For starting out in web development with Python, this makes a lot of sense.
You don't have to learn a lot of framework stuff before you can build a web app.

Create your project

Make a folder where your Flask project should be located.
You can do this in CMD on Windows.

mkdir flask_project
Enter fullscreen mode Exit fullscreen mode

Then, open your created folder with:

cd flask_project
Enter fullscreen mode Exit fullscreen mode

Installation

Installing flask via PIP is quite easy.
Simply type...

pip install Flask
Enter fullscreen mode Exit fullscreen mode

...to install the Flask module.

Setting up the application

After installing Flask, we have to make a Python file.
I will call it 'main.py'

This file will contain all of the basic code for Flask to run.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello_www():
    return "Hello World Wide Web!"
Enter fullscreen mode Exit fullscreen mode

Code explanation

First of all, we import our installed Flask module from Flask with:

from flask import Flask
Enter fullscreen mode Exit fullscreen mode

Then, we define the variable 'app' and assign "Flask(_ name _)" to it:

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

After that, we can create our first route with:

@app.route("/")
Enter fullscreen mode Exit fullscreen mode

To make it work, we need some return value, that will be displayed on our web page.

For that, we type:

def hello_www():
    return "Hello World Wide Web!"
Enter fullscreen mode Exit fullscreen mode

Last but not least, we will return a String with "Hello World Wide Web!".

Run your application

After we have created and saved our code, we want to see the result.
For that, you first need to define your Python file for Flask in this way:

set FLASK_APP=main.py
Enter fullscreen mode Exit fullscreen mode

If this is done, you can start your web app with:

flask run
Enter fullscreen mode Exit fullscreen mode

Flask running in CMD

Go to http://localhost:5000 or http://127.0.0.1:5000, to see your result:

Your result!

Congratulations!
You have created your first web app.

If you want to dive deeper into the Flask-World, visit http://flask.pocoo.org/.

Thank you for reading!

💖 💪 🙅 🚩
chrislutz
Christian Lutz

Posted on April 7, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related