I recently went throught the process to put my side project howbigisthebaby.io on Digital Ocean's new app platform, and wanted to document it in case anyone else found it helpful (or atleast me revisiting this later).


Part 1: Setup the environment for the Flask app

Step 1: Create the virtual environment on your machine

$ virtualenv venv -p Python3

Step 2: Activate the virtual environment

$ source venv/bin/activate

Step 3: Install Flask and gunicorn

$ pip install Flask gunicorn

Step 4: Freeze the requirements in requirements.txt file

Now that you have the flask package installed, you will need to save this requirement and its dependencies so App Platform can install them later. Do this now using pip and then saving the information to a requirements.txt file:

$ pip freeze > requirements.txt

Step 5: Write code for your Flask app in app.py. Here's a simple "hello world" app

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

Part 2: Setting Up Your Gunicorn Configuration

Since we cannot use Flask's built-in server in production environment, we will use Gunicron web server Why Nginx/Gunicorn/Flask?

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.
Source: Flask's docs

Step 1: Create the gunicorn config file

$ touch gunicorn_config.py

Step 2: Add this configuration to gunicorn_config.py file

# inside gunicorn_config.py
bind = "0.0.0.0:8080"
workers = 2

Part 3: Pushing the Site to GitHub

The way the Digital Ocean app platform works is that it syncs with a GitHub repo. Any changes to this GitHub repo will automatically be pushed to your app living on Digital Ocean's app platform.

Step 1: Create a new repo on GitHub.com
Open your browser and navigate to GitHub, log in with your profile, and create a new repository called flask-app.

Step 2: Then, create a .gitignore file, and add .pyc so these files are not pushed to GitHub.

$ git init
$ nano .gitignore
# inside .gitignore
.pyc

Step 3: Add files to your repository, and commit the changes

$ git add app.py gunicorn_config.py requirements.txt .gitignore
$ git commit -m "Initial Flask App"

Step 4: Add the GitHub repo you created earlier as the remote repository

$ git remote add origin https://github.com/your_username/flask-app

example: git remote add origin https://github.com/ajot/hbb-flask-webhook

Step 5: Push to GitHub

$ git branch -M main
$ git push -u origin main

Part 4: Deploying to Digital Ocean's app platform

  1. Create a new "app" on Digital Ocean.
  2. Choose GitHub as source
  3. Choose the GitHub repo you just created
  4. Follow the promots to create the app
  5. Edit the run command to this - gunicorn --worker-tmp-dir /dev/shm app:app

Bonus Tip: You can also set environment variables in Digital Ocean, and then access them in your code like this -

import OS

airtable_api_key = os.environ.get('airtable_api_key')
airtable_base = os.environ.get('airtable_base')

Resources:
How To Deploy a Flask App Using Gunicorn to App Platform | DigitalOcean

Tagged in: