DEV Community

Cover image for Create a Flask App in 5 Minutes

Posted on

Create a Flask App in 5 Minutes

    When learning to build an API with a language like Python, Flask is a solid option. Starting out is simple and implimenting database queries and insertions are easier than you'd think thanks to ORMs (Object-Relation Mapper). Flask and other unopinionated frameworks in general shine when it comes to simplicity but can sometimes make your projects lack structure and direction compared to opinionated alternatives like Django.

Before we can start this tutorial you should check if you have the required tools

Python & Pip

    Python comes pre-installed on most Linux distros, and always pre-installed on macOS. You will have to install using the Python website for Windows if you don't have it installed already. To check if Python is on your system you can use python --version or python3 --version. Pip (Pip Installs Packages), is Python's package manager and comes with Python.

Step 1: Prepping Your Setup

First open a new window with the terminal/command line application that comes with your operating system.

Then Create a new directory for your project.

mkdir flaskapp
Enter fullscreen mode Exit fullscreen mode

From here you have to move into this new directory

cd flaskapp
Enter fullscreen mode Exit fullscreen mode

Then you create a new virtual environment in this directory using Python's builtin virtual environment tool

macOS/Linux Windows
python3 -m venv venv py -3 -m venv venv

After the environment is created we activate it

macOS/Linux Windows
. venv/bin/activate venv\Scripts\activate

Lastly comes installing Flask. Flask is the framework we'll be using today. It's a lightweight, unopinionated framework and really easy to use.

Install it using pip install Flask or  pip3 install Flask depending on your Python version.

Step 2: Creating The Flask App

Create a new filed called and type in the following:

from flask import Flask

app = Flask(__name__)

def HelloWorld():
    return "Hello World!"

if __name__ == "__main__":
Enter fullscreen mode Exit fullscreen mode

The first line in the file imports the Flask object from the library. The next line initializes our Flask object as a variable we can use, add routes to and run. @app.route('/') names the route and the function HelloWorld is the function routed to the string in the decorator. It returns a string that will show when the URL is visited. The last two lines are sort of a safe guard in Python scripts to prevent unwanted execution, and will only do when the file itself is being run rather than imported.

Step 3: Running Your Flask App

Open a window of your terminal/command line application and run

Enter fullscreen mode Exit fullscreen mode

Your terminal should show this:

 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 135-059-029
Enter fullscreen mode Exit fullscreen mode

And if you visit http://localhost:5000 or you should see this:

Successful run

    Python comes pre-installed on most Linux distros, and always pre-installed on macOS. You Congratulations! You just built a flask app. With an understanding of REST APIs you could add new routes that return different information, or even files. Using ORMs you could add functionality to create, retrieve, update or delete from a local or cloud database. With a framework like Flask, the limit is up to you.



Top comments (0)