Giter Site home page Giter Site logo

viper0302 / flask-postgresql-template Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shane-kercheval/flask-postgresql-template

0.0 1.0 0.0 603 KB

Make it easy to set up a flask (w/ postgresql) project. Made to deploy on Heroku.

Python 56.44% Mako 1.34% CSS 13.42% JavaScript 1.27% HTML 27.53%

flask-postgresql-template's Introduction

Flask Template (Postgres & Heroku)

this app is currently deployed at: https://flask-postgresql-template.herokuapp.com/

Note

  • while this is designed to use Postgres and run on Heroku, I assume it can run on other platforms and use other database technologies with basic modifications.
  • development was done on OS X

Getting Started

  • download source, commit to whatever git repository you set up (assumes GitHub)
    • you may need to change
      • runtime.txt
      • Procfile.txt

postgres

  • install http://postgresapp.com (OS X)

  • run export command in terminal

      export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
    
  • start postgresapp to make sure it works and the server starts

  • open psql from app

  • in psql terminal, create the database you want to use in the site, config defaults the db url to example_site. USE:

      CREATE DATABASE example_site;
      \connect example_site;
    
  • the DATABASE_URL value in config.py is defaulted so that if none exists, it returns 'example_site'

  • So, when pushed to Heroku you must install the postgres add-on and set the config value (mentioned below); depending on the order of steps, it's possible that Heroku auto-detects postgres and installs it for you.

venv:

  • start venv using os x terminal

      source path-to-ven/bin/activate
    
  • update venv to requirements using

      pip install -r requirements.txt
    
  • if you get this error (""python setup.py egg_info" failed with error code 1"), then run this command in terminal

      export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
    
  • any time you update venv via pip, save to requirements using

      pip freeze > requirements.txt
    
  • if you get "Error: pg_config executable not found."

    • run this in terminal and retry: "export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin"

running, tests, and initial data

  • run tests locally using terminal command:

      python -m unittest tests/test_app.py
    

    or run all tests with:

      python -m unittest discover ./tests
    
  • add data to database using terminal command:

      python initialize_db.py
    
  • confirm data is in database using following command in psql terminal window

      select * from users;
    
  • run locally using terminal command:

      python app.py
    
  • in browser, go to http://127.0.0.1:5000/ ... you should see the site

  • commit/push to git repository

heroku

  • sign up for heroku account

  • download/install Heroku Toolbelt: https://devcenter.heroku.com/articles/getting-started-with-python#set-up

  • log in to Heroku via toolbelt/terminal:

      heroku login
    
  • create heroku app

    • terminal cd path-to-project

    • create app command:

      heroku create [app name] e.g. 'heroku create flask-postgresql-template'

  • Heroku has an excellent "Getting Started" Guide.

  • Heroku recognizes the app as a Python app by the existence of a requirements.txt file in the root directory. For your own apps, you can create one by running pip freeze.

  • you can use GitHub and Heroku at the same time; adding the 'remote' from the setup tutorial above doesn't overwrite git, it adds another

  • once you have hadded 'heroku' remote, deploy code:

      git push heroku master
    
  • ensure it worked:

      heroku open
    
  • this should have automatically added postgres add-in to your app (Heroku auto-detects the requirement), but verify on heroku dashboard

    • Heroku automatically adds DATABASE_URL to the config settings which makes it available to os.evnironment
  • add the following config config var to your Heroku app (either via command or heroku website)

      config name: APP_SETTINGS
      config value: config.ProductionConfig 
    
  • try running Heroku locally with

      heroku local
    
  • optionally connect heroku to github to enable automatic deployments

  • to run a different computer, follow same steps, then enable the deployment using

      heroku git:remote -a [heroku app name]
      e.g. "heroku git:remote -a flask-postgresql-template"
    
  • then do:

      git push heroku master
      heroku open
    

Migrations

  • as your database changes, you need to migrate the changes to other (e.g. production) databases

  • view remote heroku database using:

      heroku pg:psql --app flask-postgresql-template DATABASE
    

-may have to run "export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin"

  • Alembic automatically creates/tracks database migration records from the changes in the SQLAchemy models, and allows us to upgrade/downgrade to specific versions.

  • Flask-Migrate (official documentation) is an extension created for SQLAlchemy that works with Flask Script / Alembic; MigrateCommand added to manage.py

  • to see the migration command options, use:

      python manage.py db
    
  • to start tracking database changes, use:

      python manage.py db init
    
  • the previous command creates /migrations/ folder

  • create the initial migration: (causes Alembic to scan SQLAlchemy all table/column changes)

      python manage.py db migrate -m "initial migration"
    
  • make sure to do this with an empty local database, if there are existing tables, they won't be added to the migration

  • to apply migration to another database, run

      python manage.py db upgrade
    
  • view migration history:

      python manage.py db history
    
  • when switching computers, blow away the database (drop all tables) and do 'upgrade' and then 'migrate'

  • push db provisions to heroku, using:

      heroku run python manage.py db upgrade --app [APP]
      e.g. heroku run python manage.py db upgrade --app flask-postgresql-template
    

-MAKE SURE ALL MIGRATIONS/CODE-CHANGES ARE PUSHED TO REMOTE BEFORE RUNNING, THIS RUNS OFF OF REMOTE SOURCES

Authentication

Advanced Logging

  • install 'papertrail' add-on
  • open the heroku dashboard - "heroku addons:open papertrail"

MISC Notes

  • in models.py, we need to store passwords as LargeBinary. Seems unique to Postgres.

flask-postgresql-template's People

Contributors

shane-kercheval avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.