How This Tutorial Is Organized
The tutorial is divided into four articles:
- Introduction – Dive into web development with Flask. This article explains the basics of web development with Flask
- Part I – Setting up a Local Development Environment
- Step 1: Create virtual environment for all required dependencies
- Step 2: required packages: Flask, psycopg2, SQLAlchemy
- Step 3: Create requirements.txt file with all dependencies listed
- Step 4: Install PostgreSQL 9.3 and create the local database
- Step 5: Organize the structure of the application
- Step 6: Make Flask say “Hello”
- Part II – Application Development
- 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
- Sign up for a Heroku account
- Download a Heroku toolbelt to get the “Heroku client”
Login to your account (Heroku will ask for you email address and your account password)
$ heroku login
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
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.
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_URLenvironment 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
- Create a requirements.txt file with all dependencies.
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=-
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()
Now, the application can be accessed at http://flask-todoapp.herokuapp.com/.
|« Previous Part|