building flask with virtualenv

  • install virtualenv
1
pip install virtualenv
  • setup a project with virtualenv
1
virtualenv helloflask
  • activate your project
1
2
cd helloflask
source bin/activate ('deactivate' is another command)
  • install flask
1
2
3
pip install flask
#This installs Flask, Werkzeug (a utility library)
and Jinja (a templating framework).
  • create app.py
1
2
3
4
5
6
7
8
9
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
  • run your app
1
python app.py

deploy to heroku

  • install gunicorn
1
2
# Heroku seems to prefer a web server called gunicorn
pip install gunicorn
  • add a requirements.txt
1
pip freeze -l > requirements.txt
  • add a runtime.txt to specify your python version
1
2
3
# get your python version: python --version
vim runtime.txt
python-3.6.2
  • create a Procfile (app:app the first app should your python file name)
1
2
vim Procfile
web: gunicorn app:app
  • git & heroku
1
2
3
4
5
6
7
8
9
10
11
12
git init
git add .
git commit -m "first draft"
# you should know some knowledge about heroku login before
heroku create # will create a ***.git
#if you do heroku create before git init, you should use below command
git remote add heroku https://git.heroku.com/gentle-dawn-27343.git
git push heroku master
heroku open