{"id":1222,"date":"2014-09-08T20:32:22","date_gmt":"2014-09-08T08:32:22","guid":{"rendered":"http:\/\/p-s.co.nz\/wordpress\/?p=1222"},"modified":"2014-11-20T09:22:51","modified_gmt":"2014-11-19T21:22:51","slug":"deploying-simple-flask-app-on-heroku","status":"publish","type":"post","link":"https:\/\/p-s.co.nz\/wordpress\/deploying-simple-flask-app-on-heroku\/","title":{"rendered":"Deploying simple flask app on heroku"},"content":{"rendered":"<p>I&#8217;m now a fan of Heroku. How cool is it to be able to deploy a Python app to free hosting?!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/p-s.co.nz\/wordpress\/wp-content\/uploads\/2014\/09\/heroku_blackbox.png\" alt=\"Blackbox flask app on Heroku\" width=\"339\" height=\"591\" class=\"aligncenter size-full wp-image-1227\" srcset=\"https:\/\/p-s.co.nz\/wordpress\/wp-content\/uploads\/2014\/09\/heroku_blackbox.png 339w, https:\/\/p-s.co.nz\/wordpress\/wp-content\/uploads\/2014\/09\/heroku_blackbox-172x300.png 172w\" sizes=\"(max-width: 339px) 100vw, 339px\" \/><\/p>\n<p>But in spite of great docs at <a href=\"https:\/\/devcenter.heroku.com\/articles\/getting-started-with-python\" target=\"_blank\">Getting Started with Python on Heroku<\/a> there were a few issues I had to handle. The main problems were because the instructions assumed you wanted to start with their demo app and not your own &#8211; which meant that they only explained things like requirements.txt and Procfile after you needed to have already made them (they were already present in the demo version).<\/p>\n<p>Note &#8211; I am already familiar with git so I don&#8217;t explain that here &#8211; see <a href=\"http:\/\/virantha.com\/2013\/11\/14\/starting-a-simple-flask-app-with-heroku\/\" target=\"_blank\">Starting a simple Flask app with Heroku<\/a> for more fleshed-out instructions.<\/p>\n<p>Anyway, here is what I needed to do at the start:<\/p>\n<p>1) Change the <code>app.run(host='0.0.0.0')<\/code> line to<br \/>\n<code>port = int(os.environ.get(\"PORT\", 33507))<br \/>\napp.run(host='0.0.0.0', port=port)<\/code><\/p>\n<p>Otherwise the app would fail because of a problem with the port when I ran <\/p>\n<p><code>heroku ps:scale web=1<\/code><\/p>\n<p><code>Starting process with command `python main.py`<br \/>\n...<br \/>\nWeb process failed to bind to $PORT within 60 seconds of launch<\/code><\/p>\n<p>2) I really needed to use virtualenvwrapper and create a requirements.txt file e.g.<br \/>\n<code><br \/>\ncd &LT;folder with code in it&GT;<br \/>\nmkvirtualenv blackbox<br \/>\n<\/code><\/p>\n<p>Otherwise heroku wouldn&#8217;t know what dependencies my app needed fulfilled to work successfully.<\/p>\n<p>To update requirements.txt after changes,<br \/>\n<code><br \/>\ncd &LT;folder with code in it&GT;<br \/>\nworkon blackbox<br \/>\npip freeze > requirements.txt<br \/>\ndeactivate blackbox<br \/>\n<\/code><\/p>\n<p>3) I needed to make a Procfile:<\/p>\n<p><code>web: python main.py<\/code><\/p>\n<p>Note, this was a toy flask app so not using gunicorn etc. Probably should look into that later: <\/p>\n<p>4) Setting debug mode off probably isn&#8217;t essential for deployment but probably a good idea anyway: <code>app.debug = False<\/code> before deploying.<\/p>\n<p>Some other points: when developing on a different machine, I needed to supply my public key to heroku from that other machine (<a href=\"http:\/\/stackoverflow.com\/questions\/4269922\/permission-denied-publickey-when-deploying-heroku-code-fatal-the-remote-end\" target=\"_blank\">Permission denied (publickey) when deploying heroku code. fatal: The remote end hung up unexpectedly<\/a>).<\/p>\n<p><code>heroku keys:add ~\/.ssh\/id_rsa.pub<\/code><\/p>\n<p>And the full sequence for upgrading your app after the prerequisites have been fulfilled is:<\/p>\n<ol>\n<li>Make sure you have the port set for heroku<\/li>\n<li>Then git to local repo<\/li>\n<li>Then git push to heroku<\/li>\n<li>Then run <code>heroku ps:scale web=1<\/code> again<\/li>\n<li>Revert from the heroku port back to local for local testing and dev.<\/li>\n<\/ol>\n<p>heroku addons:add redistogo<\/p>\n<p>To add redis support &#8211; NB need to register credit card to use any add-ons, even if free ones.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m now a fan of Heroku. How cool is it to be able to deploy a Python app to free hosting?! But in spite of great docs at Getting Started with Python on Heroku there were a few issues I &hellip; <a href=\"https:\/\/p-s.co.nz\/wordpress\/deploying-simple-flask-app-on-heroku\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-1222","post","type-post","status-publish","format-standard","hentry","category-python","category-web-development"],"_links":{"self":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1222"}],"collection":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/comments?post=1222"}],"version-history":[{"count":13,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1222\/revisions"}],"predecessor-version":[{"id":1265,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/1222\/revisions\/1265"}],"wp:attachment":[{"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/media?parent=1222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/categories?post=1222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/tags?post=1222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}