DEV Community

Cover image for Building a Minimal Flask Application from scratch
Hans(Akhil) Maulloo
Hans(Akhil) Maulloo

Posted on • Updated on

Building a Minimal Flask Application from scratch

Hello world!
In this article, I will be demonstrating how Flask is used in web development by building the most basic application you can ever build; which is displaying the message 'Hello World!' on your browser.

Content

  1. Introduction to Flask
  2. Prerequisites
  3. Install Flask
  4. Build flask application
  5. Run the app!

1. Introduction to Flask

Flask, known as Python's micro web framework, has gained a lot of popularity over the past recent years. It is used for development of web application and, personally, I believe it provides all the flexibility needed to build a powerful full stack web application.

While Flask is mostly used for the backend, it deals with frontend with a templating language called Jinja2 with which you can write HTML pages from scratch and render on your browser.

2. Prerequisites

  • Python3
  • Pip3

As some of you may know, Python has come to an end of life on December 2019, and Python3 is now taking over. All os are coming with Python3 installed by default. And since we are using Python3, we will obviously need to install pip3. It may come by default on certain OS.

3. Install Flask

First things first, you have to make sure that you have Flask installed on your machine. We will use pip, a package manager that allows you to install and uninstall python modules easily, to install Flask on our environment.

$ pip3 install Flask

To ensure Flask has been installed correctly, you can run the following command, which will display the version of Python and Flask you currently have on your machine.

$ flask --version

If you can see the versions correctly, that means Flask is installed. You are good to go! Now that we have setup our environment, let's get to coding!

4. Build flask application

You can create an empty directory and call it whatever you want to name your application. Inside the directory create an app.py. This python script will contain you minimal Flask application.

# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return '<h1>Helllo World!</h1>'

if __name__ == '__main__':
  app.run()

This is everything you need to run a minimal Flask application on your machine! Pretty cool right?

Let's dive into app.py and examine each line to see what it does.

  • First we imported the Flask class.

  • Using the Flask class we imported, we create an instance and pass the name of our application as the first argument. Here we are passing __name__.

    • __name__ is a special built-in character in Python.
  • We then use something called the route decorator to specify the route we want to create in our application.

  • After the route '/' is hit, it should execute a method. This method is defined right below the route decorator

    • This where you specify the actions you want the app to do when the user call the predefined route('/').
  • Last but not least, we call our app, which we defined on line 2, with app.run().

5. Run the app!

Now we have everything setup, we only need to run the script to get our message on the browser. Open up a terminal, change directory to your project's root directory and simply run the command:

$ python3 app.py

Open up localhost:5000 on your browser and your message should be there waiting for you! :)

Alt Text

If you have enjoyed it so far, feel free to checkout buildflaskapp, which is a command-line tool to automatically generate a minimal Flask application by running one command.


Congrats if you made it up to here! You now have a minimal Flask application which you can modify and add in your own content. Some of the next articles regarding Flask will include:

  • Rendering html page with Flask
  • Building a CRUD application with Flask
  • Create a REST api with Flask
  • Containerizing our flask application
  • Deploying flask apps on heroku

Cheers!

Top comments (2)

Collapse
 
nullck profile image
Thiago Dias de Francisco

hey man I think you just missed to invoke the route func from the app instance

@app.route('/')

Collapse
 
kouul profile image
Hans(Akhil) Maulloo

Hi. Thanks for letting me know. I just fixed it