How To Write Your First Flask App

This Tutorial Will Teach You How To Build A REST API Using Python And Flask Framework

Bright
Geek Culture
4 min readApr 20, 2022

--

Photo by James Harrison on Unsplash

In this blog, we’ll be looking at what HTTP verbs are, and with the knowledge acquired, build a basic Flask REST API from the ground up. It’s very beginner-friendly and assumes you do not know Flask.

Programming constructs like loops and conditional statements aren’t difficult to grasp so you can follow along even if you’re new to coding. Having some basic understanding of programming would make you appreciate it more though.

Before we begin though, let’s understand what HTTP verbs are. They’re request methods that define communication standards between a server and a client. They determine a server’s response based on a request. The basic ones covered here are GET, POST, PUT, and DELETE.

Now that we have that out of our way, let’s learn some Flask.

Why Flask?

Flask is a lightweight micro-framework for developing web apps in Python. The micro term here doesn’t make Flask any less functional, but rather gives one the basic building blocks to scaffold a web app in a matter of minutes.

Flask abstracts away some of the complexities of creating web apps in native Python to make code quicker to write and easier to maintain. Unlike Django, it’s not opinionated, and you can cherry-pick whichever library you deem fit for your project.

It’s grown so much in popularity because of its easier to set up, lightweight components. Many large companies like Reddit, Pinterest, and LinkedIn use it. Let’s install Python and get to work.

Installing Python

To install Python, visit the official website and download the latest stable version for your Operating System. If you’re on a Unix-like environment, you probably have it installed already. Confirm you have python properly installed by entering python — version in your terminal.

We’re going to create a very basic web app but first, let’s set up our virtual environment.

Creating Our Virtual Environment

Virtual Environment creates isolated environments to run specific python applications. If you don’t have Virtual Environment installed already, check my earlier blog on setting it up. We’ll be using python3 on Ubuntu for this tutorial but the commands are the same for Mac and other Unix-like Operating Systems. There’re slight variations in a Windows environment, and I’ll cover them too.

Let’s create our project directory and set up our virtual environment.

What the above code does is create a directory called bookstore and cd into the directory. It then sets up our virtual environment and lists its contents. We only have the env directory for now.

Activating Our Virtual Environment

If you’re on a Unix-like environment, enter the following command:

Windows users can enter the following:

Building The App

We’re all set to start building our app. Let’s install flask:

Let’s create a file at the root of our project and start writing some code:

Note: I borrowed the code in this blog from Jose Salvatierra Fuentes’ REST API with Flask and Python course.

We can now open the app.py file and start building our app. The file name can be any of your choosing but app.py and server.py are the conventions. At the very top of the file, we have to make a few imports:

We have to create an app from the imported Flask class; we do this underneath the imported packages:

The dunder name (name sandwiched by two underscores) gives each file a unique name so when we start the flask app, it tells Flask its unique location.

Because this is a basic app, we’ll create our store list with some basic data. A real-world app would use some form of a database for data persistence.

Let’s create our first route:

This is a basic home route that renders Hello, World! on the browser.

Let’s create our first GET method:

The above code is a GET request to our store endpoint that retrieves all our store items; the app.route is a decorator that routes all our requests. jsonify serializes data to JSON and wraps it in a flask response. This way, it is properly parsed as a JSON to a calling client.

Note: The very first line is a comment that describes what endpoint we’ll be hitting to make our request.

Let’s create our next GET method:

This is a GET request that retrieves a single store from our stores collection. Take note of how we construct the URL. Here, we’re passing the name of the store in our URL. Once that stores/ endpoint is hit, the suffix of the / in our URL is extracted and compared to our stores collection to find a match. If there’s a match, it’s returned. If there’s none, it returns a custom JSON error response.

Now, let’s create our very first POST method.

Do you see we have another argument in our app.route decorator? This is an HTTP POST verb. You may be wondering why we didn’t have one in our first GET methods. Well, that’s because GET is the default method so when omitted, it’s assumed you’re making a GET request. stores is a list so we use the append method to add a new store to it.

Let’s add an item to a store:

Let’s retrieve items from a store:

Last but not least, let’s add the run method that would start our server:

See the full code below:

Conclusion

We’ve learned about what HTTP verbs are, and used them to build a basic Flask REST API from scratch. With this foundation, you can go ahead and build amazing stuff.

Happy coding!

--

--