Follow this steps and check the code to build and deploy a simple feedback app that an be used as a template for future projects.
-
Install virtual environment manager:
pip install pipenv
-
Create environment:
pipenv shell
(Close env:exit
) -
Install Flask within VE:
pipenv install flask
(It will create a lock file for dependencies) -
Install psycopg2 (PostgreSQL adapter):
pipenv install psycopg2
(Install psycopg2-binary if some issue appears) -
Install sqlalchemy (SQL Toolkit and ORM):
pipenv install flask-sqlalchemy
-
Instal gunicorn (Unix server):
pipenv install gunicorn
-
Make sure you have correct interpreter: Ctrl/Cmnd + Shift + p -> "Python: Select interpreter" -> python...(my_app)
-
Download and install postgreSQL from web page to be able to start pgAdmin4 locally
-
Create database using left panel
-
Create table based in model using REPL. Type
python
and run the following (assuming code is ready). After run app again, the table should be visible in pgAdmin4 Schemes panel:from app import db
db.create_all()
exit()
- Create an account to get your credentials and prepare send email service
-
Create Heroku account and download-install Heroku CLI. Also, Git version control is needed
-
Run
heroku login
to connect to your account -
Create Heroku app with
heroku create [app name]
-
Create a Heroku database using Heroku addons for PostgreSQL:
heroku addons:create heroku-postgresql:hobby-dev --app [app name]
-
Get new database URL (then, set your SQLALCHEMY_DATABASE_URI for production):
heroku config --app [app name]
-
Create requirements file with dependencies:
pip freeze > requirements.txt
-
Create Procfile, so Heroku, as a Paas, will know how to run the app:
touch Procfile
(Unix). Here, we set gunicorn as our server -
Create runtime file to specify Python version
-
Push everithing to remote git repository
-
Add remote repository to Heroku:
heroku git:remote -a [app name]
-
Push project to Heroku:
git push heroku master
-
See your creation with
heroku open
. However, it doesn't work, because we aren't created the table yet -
Create table based in model using REPL, but this time with Heroku. Type
heroku run python
and run the following:from app import db
db.create_all()
exit()
-
After that, you can see the table with
heroku pg:psql --app [app name]
(after install PostgreSQL in Windows, you'll need to add to PATH, otherwise, this command will not work). Useselect * from feedback;
to see all rows