#100DaysOfWeb in Python Transcripts
Chapter: Days 65-68: Heroku and Python Platform-as-a-Service
Lecture: Deploy and launch your first Heroku app!
0:00 Time for some fun.
0:01 Now to create the actual Heroku application.
0:05 This is the one that you could have created
0:08 in one of the first videos where you had the web GUI
0:11 and you could have clicked that button
0:13 we're going to do it over the command line
0:14 'cause that's awesome, but
0:17 one thing to keep in mind is that
0:20 the app name needs to be unique.
0:23 This is going to be a unique URL
0:25 with the Heroku domain added on to it.
0:28 So you have to give this some thought.
0:30 What I'm going to type here, you can't copy to follow along.
0:35 So let's come up with something interesting
0:38 I will try throwing pybites in front of it
0:43 which should actually give me something unique.
0:45 So let's go Heroku create, pybites100days.
0:51 Now let's just try that
0:54 and hopefully this will work.
0:57 Says it's creating it, done, bingo.
1:00 That's what it looks like if your app was unique.
1:04 If your app name was unique
1:05 and it actually worked.
1:07 Clearly I've tried this before recording
1:10 and I came up with something that was not unique.
1:13 That's why I'm laughing.
1:14 Now if we actually browse to this URL
1:17 which I'm not going to do
1:19 because there's nothing there to see yet
1:22 we would see our website, we would see everything
1:25 that's stored in the Git repository for this app.
1:30 Now, we initialized our local folder as a Git repository
1:34 but we haven't pushed that code off to the Heroku cloud.
1:39 We will do that in just a second.
1:41 To see that we are going to be pushing our code to that
1:47 we can actually use the Git remote command.
1:49 So git remote -v
1:52 and it tells us that we are pushing it to this URL here
1:56 this Git repo here, which matches what we put up here.
2:02 So perfect, we know we're going to be pushing our code
2:04 to that correct place.
2:08 Now, how do we push the code?
2:10 We use our standard Git commands
2:12 we can go git add. ignore those Windows messages
2:17 and then we can do a git commit.
2:20 And this will be our first commit of our code to the repo
2:23 we can put that in the message, like so.
2:27 Git commit -m "our first push of code"
2:34 to Heroku excellent.
2:39 And we'll commit that, and now we can push it.
2:42 So, git push heroku master.
2:49 And that's it, you've just pushed your code to the repo.
2:53 It should end, if everything was successful
2:55 with verifying deploy done
2:58 to https and your Git repo.
3:01 You would've seen all of those packages installed
3:04 that you had in your requirements.txt file.
3:08 Now obviously with the magic of editing
3:10 I have skipped through all of this, but
3:12 you would have noticed that this took
3:14 quite some time to install.
3:16 And to actually upload, so.
3:19 Now that we've been patient, and we've seen through that
3:22 there's one more little thing that we need to do.
3:25 And that's actually tell Heroku how to run our app.
3:29 Now, we create a file called a Procfile.
3:34 And all it does is simply
3:36 tell Gunicorn, that's the web server
3:39 what script to run, what file to run
3:41 how to run our application.
3:45 In our directory we have an app.py file.
3:49 Now that's the script that runs our app.
3:52 So we need to put in our Procfile
3:54 that that's the name of the application.
3:57 So, vim Procfile, just create this in the parent directory
4:02 so where your applications run from.
4:05 So vim Procfile, and in this file
4:09 all you need to run is web: Gunicorn
4:13 so this is saying that we're using Gunicorn
4:15 for our web server. And app is our app.
4:21 Now which app is which?
4:23 The app on the left hand side of the colon
4:25 is the name of the file that we will be executing.
4:29 So if the name of our application was
4:32 let's call it, if it was, julian.py
4:36 we would run our application as web Gunicorn julian app.
4:41 But in this case our app is named app.py
4:45 so let's keep it as app:app.
4:49 That's all we need to put in there.
4:52 Let's save the file.
4:55 And last but not least we have a runtime file
4:58 that we sort of don't have to put in
5:01 but it's probably a good practice to do.
5:04 This file runtime.txt
5:08 will simply tell Heroku what version of Python
5:12 we want to run.
5:13 This is important if your app obviously has dependencies
5:17 on the version of Python.
5:19 Now, as I mentioned before, Heroku does have Python 3.7.
5:24 So you can simply put in Python 3.7 in there
5:28 for the sake of this
5:30 you can actually specify whatever you want.
5:32 So I can do 3.6.0 because that's the version of Python
5:36 I'm running at the moment. Right, that's that.
5:43 All we have to do now is actually push that code
5:46 off to Heroku again.
5:48 And that's done in the same fashion, let's do that quickly.
5:51 git add. git commit -m
5:56 and we'll say Procfile and runtime file.
6:02 And then git push heroku master.
6:07 And with the code up there, it's time to run the app.
6:10 Let's just clear all of this out.
6:12 For our app to run, we need to tell Heroku to run it.
6:15 And we do this by spinning up what they call a dyno.
6:20 It's pretty much, if you've worked with AWS before
6:23 it's an instance of our application.
6:27 Now on the free tier, we can only have a couple of these
6:30 running at a time, so this won't matter
6:33 if this is the first time you've done this.
6:36 So we'll do heroku ps scale
6:39 we want our dyno to be scalable.
6:41 And we want web=1.
6:44 And what the one indicates is that we only want our dyno
6:48 or our application instance, to scale to one dyno.
6:53 And we're limiting that just because we're on the free tier.
6:56 Once we have that we can hit enter
6:59 and this will actually spin up our dyno for us
7:02 scaling, done, now running web at one free.
7:07 And the exciting part is we now open it.
7:12 We do Heroku open and this will open a web browser
7:16 to our application. And there it is.
7:19 pybites100days.herokuapp.com, my test Heroku index page.
7:25 Everything that was in the index.html file.
7:28 Perfect, you've just deployed your first app to Heroku.
7:32 So exciting, be proud of yourself.
7:35 And now think of all the wonderful possibilities ahead of you.