Skip to main content

Deploying a Flask application

Introduction

Flask is a popular and lightweight web framework for Python. It's highly suitable for developers who want to make a standalone app. In this article, we'll cover how to deploy a Flask application for the world to see.

Prerequisites

Before we begin, make sure you have the following:

  • Basic knowledge of Python.
  • A complete Flask app that you want to deploy.
  • Python and Flask installed on your local machine.
  • An account on a deployment platform (we will use Heroku for this tutorial)

Deploying a Flask Application

Step 1: Install Gunicorn

Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX. It's a pre-fork worker model. This server will interface with our application, allowing it to speak 'web'.

pip install gunicorn

Step 2: Install Heroku CLI

The Heroku Command Line Interface (CLI) is a tool that wraps the Heroku Platform API, providing support for things like creating/renaming apps, running one-off dynos, taking backups, and configuring add-ons.

brew install heroku/brew/heroku

Step 3: Login to Heroku

Log in to your Heroku account.

heroku login

Step 4: Create a Heroku App

Creating an app on Heroku will prepare Heroku to receive your source code.

heroku create your-app-name

Step 5: Create a Procfile

Heroku apps include a Procfile that specifies the commands that are executed by the app on startup.

echo "web: gunicorn app:app" > Procfile

Step 6: Initialize a Git Repository

Initialize a git repository in a new or existing directory.

git init

Step 7: Commit Your Changes

Heroku uses Git as a means of deploying your app. So you need to commit your changes before you can deploy.

git add .
git commit -m "Initial commit"

Step 8: Deploy Your App

Now you can deploy your app to Heroku.

git push heroku master

Step 9: Open Your App

Finally, you can now access your app in the browser.

heroku open

Conclusion

Congratulations, you've just deployed your first Flask application!

In this tutorial, we've covered how to deploy a Flask application on Heroku. This is just the beginning of what you can do with Flask and Heroku.

Remember, learning is a journey that never ends. So keep exploring, keep learning and keep implementing. If you run into any issues, don't hesitate to ask for help. Happy coding!