Better Programming

Advice for programmers.

Follow publication

Member-only story

Build and Deploy Your Flask API With a Postgres Database

Ardy Gallego Dedase
Better Programming
Published in
5 min readMay 31, 2020

--

Photo by Neven Krcmarek on Unsplash

We often find ourselves rewriting the same boilerplate code — like configuration, structure, and the build and deployment setup — when writing a Flask web service. The goal of this article is to help you minimise doing eating the same thing over and over in your next Flask API project. You will also learn how to deploy your Flask web service with a database live on Render.com. If you don’t use Render, no problem — this cookiecutter also uses Docker to help facilitate build and deployment on other platforms.

What’s Included in the Template

  • Go live with a continuous deployment pipeline using GitHub and Render’s Infrastructure as Code. See sample IaC file here.
  • Works with SQLAlchemy/Postgres DB out of the box.
  • Uses mypy for static type checks.

High-level structure

Below is the structure of our Flask API. We will go through each file or component in the next section.

High-level structure of the cookiecutter template.

Breakdown of the structure

Let’s dissect and learn about the structure of our cookiecutter template.

main.py — where everything starts

This is the entry point of the API request.

  • It contains the API endpoint and CLI declarations. It uses Flask-Restful for its APIs.
  • Creates the Flask app instance.
  • Uses Flask-Migrate to run SQLAlchemy migrations.
  • Imports the API resources.

We might want to move the CLI functions when the file gets bigger.

--

--

Ardy Gallego Dedase
Ardy Gallego Dedase

Written by Ardy Gallego Dedase

Software Engineer working remotely 🇨🇦 🇺🇸

No responses yet

Write a response