As of late I have gained a growing interest into machine learning, most notably Tensor Flow. That is out of the scope of this blog post, but all of the machine learning libraries are built in Python. So I dove into Python and made an API to do some text processing using Flask and FlaskRESTful.
Flask is a “micro” framework for Python. It is called a micro framework because they want to keep the core simple but expandable. For example: Flask does not have built in form validation or a database abstraction layer, but there are numerous plugins available to achieve those.
Once I was satisfied with the functionality of my API I wanted to put it on Heroku so I could have access to it away from my local computer. This turned out to be a little trickier than I expected, so hopefully this tutorial will save you some time. :)
Using pip, Python’s recommended tool for installing packages you can install them using:
pip install flask gunicorn
To install the HerokuCLI go here. On a Mac it is effortless, as they have an OS X Installer.
We need to have a working Flask app so that we have something to push to Heroku. Lets make a simple one right now. In a new folder create a file called
app.py and put the following code inside of it:
We need to list the dependencies that are required for the Heroku environtment. To list them we can run
pip freeze from a terminal window:
We need to put those dependencies in a file called
requirements.txt. We can do that with one command:
pip freeze > requirements.txt
The last thing that we need is a Procfile. In it’s simplest form you put one process per line that you want to be ran on your Heroku environment.
In our case we want to run our Flask application and we will use gunicorn to do this. Create a file named
Procfile and put the following in it:
web: gunicorn app:app
We can verify that our application works by running
python app.py and then go to
127.0.0.1:5000 in the browser to see it in action:
Lastly, we need to create a new app in Heroku.
Follow the following instructions:
Note: Your Heroku app will not be called
At first this process was odd to me because we do not explicitly tell Heroku what type of app we will be running. Heroku goes through different build packs that they support to determine it. Heroku then goes through
requirements.txt and installs all the dependencies for us!
At this point we are done and our app is now online with its own unique url! I hope this helped you out and if you have any questions along the way please don’t desitate to ask me in the comments! :)