Flask Web Development Part 1: Hello World App

Shawn Hymers
Analytics Vidhya
Published in
3 min readApr 3, 2021

This is Part 1 of a series of articles that will walk you through the basics of Flask Web Development by developing and hosting a simple CRUD application.

This series is split into 6 parts:

  1. A simple Hello World app
  2. Rendering HTML templates.
  3. Building a scaleable file structure.
  4. Configuring a database.
  5. Handling user login and registration.
  6. Adding CRUD (Create, Read, Update, Delete) functionality.

This series assumes you:

  1. Have a development IDE you prefer.

2. Understand how to download and import Python packages.

3. Understand basic HTML.

4. Understand basic Python.

This series has an accompanying Git

The master branch is here: https://github.com/shawnhymers/FlaskApp/tree/master

There is a branch for each stage of this series. Each branch has the code you will have written by the end of that part. The master branch has the code for the finished product.

Part 1: Hello World App

For this part we will start with just one folder and one .py file. This will grow more complex as our app does.

First, we will install the flask package in our environment. You can do this by running the following command in your terminal.

pip install flask

If you don’t have pip installed check out the documentation here: https://pip.pypa.io/en/stable/installing/.

Then in the app.py file add the following code.

from flask import Flask
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__': app.run(host="localhost", port=8000)

The code above is all you need to get started. Run this file in your favourite IDE then open your browser to http://localhost:8000/ and you should see the following message.

page shown in browser

So what is actually happening in our app.py file?

  1. First we are importing the Flask class.
from flask import Flask

2. Next we create an instance of the Flask class. The __name__ argument is thename of the application’s package name. If you are interested in why we use __name__ check out the documentation here: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask

app = Flask(__name__)

3. Next we use the route decorations to indicate which url will use the function. We use ‘/’ and ‘/index’ so the hello world function runs when you go to http://localhost:8000/ or http://localhost:8000/index

@app.route('/')
@app.route('/index')

4. Next we create the function that we want to run when the route is in use. For now we will just use a simple hello world function that just returns a string.

def hello_world():
return 'Hello, World!'

5. Lastly, we run our app.

if __name__ == '__main__': app.run(host="localhost", port=8000)

The source code for this part can be found at my GitHub here:

In part 2 of this series I will walk you through how to render an HTML template instead of our simple “Hello, world!”.

--

--