Skip to content

Instantly share code, notes, and snippets.

@sandeepkv93
Created November 27, 2018 00:29
Show Gist options
  • Save sandeepkv93/fe0cfbfd3fe407dcc29c94b4c6b75dc8 to your computer and use it in GitHub Desktop.
Save sandeepkv93/fe0cfbfd3fe407dcc29c94b4c6b75dc8 to your computer and use it in GitHub Desktop.
Creating Flask App and deploying it in Heroku

Creating Flask App and deploying it in Heroku

Create a Flask App

  1. Create a virtual environment

    • In Windows
    virtualenv env
    cd env\Scripts
    .\activate
    • In Linux/Mac
    virtualenv env
    source env/bin/activate
  2. Install Flask Module

    pip install Flask --upgrade
  3. Create a .gitignore file and add the following lines

    __pycache__/
    *.py[cod]
    .vscode
    env/
    
  4. Sample Flask app which returns current Date

    from flask import Flask
    from datetime import datetime
    app = Flask(__name__)
    
    @app.route('/')
    def func():
        return str(datetime.now())
    
    if __name__ == '__main__':
        app.run()
  5. Run the app

    python app.py

    When you goto http://localhost:5000/ you should be able to see current date and time.

Deploying this app in Heroku

  1. Create a new file and name it as Procfile in the project's root folder and add the following text

    web: gunicorn app:app
    
  2. Add guicorn module and other installed modules to requirements.txt

    pip install gunicorn --upgrade
    pip freeze > requirements.txt
  3. Specify a python runtime. Example: python-3.6.2

    • Create a file called runtime.txt in the project's root folder and add the following text
    python-3.6.2
    
  4. Create a heroku app

    heroku create flaskdemoapp
  5. Initialize git and add remote as heroku

    git init
    git remote add prod git@heroku.com:flaskdemoapp.git
  6. Commit and push

    git add .
    git commit -m "Your message"
    git push -u prod master
  7. Incase if you had not added public key, then

    heroku login #Username & Password
    heroku keys:add ~/.ssh/id_rsa.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment