Back to articles list
- 4 minutes read

Flask Web-App Development. Part III: Deployment to Heroku

How This Tutorial Is Organized

The tutorial is divided into four articles:

  1. Introduction – Dive into web development with Flask. This article explains the basics of web development with Flask
  2. Part I – Setting up a Local Development Environment
  3. Part II – Application Development
  4. Part III – Deployment to Heroku (You are here)

 

Recently, hosting applications in the “cloud” has become the most popular method of application because it offers great flexibility and is relatively simple to set up. This model earned a formal name known as Platform as a Service (PaaS). In a PaaS model the service provider offers a fully managed platform in which a web application is uploaded and deployed. Most PaaS providers offer to scale an application by adding or removing servers. For hosting our TODO app I’ll give Heroku a chance.

Heroku is one of the first PaaS providers. This platform supports a lot of programming languages. The computing units used by Heroku are called dynos. We will use web dyno. It represents a web server instance. By increasing the number of web dynos we increase the number of requests that our application can handle.

Briefly, deploying our app requires:

  • uploading the application using git
  • creating a file called Procfile with instructions on how to execute the application
  • file requirements.txt which lists all the module dependencies that need to be installed
  1. Sign up for a Heroku account
  2. Download a Heroku toolbelt to get the “Heroku client”
  3. Login to your account (Heroku will ask for you email address and your account password)

    $ heroku login

  4. To deploy to Heroku, the application must be in a local git repository.

    $ git clone https://github.com/pdybka-ep/flask-todoapp.git
    $ cd flask-todoapp
    

  5. Create a new Heroku app:

    $ heroku  apps:create flask-todoapp
    
    Creating flask-todoapp... done, stack is cedar-14
    https://flask-todoapp.herokuapp.com/ | https://git.heroku.com/flask-todoapp.git
    Git remote heroku added

    This command adds a git remote to our git repository.

  6. Create a Heroku database:

    $ heroku addons:create heroku-postgresql:dev –app flask-todoapp
    
    Creating looking-fairly-5910... done
    Adding looking-fairly-5910 to flask-todoapp... done
    Setting HEROKU_POSTGRESQL_ROSE_URL and restarting flask-todoapp... done, v4
    Database has been created and is available
     ! This database is empty. If upgrading, you can transfer
     ! data from another database with pgbackups:restore
    Use `heroku addons:docs heroku-postgresql` to view documentation.
    

    Heroku knows how to connect to this database by $DATABASE_URL environment variable. For now change in todoapp.py in app configuration:

    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
    

    Heroku expects the application to starts its own server. Unfortunately the Flask built in web server is not good for production use, that’s way we switch to gunicorn server

  7. Create a requirements.txt file with all dependencies.
  8. Create a Procfile (it contains information how to run the application on Heroku)

    For our purpose it will look like follows:

    web: gunicorn todoapp:app --log-file=-

  9. Commit changes and deploy the application to Heroku

    $ git push heroku master
    

    Now the application is deployed. If you type in address bar https://flask-todoapp.herokuapp.com/ you receive Internal server error. This happens because the tables aren’t created. So, in the last step we have to create tables in the Heroku database. With that comes another issue: our application needs to have three priorities, which we have to insert manually in shell or create a script with required commands.

    >>> import todoapp
    >>> import models
    
    >>> models.db.create_all() 
    >>> high = models.Priority(name=u’high’, value=3)
    >>> medium = models.Priority(name=u’medium’, value=2)
    >>> low = models.Priority(name=u’low’, value=1)
    >>> models.db.session.add(high)
    >>> models.db.session.add(medium)
    >>> models.db.session.add(low)
    >>> models.db.session.commit()
    

Done!

Now, the application can be accessed at http://flask-todoapp.herokuapp.com/.


« Previous Part  

go to top