#100DaysOfWeb in Python Transcripts
Chapter: Days 65-68: Heroku and Python Platform-as-a-Service
Lecture: Deploy and launch your first Heroku app!

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


Talk Python's Mastodon Michael Kennedy's Mastodon