|
| 1 | +# Session 6: Deploying your code |
| 2 | + |
| 3 | +## Learning outcomes |
| 4 | + |
| 5 | +- Understand the purpose of platforms like Heroku |
| 6 | +- Understand the importance of version control and testing |
| 7 | +- Deploy your first app |
| 8 | + |
| 9 | +## Recap from last session |
| 10 | + |
| 11 | +- What are APIs used for? |
| 12 | +- What's an API key? |
| 13 | +- What’s JSON, and why might we want to use it? |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Let's get started! |
| 18 | + |
| 19 | +## What it means to deploy code |
| 20 | + |
| 21 | +So far, you’ve been running your website “locally”, using your own computer |
| 22 | +as a server, so that your Python and HTML code, working together with Flask, |
| 23 | +can display your website in your browser. This means your website is only |
| 24 | +available on your own machine, and the rest of the world can’t see it. |
| 25 | + |
| 26 | +In order to show off your fantastic work to the world, you’ll need to “deploy’ |
| 27 | +your code to a web server, so that when people type in your website’s URL, |
| 28 | +there’s a server somewhere that knows what to do with that request and how |
| 29 | +to run your code so that people can see your website. |
| 30 | + |
| 31 | +You could use your own computer as a web server, but its processors can |
| 32 | +only handle so much. For example, if your website had 100,000 visitors a |
| 33 | +day, you’d need a computer that was powerful enough to handle all those |
| 34 | +requests coming in (directing people to the right pages, showing them the |
| 35 | +right images, crunching numbers, executing queries, etc.). You also wouldn’t |
| 36 | +want your website to go offline every time your laptop goes offline! |
| 37 | + |
| 38 | +Fortunately, there are services we can use that take care of all these |
| 39 | +headaches for us. For this course, we’re using one called Heroku. |
| 40 | + |
| 41 | + |
| 42 | +## About Heroku |
| 43 | + |
| 44 | +Heroku is a cloud-based Platform as a Service (or “PaaS”) that people can |
| 45 | +use to “deploy” their code online to get their web apps up and running. |
| 46 | +Unlike your laptop, cloud-based platforms are always on, and easily scalable - |
| 47 | +so if your web app becomes massively popular, you can upgrade your |
| 48 | +computing power to handle all that extra traffic without having to buy |
| 49 | +another actual computer. |
| 50 | + |
| 51 | +## Setting up Heroku so it works with the command line |
| 52 | + |
| 53 | +Today we’ll be using the command line, Heroku, and some simple git to |
| 54 | +deploy some test code and get a simple web app “live” on the internet! |
| 55 | +Before doing this for your own project, we’ll work through Heroku’s demo |
| 56 | +together. We're not following the exact setup instructions Heroku gives, so |
| 57 | +please follow what your instructors say step by step. Don't get tempted to |
| 58 | +rush ahead! |
| 59 | + |
| 60 | +1. Log into your [Heroku account](https://id.heroku.com/login). |
| 61 | +2. Click **Python** from the options that show, then you'll automatically be |
| 62 | + taken to another page to get started |
| 63 | + |
| 64 | +3. You can ignore what the introduction says, because our setup is a little |
| 65 | + bit different. Simply click button that says **“I’m ready to start”** , and |
| 66 | + you’ll be taken to a new page. |
| 67 | +4. When you see the button below, click the arrow on the right side to |
| 68 | + select your operating system, and download the **“Heroku Command** |
| 69 | + **Line Interface”**. |
| 70 | + |
| 71 | + |
| 72 | +Look for the installer in your Downloads folder, or wherever downloaded files |
| 73 | +usually end up on your computer, and then open the installer and follow the |
| 74 | +instructions. Installing this allows your command line to understand what you |
| 75 | +mean when you use the word heroku in your commands, and also makes it |
| 76 | +possible for you to push your code from your computer to Heroku’s servers |
| 77 | +(you’ll hear more about this later). If you have a Windows laptop, choose the |
| 78 | +"Recommended" installation. It might take a little time to install. |
| 79 | + |
| 80 | +Once you’ve installed the Heroku CLI, open your command line and type |
| 81 | +heroku login. You’ll need to use your Heroku account details here. |
| 82 | + |
| 83 | +When you’ve finished Step 6 above, go back to your web browser, scroll |
| 84 | +down a bit, and click the button saying you’ve installed the Heroku CLI. |
| 85 | + |
| 86 | +## Getting your Github code ready to work with Heroku |
| 87 | + |
| 88 | +We’ll be using [Heroku’s demo](https://devcenter.heroku.com/articles/getting-started-with-python#prepare-the-app) |
| 89 | +to see how to use git and Heroku to deploy a simple web app. For homework, you'll do the same thing for your competition |
| 90 | +project. |
| 91 | + |
| 92 | +1. We'll be “cloning” some files from a GitHub repository that already |
| 93 | + exists, so before we do that, we want to make sure we know where |
| 94 | + we're putting these files on our computer. Use the command line to |
| 95 | + navigate to wherever you’ve got your Code First Girls Python work. For |
| 96 | + example (yours will look different, depending on your file path) cd |
| 97 | + Documents/Code_First_Girls/Python_Stuff/Heroku_Demo |
| 98 | +2. Now that we know where we'll be putting the project files from GitHub, |
| 99 | + we'll use git clone to create a local copy on our computer. Type this into |
| 100 | + your command line: git clone https://github.com/heroku/python-getting-started.git |
| 101 | + |
| 102 | + |
| 103 | +3. _Now we’ll make a folder to put these project files in (and navigate to_ |
| 104 | + _that folder at the same time) by typing_ cd python-getting-started |
| 105 | + |
| 106 | +If you navigate in Explorer (Windows), Finder (Mac), or your command line to |
| 107 | +the **python-getting-started** folder you just created, you’ll see you’ve now |
| 108 | +got a copy of the files from GitHub, on your local machine: |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +You’ll need to create and deploy your simple app. You don’t have to write any |
| 113 | +code for this demo, because we’ve got all the files cloned from GitHub, and |
| 114 | +Heroku knows what to do with them. In your command line, type heroku |
| 115 | +create |
| 116 | + |
| 117 | +## Where’s my website and how do I update it? |
| 118 | + |
| 119 | +When you create a web app, Heroku assigns you a random name/URL for it. |
| 120 | + |
| 121 | +1. Notice the light blue text below – copy the URL into your web |
| 122 | + browser....what do you see? |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +Notice the green text above – when you create an app with Heroku, Heroku |
| 127 | +makes an additional remote git repo, and associates it with your local git |
| 128 | +repository. It’s the repo in green that you push code to in order to make it |
| 129 | +live. |
| 130 | + |
| 131 | +Remember, the only way to update your “live” website files is by pushing |
| 132 | +code from your local git repo to Heroku’s git repo (see the picture below). |
| 133 | +This means we’re not done yet, because we haven’t pushed any code to the |
| 134 | +Heroku Master. |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +In your command line, type git push heroku master. What do you see now, |
| 139 | +when you navigate to your web app’s URL? |
| 140 | + |
| 141 | +In preparation for setting up Heroku for your competition project, have a read |
| 142 | +through the text at that URL, and also here. |
| 143 | + |
| 144 | + |
| 145 | +## Getting your own project set up on Heroku |
| 146 | + |
| 147 | +Be sure to have a look through the rest of the Getting Started documentation |
| 148 | +to get your project set up. If you see “Django” mentioned, don’t worry about |
| 149 | +that – we’re using Flask instead. |
| 150 | + |
| 151 | +Since your website only has one URL, only one person on your team needs to |
| 152 | +do the create and deploy bits on Heroku for your competition project. |
| 153 | +However, if you want more than one team member to be able to push code |
| 154 | +to your live site (your Heroku remote repo), each person needs to be given |
| 155 | +access privileges for your app. You can set that up by clicking on the Heroku |
| 156 | +logo on the top left of the page, then clicking on your app, then the **Access** |
| 157 | +tab, and then **Add collaborator**. |
| 158 | + |
| 159 | +## Why it’s important to use git for version control |
| 160 | + |
| 161 | +Whether you’re working on a project alone, or in a group, it’s really important |
| 162 | +to use git for version control. There are lots of benefits: |
| 163 | + |
| 164 | + |
| 165 | +- If you’ve been committing code regularly, you won’t lose all your work |
| 166 | +if something happens to your computer (because you’ve got a copy of |
| 167 | +it in your git repository in the cloud) |
| 168 | +- Having your work on Github helps you be a part of the coding |
| 169 | +community (and even get your first job as a developer!) because you |
| 170 | +can show off your code for others to see, and contribute to other |
| 171 | +people’s public projects |
| 172 | +- It helps keep your team organised and working on the right bits, |
| 173 | +because every person in your team can see a master copy of code, any |
| 174 | +code branches you have, and commits and comments that might have |
| 175 | +important information (like, if a commit fixed a bug) |
| 176 | + |
| 177 | +Remember, as shown in the picture earlier in these notes, anything you push |
| 178 | +to Heroku is “live” (available for the world to see), so it’s really, really |
| 179 | +important that you only push code to Heroku that comes from the Master |
| 180 | +branch of your project’s git repository. |
| 181 | + |
| 182 | +If you’ve followed best practice and tested your code locally on your laptop |
| 183 | +before committing it to your team’s Master code branch, then your code |
| 184 | +should work as expected when it’s deployed on Heroku, because it’s just a |
| 185 | +copy of the code you’ve already tested. |
| 186 | + |
| 187 | + |
| 188 | +## Homework |
| 189 | + |
| 190 | +1. Deploy your competition site on Heroku. If you’re having trouble, please |
| 191 | + let an instructor know. |
| 192 | +2. Start using basic git commands to commit your code and push it to |
| 193 | + Heroku (check out the notes from Session 5). |
0 commit comments