This tutorial summarizes the steps required to deploy your Flask app to Heroku, taking the post "Create and Deploy a Simple Web Application with Flask and Heroku" by Naveen Venkatesan as a reference.
It assumes you already have Git installed on your machine, a Github account and Heroku account.
Deploying your Flask app to Heroku can be done in a simple way, following the steps below:
You will start by setting up the basis for your app to be developed:
- Create a Github repository for your application;
- Clone your newly created repository using
git clone https://github.com/your_name/repo_name
and cd into the folderrepo_name
; - Create a virtual environment for your application with
python3 -m venv venv
(for example) and activate it. - pip install the following modules:
- flask
- gunicorn
- ghhops-server
- rhino3dm (this is optional)
- your_additional_modules (also optional)
Now comes the fun part: developing your app! You can follow the Quick Start guide in McNeel's Github repository.
- Develop and test your Flask app;
⚠️ CYTHON - if you are using the cython module in your app, please refer to this post on stackoverflow: https://stackoverflow.com/questions/46611178/cython-error-when-pushing-git-on-heroku-master (thanks to lennert-loos for the reference)
- Create a
requirements.txt
file, where all necessary modules will be listed. You can usepip freeze > requirements.txt
to automatically write all modules and versions you have installed via pip; - Create a
Procfile
with the following content:web: gunicorn app:app
, a list of instructions for Heroku to run your application considering you are usinggunicorn
and that you named your appapp
; - Create a
runtime.txt
file with the following content:python-3.9.2
(this will force Heroku to install Python version 3.9.2 - my case). The version should match the one you are using for development - remember that Hops-Server requires Python 3.8 or above.
- Now you need to stage these changes in git with
git add .
(if you want to add all unstaged changes at the same time), commit them withgit commit -m "Meaningful commit message"
and push them to the application's Github repogit push origin main
(VS Code has plugins that make these steps a breeze). - Login to your Heroku account and select the option to create a new app, give it a name and choose an adequate region. Then just click the button
Create app
below; - On your application dashboard click the tab
Deploy
and on the sectionDeployment method
choose to connect Heroku to your application's Github repository; - Choose one of the following options and... deploy!
- Automatic deploys - everytime there's a change to the selected branch on Github, Heroku automatically runs the process;
- Manual deploy - you choose when to deploy the content from Github, with the push of a button.
You can check the activity feed and follow the deployment in progress (click the link of the last entry).
Congratulations, you're done!
Now, all that is left for you to do is click the button Open app
on the top-right corner of Heroku and see it working. If all went well, you should now be able to feed the URL of your app's component's endpoints to the Hops Component's path in Grasshopper.