Skip to content

brendon-wong/common-flask

Repository files navigation

Common-Flask

Common-Flask is a Flask boilerplate app that includes common structuring, practices, and Flask extensions. It uses a streamlined amount of code designed to make it easy to get started with building a web application in Flask. Flask-Login is used to handle user registration, login, and permissions. Flask-Admin is used for the administrative dashboard, and Flask-Migrate is used for handling database migrations. Additional functionality can be easily added and existing extensions and functionality can be easily removed. The purpose of all files is included in a docstring at the top of the file and explanatory comments are included in the code.

This app can be deployed on Heroku immediately and includes the Heroku-related files Procfile and runtime.txt. These files can be removed if Heroku deployment is not needed.

Inside the config.py files and the setup.py file, the app is referred to as "Project." A case sensitive search and "replace all" operation in the project's root directory is all that is needed to customize the name of the app. It is easiest to keep the Flask app directory name as "project," which is a common naming practice, but a case sensitive search for "project" will allow the directory to be renamed and for imports in other files to function correctly.

To make the app fully functional, the app's two config.py files must be adjusted. Config.py in the root directory is used for development, and the config.py file in the instance folder is used for production. The main settings that need adjustment pertain to Flask-Mail—an Gmail, Yahoo, Zoho Mail, or other email account must be linked, otherwise emails for account confirmation and password resets will not be sent.

The following instructions are for MacOS. See the Installation part of the Flask documentation to run the app on other operating systems.

To set up the application:

  1. git clone this repository
  2. python3 -m venv venv to create a new virtual environment
  3. . venv/bin/activate to activate the virtual environment (deactivate the virtual environment with deactivate)
  4. pip3 install -r requirements.txt to install/update all requirements
  5. pip3 install --editable . in the root directory to install the application package

To run the application:

  1. . venv/bin/activate to activate the virtual environment
  2. python3 run.py to run the app
  3. View the app in a web browser at http://localhost:5000

Local database setup

  1. The application is configured to create a sqlite database called dev.db in the top-level folder when it is run locally
  2. flask db init to create the migration repository
  3. flask db migrate to have Alembic compare the db schema with db file
  4. flask db upgrade to apply changes to the db file

Heroku Deployment

  1. On Heroku, set the environment variable ENV to 'prod' which loads config.py from the instance folder instead of the root directory
  2. heroku addons:add heroku-postgresql:hobby-dev to create a Postgres database
  3. Set SQLALCHEMY_DATABASE_URI to Heroku's DATABASE_URL environment variable in the config.py file in Flask's instance folder
  4. Use Flask Migrate's flask db init and flask db migrate commands to generate migrations
  5. Push the migrations folder to Heroku
  6. heroku run python3 manage.py db upgrade --app app-name to upgrade the production database

Running Postgres locally

  1. brew install postgresql to install Postgres with Homebrew
  2. brew services start postgresql to start
  3. brew services stop postgresql to stop

Locally Create Heroku Migrations

Source: https://stackoverflow.com/questions/21529118/running-flask-migrate-on-heroku-produces-error

  1. Use the command heroku pg:pull DATABASE_URL new_db_name -a heroku_app_name which creates a new local Postgres database with the name new_db_name and the same database schema and content of your Postgres database on Heroku. A database with the same name must not already exist. You can use a tool like Postico to view and manage your local Postgres databases with a GUI.
  2. Configure your Flask app to use the new Postgres database. Assuming Flask-SQLAlchemy is being used, in Flask's configuration set SQLALCHEMY_DATABASE_URI = "postgresql://localhost/new_db_name".
  3. Now that Flask recognizes the new local Postgres database mirroring the production database, use Flask-Migrate's flask db init and flask db migrate commands to generate a migration script.
  4. Push the migrations folder generated by Flask-Migrate to Heroku.
  5. Use Flask-Migrate to upgrade the production database on Heroku with heroku run flask db upgrade -a heroku_app_name.

Brendon's development notes

Inspiration

About

A Flask boilerplate app with streamlined code to quickstart development and demonstrate common practices

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published