Skip to content

cogentii/flask-postgres-boilerplate

Repository files navigation

Flask PostgreSQL Boilerplate

Code Check

This is a boilerplate for a PostgreSQL Flask app that can run as a local poetry-based, Heroku or Docker application. Requires python 3.7 or higher.

Local Development

Install Poetry

  • Install Poetry if you don't have it using pip install poetry
    • On WSL install using the get-poetry route. For example, on Ubuntu, do curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -.
  • Install the packages: poetry install
  • To open a Quart shell, just do poetry run flask shell

First Migration

  • Run the first migration with poetry run flask db upgrade
    • Subsequent migrations after models changes can be run with poetry run flask db migrate -m "added app table field".

Running the application

  • To run the application do: poetry run flask run
  • Open http://localhost:5000 on your browser

Run Tests

  • Run tests by doing poetry run pytest

Using Docker

  • Make sure your folder is being shared within Docker client (Preferences > Resources > File Sharing)
  • Run docker-compose up --build. If there's a timeout error, you can restart the Quart container.
  • To do the first migration:
    • docker-compose run --rm web poetry run flask db upgrade
  • Restart using docker-compose and head over to http://localhost:5000 on your browser
  • Run tests by doing docker-compose run --rm web poetry run pytest -s
  • To connect to the Docker PostgreSQL shell, do docker exec -it app_db_1 psql postgres -U app_user

Production

  • Use Hypercorn hypercorn --bind 0.0.0.0:$PORT --reload wsgi:app

Codespaces

  • Start the Codespace
  • First time:
    • Run poetry install
    • Make sure to select the poetry Python interpreter for VSCode. If you don't see it, hit refresh on the dialog.
    • Do the first migration: poetry run flask db upgrade
  • To run the application: poetry run flask run
    • The codespace will give you a private URL for your application
  • To connect to Postgres Database: psql -h localhost -Uapp_user postgres

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published