Skip to content

Flask / Postgres Starter Template for DigitalOcean App Platform

Notifications You must be signed in to change notification settings

heyaustinwood/flask-do-app-platform

Repository files navigation

flask-do-app-platform

quickly setup and deploy a flask app with postgres to DigitalOcean app platform with GitHub integrations

Download this project and follow the steps below.

setup flask project

  1. download or fork the project
  2. open terminal and cd into project folder
  3. run following commands:
python3 -m venv venv
source venv/bin/activate
pip install flask
pip install -r requirements.txt
  1. rename .env.sample to .env

setup development database

  1. setup local database named doappplatform or you can call it whatever you want, just make sure you change the config.py file to match. user: postgres, pass: postgres.

test local app

  1. run command in terminal: python app.py
  2. add a faq, make sure local db connection works

push to github

  1. create repo with project files
  2. push to your github account

setup digital ocean app platform

  1. go to digitalocean.com, login, click Create button and select Apps
  2. select github as the provider and then find your repo and select it. select settings you prefer.
  3. click add a resource on the next page and then select database. name it, create it and attach it to the project
  4. setup environment variables on the next screen. you will need to setup the following env variables:
  • FLASK_ENV = Production
  • FLASK_DEBUG = False
  • SQLALCHEMY_DATABASE_URI = get this later
  • SECRET_KEY = generate_a_key
  • DATABASE_URL = this is autogenerated by digitalocean, just leave it

you can generate a secret key with the following command in the terminal: python3 -c 'import secrets; print(secrets.token_urlsafe(16))'

  1. publish and deploy project. let the project build and database provision. once it's done or fails to build, we need to make some additional modifications.
  2. to get the app to deploy successfully, the run command needs to be modified. by default, the run command is missing app:app, so it needs to be added. go to settings, select the app under components, scroll down to commands and press edit.

original command by default:

gunicorn --worker-tmp-dir /dev/shm

modified command, necessary to deploy:

gunicorn --worker-tmp-dir /dev/shm app:app

once you hit save, the build and deploy process should start over again.

  1. the app should build and deploy successfully, however when you open it in the browser, it will probably have an error screen. app is good but we need to go back and get database connect settings now.
  2. go back to settings and select the database under components just below the horizontal nav bar. Scroll down to connect details and click view. change the dropdown to connection string and copy the contents. scroll up and select the app under components and then edit the environment variable for SQLALCHEMY_DATABASE_URI. Paste the connection string into the value input. Press save and your app will begin rebuilding and deploying.

testing

if you've completed all the steps above correctly, fuck ya, your flask app with postgresql should be running on DigitalOcean App Platform. Anytime you push to /main branch, the app will rebuild and deploy automatically (unless you changed settings)

About

Flask / Postgres Starter Template for DigitalOcean App Platform

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published