Skip to content

Instantly share code, notes, and snippets.

@OctoberWu
Last active October 5, 2020 21:37
Show Gist options
  • Save OctoberWu/7f226ed468fea283e892d13b862c12a0 to your computer and use it in GitHub Desktop.
Save OctoberWu/7f226ed468fea283e892d13b862c12a0 to your computer and use it in GitHub Desktop.
[Deploy Flask on Heroku] #flask #heroku

prerequisetes - Flask, gunicorn, Heroku account and Heroku CLI are needed

$pip install flask gunicorn

step 1: Create a simple Flask App

// ./app.py

app = Flask(__name__)

@app.route('/')
def index():
	return 'Yo, it's working!'
    
if __name__ == "__main__":
	app.run()

step 2: Wrap-up Python Dependencies

put those dependencies in a file named requirements.txt

$pip freeze > requirements.txt

step 3: Create Procfile

create a Procfile and append the following text format: <process_type>:

  • activate web application
  • gunicor is a tool for developing WSGI in Python
web: gunicorn app:app
$python app.py

On Heroku side

suppose we create an app called flask-deploy-blog

  • login
$heroku login
  • create a new Git repository
$cd my-project/
$git init
$heroku git:remote -a flask-deploy-blog
  • create a new Heroku App
#if <app-name> is empty, Heroku will generate a random one.
$heroku create <app-name>
  • assign the Heroku App to the project
# -a option is followed by <app-name>
$heroku git:remote -a <app-name>
  • deploy the application
$git add .
$git commit -am "make it better"
$git push heroku master

Web-Server initialization

For the 1st time, configuration is needed for Heroku(we need a machine to run server)

heroku ps:scale web=1

Check the result

by web browser https://<Heroku app name>.herokuapp.com or by terminal

$heroku open

Miscelleous

.gitignore: for those we don't expect to push those irrevelent file on web

# my-project/.gitigore
my-project 
*.pyc
__pycache__
staticfiles
db.sqlite3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment