(window.webpackJsonp=window.webpackJsonp||[]).push([[173],{697:function(e,t,o){"use strict";o.r(t);var r=o(56),i=Object(r.a)({},(function(){var e=this,t=e.$createElement,o=e._self._c||t;return o("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[o("h1",{attrs:{id:"deploy-the-movie-watchlist-project-to-heroku"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#deploy-the-movie-watchlist-project-to-heroku"}},[e._v("#")]),e._v(" Deploy the movie watchlist project to Heroku")]),e._v(" "),o("div",{staticClass:"custom-block tip"},[o("p",{staticClass:"custom-block-title"},[e._v("TIP")]),e._v(" "),o("p",[e._v("List of all code changes made in this lecture: "),o("a",{attrs:{href:"https://diff-store.com/diff/section14__19_deploying_to_heroku",target:"_blank",rel:"noopener noreferrer"}},[e._v("https://diff-store.com/diff/section14__19_deploying_to_heroku"),o("OutboundLink")],1)])]),e._v(" "),o("h2",{attrs:{id:"files-we-need-to-add-for-heroku-deployment"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#files-we-need-to-add-for-heroku-deployment"}},[e._v("#")]),e._v(" Files we need to add for Heroku deployment")]),e._v(" "),o("p",[e._v("There are three files we may need to change or add to our project:")]),e._v(" "),o("ul",[o("li",[o("code",[e._v("Procfile")]),e._v(", which tells Heroku what command to run")]),e._v(" "),o("li",[o("code",[e._v("runtime.txt")]),e._v(", which tells Heroku what Python version to use")]),e._v(" "),o("li",[o("code",[e._v("requirements.txt")]),e._v(", which we must make sure has all dependencies our project uses")])]),e._v(" "),o("h3",{attrs:{id:"writing-the-procfile"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#writing-the-procfile"}},[e._v("#")]),e._v(" Writing the "),o("code",[e._v("Procfile")])]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v('web: gunicorn "movie_library:create_app()"\n')])])]),o("p",[e._v("We'll be using "),o("code",[e._v("gunicorn")]),e._v(", which takes in as an argument the command to run to find the Flask app.")]),e._v(" "),o("p",[e._v("Here we're telling "),o("code",[e._v("gunicorn")]),e._v(" that it should look in the "),o("code",[e._v("movie_library")]),e._v(" module (i.e. "),o("code",[e._v("movie_library.__init__.py")]),e._v("), and in that file it should run the "),o("code",[e._v("create_app()")]),e._v(" function. The return value of that function is the app.")]),e._v(" "),o("h3",{attrs:{id:"writing-the-runtime-txt"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#writing-the-runtime-txt"}},[e._v("#")]),e._v(" Writing the "),o("code",[e._v("runtime.txt")])]),e._v(" "),o("p",[e._v("To tell Heroku what Python version to use, we just add another single-line file called "),o("code",[e._v("runtime.txt")]),e._v(". Make sure to use the latest Python version"),o("sup",{staticClass:"footnote-ref"},[o("a",{attrs:{href:"#fn1",id:"fnref1"}},[e._v("[1]")])]),e._v(" supported by Heroku, as new Python versions often add features, improvements, and potentially fix bugs and security issues.")]),e._v(" "),o("p",[e._v("At the time of recording, the latest supported Python version was Python 3.10.3, so that's what we'll write in "),o("code",[e._v("runtime.txt")]),e._v(":")]),e._v(" "),o("div",{staticClass:"language- extra-class"},[o("pre",{pre:!0,attrs:{class:"language-text"}},[o("code",[e._v("python-3.10.3\n")])])]),o("h3",{attrs:{id:"verifying-the-requirements-txt"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#verifying-the-requirements-txt"}},[e._v("#")]),e._v(" Verifying the "),o("code",[e._v("requirements.txt")])]),e._v(" "),o("p",[e._v("The easiest way to make sure your "),o("code",[e._v("requirements.txt")]),e._v(" file has everything your app needs is to do the following:")]),e._v(" "),o("ol",[o("li",[e._v("Deactivate your virtual environment")]),e._v(" "),o("li",[e._v("Delete your virtual environment folder (mine is called "),o("code",[e._v(".venv")]),e._v(")")]),e._v(" "),o("li",[e._v("Re-create your virtual environment folder, using Python 3.10.3, and activate it")]),e._v(" "),o("li",[e._v("Install the requirements by running "),o("code",[e._v("pip install -r requirements.txt")]),e._v(" (this is what Heroku will run when you deploy)")]),e._v(" "),o("li",[e._v("Run your app and make sure it works")])]),e._v(" "),o("p",[e._v("If your app works without any problems, then you've got everything you need!")]),e._v(" "),o("h2",{attrs:{id:"config-variables-needed-for-heroku"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#config-variables-needed-for-heroku"}},[e._v("#")]),e._v(" Config variables needed for Heroku")]),e._v(" "),o("p",[e._v("Our application uses MongoDB Atlas, so grab the connection string and add it as a Heroku Config Var.")]),e._v(" "),o("p",[e._v("This is under the Heroku project -> Settings -> Config Vars.")]),e._v(" "),o("p",[e._v("Make sure the Config Var you add has the correct name, depending on what you've used in "),o("code",[e._v("__init__.py")]),e._v(" to access it from the environment variables.")]),e._v(" "),o("p",[e._v("In my case, that's "),o("code",[e._v("MONGODB_URI")]),e._v(".")]),e._v(" "),o("h2",{attrs:{id:"putting-the-movie-watchlist-on-github"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#putting-the-movie-watchlist-on-github"}},[e._v("#")]),e._v(" Putting the movie watchlist on GitHub")]),e._v(" "),o("p",[e._v("To deploy to Heroku you'll want to put your entire code in GitHub, if it isn't there already.")]),e._v(" "),o("p",[e._v("Create a new public (or private) GitHub repository and push to it all your project files.")]),e._v(" "),o("p",[e._v("You don't want to push:")]),e._v(" "),o("ul",[o("li",[o("code",[e._v(".vscode")])]),e._v(" "),o("li",[o("code",[e._v(".venv")]),e._v(" (or any other virtual environment folder)")]),e._v(" "),o("li",[e._v("Hidden files such as "),o("code",[e._v(".DS_Store")])]),e._v(" "),o("li",[e._v("Compiled Python files, all files ending in "),o("code",[e._v(".pyc")])]),e._v(" "),o("li",[e._v("Python cache folders, all "),o("code",[e._v("__pycache__")]),e._v(" folders")])]),e._v(" "),o("p",[e._v("Think carefully about what files you're adding to your repository. You only want to add those files which are necessary and cannot be created by whoever downloads your code.")]),e._v(" "),o("p",[e._v("For example, "),o("code",[e._v("*.pyc")]),e._v(" and "),o("code",[e._v("__pycache__")]),e._v(" are generated by Python when you run the code. Therefore, it's not necessary to add them to the repository.")]),e._v(" "),o("p",[e._v("Similarly, "),o("code",[e._v(".vscode")]),e._v(" stores workspace-specific Visual Studio Code settings. Whoever downloads your code will have their own settings, or they may not even be using VSCode!")]),e._v(" "),o("p",[e._v("The virtual environment folder should be created in their computer by each person who downloads the project. You shouldn't include it in your repository.")]),e._v(" "),o("p",[e._v("And so on! Be critical about what you add, and only add what's precisely necessary!")]),e._v(" "),o("h2",{attrs:{id:"deploying-on-heroku"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#deploying-on-heroku"}},[e._v("#")]),e._v(" Deploying on Heroku")]),e._v(" "),o("p",[e._v("Now that you've got the GitHub repo, all you have to do is create a Heroku project and link it to the GitHub repo.")]),e._v(" "),o("p",[e._v('Then you can hit "Deploy" (under the project -> Deploy, and scroll to the bottom), and you should see it become live shortly!')]),e._v(" "),o("hr",{staticClass:"footnotes-sep"}),e._v(" "),o("section",{staticClass:"footnotes"},[o("ol",{staticClass:"footnotes-list"},[o("li",{staticClass:"footnote-item",attrs:{id:"fn1"}},[o("p",[o("a",{attrs:{href:"https://devcenter.heroku.com/articles/python-support#supported-runtimes",target:"_blank",rel:"noopener noreferrer"}},[e._v("Heroku Python Runtimes (Official Documentation)"),o("OutboundLink")],1),e._v(" "),o("a",{staticClass:"footnote-backref",attrs:{href:"#fnref1"}},[e._v("↩︎")])])])])])])}),[],!1,null,null,null);t.default=i.exports}}]);