Get the sample Bottle app

Bottle basics

To keep things simple, the sample app simulates an API request that gets the name and role of a user.

Navigate to the bottle_heroku_tutorial folder in a file browser. The sample_app.py file is the app's nerve center. Open it in a text editor to take a look. Here's a link to the file in this repository:

Check your email to confirm your account. When prompted, set up a password.

After setting up a password, you'll be taken to a welcome page.

Install the Heroku Command Line Interface. To download and install it, see Heroku CLI in the Heroku docs.

As the doc suggests, you should login at the command line immediately after installing the tool.

Create a remote Git repo for your app on Heroku

After you push your app to the repo, Heroku will serve the app from there.

If not already done, login to Heroku in your command-line interface:

$ heroku login

Run the following command:

$ heroku create your-app-name

Change "your-app-name" to whatever you want to name your app. You can expect that common names like “my-app” or “tutorial” will already be taken. You can omit the name and let Heroku generate a random name that you can change it later. Example:

This is a one-time-only requirement. Heroku creates a remote git repository that you'll link to a local Git repo in the next section. This setup will let you use a simple git push command to deploy your app to the server.

Create a local Git repo

Before you start, make sure you installed Git. See Install Git above for instructions.

In your command-line interface, navigate to the folder that contains the files of your Bottle app.

If you copied the sample app, it should be the folder that contains the sample_app.py file.

Run the following 3 commands one after the other:

$ git init
$ git add .
$ git commit -m "my first commit"

This creates a local Git repository and adds you files to it. You'll connect this local repo to a remote repository on Heroku in the next step.

Make sure you're logged in to Heroku ($ heroku login), then run the following command to set your Heroku app repo as the remote repo of your local repo:

$ heroku git:remote -a your-app-name

Important: Change "your-app-name" to whatever is the name of your app. Example:

$ heroku git:remote -a fast-sierra-15737

To verify you set the remote repo, run:

$ git remote -v

Prepare the app files for deployment

When deploying, the following configuration files need to be included in the web app's root directory:

Procfile

runtime.txt

requirements.txt

In a text editor, create a file named Procfile and make sure it contains the following line:

web: python sample_app.py

A Procfile lists the app's process types and the commands to start each process. The Bottle app runs a single web process, which is started by executing the python sample_app.py command.

Create a file named runtime.txt and make sure it contains the following line, adjusted for your version number:

python-3.5.2

The runtime.txt file tells Heroku what Python version to use for your app. The setting ensures Heroku runs your app in the same runtime environment you used locally to develop and test the app.

To find out the version you're using locally, run the following command at the command line:

$ python3 --version

Create a file named requirements.txt and make sure it lists the following libraries:

bottle==0.12.13
requests==2.12.4

The file lists all the external libraries the app needs to run. Update the version number of each library, if necessary. To find out the version, run the following commands:

$ pip3 show bottle

or

$ pip3 show requests

Add the new config files to your local repo:

$ git add .
$ git commit -a -m "Add config files"

Push the app to Heroku

In this step, you deploy the app to Heroku for the first time.

If not already done, login to Heroku and enter your Heroku email and password when prompted:

$ heroku login

Deploy the app:

$ git push heroku master

The command uploads the app files to the remote git repository on Heroku. Heroku then builds and deploys the app.

Set the following APP_LOCATION environment variable in Heroku:

$ heroku config:set APP_LOCATION=heroku

This is a one-time-only requirement. The code in sample_app.py has two different run statements: one for the local server and one for the Heroku server. At runtime, the app checks for the APP_LOCATION environment variable to decide which one to run:

If something goes wrong, go back to your command-line interface and enter heroku logs --tail. Check the entries for clues of what went wrong.

Bookmark the URL of your app.

Push updates to Heroku

You can start tweaking or adding to the app. See the Bottle tutorial. Test your changes locally before pushing the changes to Heroku, as described next.

Note: If you make changes to the sample_app.py file while the local server is running, you have to stop and restart the server to see the changes. You don't have to restart the server if you make changes to static files like the templates or css. Just refresh the page in the browser.

To push updates to Heroku

In your command-line interface, navigate to your app folder.

Commit all the changes in Git:

$ git commit -a -m "Various updates"

If not already done, log in to Heroku and enter your Heroku email and password when prompted: