How to Deploy a Flask App with a sqlite database on Heroku

How to Deploy a Flask App with a SQLite database on Heroku

Posted by

Built a App using the Flask framework? Great! Now show it to the world 🌎

Alternatively, you can use Amazon Web Service’s Lightsail

Following this tutorial you can only upload a flask app with a sqlite database. If you want to upload a database with a postgresql database, check out this tutorial by Pretty Printed on Youtube.

What you’ll need

  1. A Github account
  2. Knowledge of basic git commands
  3. Knowledge of basic linux commands
  4. Git bash
  5. A flask App ready to be deployed.

Setup Heroku

  1. Go to Heroku and create a free account. Leave the Company name field blank.

2. Log in to your account.

Prepare your flask app to be deployed

  1. In git bash type the following. This creates a file called requirements.txt which contains all the dependencies for your project. It also acts as a buildpack for Heroku and tells it that you are using python for this project.
pip freeze > requirements.txt

2. Create a new folder and copy all the contents except the requirements.txt into it.

3. Rename the python file in which you have create the app to __init__.py

4. In __init__.py, define a function getApp which just returns the flask app

def getApp():
    return app

3. Upload it to github

Install the Heroku CLI

  1. Go to this page and install the heroku CLIfor your OS.
  2. For windows type the following in your command prompt
npm install -g heroku

3. After you’ve installed the Heroku CLI . Type the following

heroku --version

If you get the following output, you’ve successfully installed Heroku.

Create Heroku app

  1. Log in to your Heroku account and click new.
  2. Give a unique name to your app and click the Create App button. This name will be a part of the name of website so chose a descriptive name.
  3. Select your app in the Heroku Dashboard. Click on Deploy in the top navabr.
  4. Select ‘Connect to Github’ as your deployment method.

5. Search for your github repo and click connect.

6. Click on ‘Enable automatic deploys’.

This tells Heroku to re-deploy the app anytime a commit is made to the master branch. This saves us a lot of time since we, do not have manually deploy the app each time we make any change. Heroku takes care of this for us.

7. Click on deploy branch and wait for it to build and be deployed. Heroku will now install all the dependencies listed in requirements.txt.

8. Click on view app and go to your website.

If you see the above page, Good Job 👏.

Yes it shows an error but this means that you have successfully connected Heroku to your Github repo and Heroku has installed the required dependencies.

Create a server using gunicorn

  1. Type the following in command prompt
heroku login

2. If you type the following in git bash, you’ll see more information about the error.

heroku logs --tail -a yourAppName

No web processes running

We get this error since we have not yet setup our server. We will use

gunicorn to setup a web server.

3. Using git bash cd into your app and install pipenv

pip install pipenv

4. Next type the following

pipenv install gunicorn

5. Update your requirements.txt

pip freeze > requirements.txt

Make sure your requirements.txt file contains gunicorn, if not, add it manually at the end

gunicorn== 19.9.0

Create a wsgi.py file and Procfile

  1. Since we have named our python file as __init__.py , we can directly import the getApp from the folder inside our root directory.
from catalog import getApp

app = getApp()

2. Create a file named Procfile and paste the following

web: gunicorn wsgi:app

This tells Heroku that we are using gunicorn and our app is in the wsgi.py file.

Upload the changes to Github and wait for Heroku to build it automatically.

Congrats!! 👍 👍 You’ve successfully deployed your application