I Can Be Your Heroku, Baby

Our Flask apps work just fine on local Python servers, but the seemingly easy Heroku setup would crash during ‘create’ or ‘open’ leaving us punching walls and yelling at … inanimate objects.Anyway, the point is that Heroku is incredibly temperamental..Preparing your applicationObjective: Boring Python fundamentalsLike most projects, it’s best if you start with a fresh environment, so go ahead and create, copy, or install an environment..I’m using Conda here.$ conda create — name env_nameOr clone your existing working environment$ source activate env_nameYou can always view your list of environments with:$ conda env listMore on managing Conda environments here:Note: if you are using the sample code in PyHeroku, skip the next step and build the environment on the requirements.txt file.$ conda install —- yes —- file requirements.txtInstall all necessary librariesSometimes creating a fresh python environment won’t contain the basic libraries standard with your root environment..Therefore, make sure you’ve got everything installed in your environment required to run your app.Check packages by calling the following script in the active environment$ conda listIf you’re missing essentials (e.g. pandas) or anything else (e.g. Flask, Flask-SQLAlchemy), pip or conda install now..Just keep it all in the root directory.Finally, we create the requirements.txt file by$ pip freeze > requirements.txt`Note: these files already exist in the PyHeroku repo2..The name must be unique to Heroku, but the name must also match the name of your local project directory (that directory that contains the app.py, procfile, requirements files)..In the screenshots below, the heroku app name is ‘PyHeroku’; the directory name is ‘PyHeroku’..Because if you end up needing to modify your directory name due to a Heroku app name not being available, this could create issues with Github or any other code dependencies..Pushing to Heroku via GitObjective: Hooray!.Then execute the following line by line$ git init$ git add .$ git commit -m “init repo”$ heroku create$ git push heroku master$ heroku openYOUR BROWSER SHOULD OPEN THE FUNCTIONING APP!!!You did it!.Create a whole new Heroku app just because you can.But just for solidarity, here’s how to use Github instead of updating and pushing to Heroku git with every minor change and update.Bonus: GithubI haven’t had much luck with this “@route”… get it…?.Anyway, to do so, in deployment tab where we selected Heroku GitSelect `Connect to Github`, and click the button at the bottom.In the field `repo-name`, enter the name of your Github repo, and click `Search`.Click `Connect` to link your Heroku app with your Github repo.Click on `Enable Automatic Deploys`, which will allow the Heroku server to restart your app whenever you push a change to your Github repo.Finally, click on the `Resources` tab, followed by clicking the `edit` button with the pencil icon.Slide the worker switch to the right, then click on `Confirm`.Cross your fingers and pray to deity of choiceHope this helped.. More details

Leave a Reply