Modern Python Projects Transcripts
Chapter: Deployment
Lecture: Uptimer website

Login or purchase this course to watch this video and the rest of the course contents.
0:00 before we can move on to deploy our Web application,
0:03 we need to have some kind of Web application to start with.
0:07 We were not building a Web application in this course,
0:09 so we can't use the uptimer as it is.
0:13 I went ahead and I turned our uptimer into a website.
0:17 Let me walk you through the code.
0:19 The main file of our application is called,
0:23 and I've decided to use the Fast API to build the website.
0:26 Initially, I wanted to use flask,
0:28 but then I decided that Fast API.
0:31 is very similar to flask,
0:34 and we can also benefit from the Asynchronous support that it has.
0:38 So if you don't know fast API,
0:40 I don't worry. It's a very simple website and I will try to explain you
0:44 everything. If you want to learn fast API there is already a course at
0:49 talkpython that will walk you through the basics of it.
0:52 So, what do we have here?
0:54 First we create the fast API application.
0:57 Then we define the folder with our templates.
1:00 So, here we have only one,
1:02 template with some HTML code.
1:06 Next, I have a dictionary that maps some information.
1:09 I will go back to it later,
1:11 and here we have just two websites.
1:14 First we have the home page, that all it does is to display the home.html
1:19 template. So let's open the template Finally,
1:23 and here we have pretty standard HTML stuff.
1:26 So, we import the bootstrap to get some nice styling,
1:30 and then we create a simple to roll layout to display some form,
1:36 and then the responses from the website, looking at HTML code probably doesn't tell you anything.
1:41 So, let's start the Web servers, so we can see how it works.
1:46 So, go to the terminal, make sure that you are using a virtual environment and
1:50 then install the requirements from requirements.txt file.
1:56 I already run this command before,
1:58 so I will have all the requirements ready.
2:00 Once we have the requirements installed,
2:02 we have to start the Web server,
2:05 and now we can go to the browser.
2:11 So, that's how our simple application looks like. We have the text area here where we
2:15 can put some URL to check.
2:17 Then we can click this button and we'll get the responses here. Let's test it with
2:23 some urls. I have some of them store in the main file.
2:27 So let me copy this. So when we click the check,
2:37 we go to the slash check url,
2:40 and it's still displays the same form.
2:42 But this time we get the responses color coded on the right side.
2:46 Since Bootstrap has only one red label,
2:49 I'm using the Orange label for 404.
2:53 Okay, so now that we see how it works,
2:55 let's go back to the code.
2:59 So, that was our home template.
3:02 The only complicated part is the special handling.
3:05 So if the status is zero,
3:07 we output wrong, url.
3:09 And if the status is -1,
3:11 we output time out. Otherwise we output the status.
3:16 Let me show you how it works.
3:18 Let's go back here with http Status website.
3:22 We can add the sleep parameter and here we can specify for how many millisecond we
3:29 wanted to sleep. The library that we used to ping those websites by default has five
3:34 seconds time out. So if we put the time out to seven seconds,
3:39 it should get a time out.
3:41 And let's also put a website that doesn't exist.
3:51 And as you can see here,
3:52 we have two errors. First is the wrong URl and the second gives
3:56 us a time out. Go back to the
4:01 So the home page returns just the form and then from the form,
4:06 we can send the post request to the /check url,
4:11 this function accept check url, sent in the form.
4:14 That's how we have to actually write down that we're sending form using the fast API
4:19 And then since urls is basically a block of text with
4:24 the new line characters, we have to split it to get a list.
4:29 Then we create a list of task using this get status method from the helpers file
4:34 that I will show you in a moment.
4:37 Once we have the list of task we use.
4:39 asyncio.gather to execute all the task in asynchronous manner,
4:44 and then we get a list of statuses and then we'll return.
4:48 This list of status is together with the color dictionary,
4:52 and that's basically what we display here.
4:56 So, this color dictionary is used to map bootstrap classes to status for example
5:02 if we get status 100,we divided by 100 which gives us 1, and then
5:09 we map it to the background primary, the same for any state of starting with 200
5:15 300,400,500. And then we have two special cases for a wrong URL.
5:21 I'm returning status 0,and from a time out,
5:25 I'm returning, Status -1,
5:27 this is not very pretty handling of the special cases,
5:30 but since we only have two I will go with that.
5:36 So the last part is the
5:39 And here we Only use the http x library to perform the head request in an
5:45 asynchronous manner. Http X is basically a replacement for requests Library to perform asynchronous
5:52 request. So if you're familiar with the request library,
5:56 just keep in mind that https is basically a drop in replacement.
6:01 It has the same functions and it accepts the same parameters.
6:05 So, as you can see,
6:06 I have the asynchronous functions basically everywhere.
6:10 One of the reasons I decided to use Fast API is that it supports
6:14 asynchronous code out of the box and for this type of website that we are
6:18 building asynchronous code is actually a good idea.
6:21 Let me show you an example.
6:23 So let's say I have those four urls, each of them has a time
6:27 out of 3 seconds. If I don't use asynchronous code,
6:41 then it would take around 12 seconds to check all those websites.
6:45 If we check the developer console.
6:48 No, no, I wanted to go to the downside.
6:52 Want to move them? Okay,
6:55 let's go to network and let's click check again.
6:58 I'm not doing any caching or anything,
7:03 as you can see it sending and it takes around 4 seconds.
7:08 So, it's much less than the synchronous code would take.
7:12 And if we rerun it and then 3.5 seconds.
7:20 So 3 seconds was the delay from the http status website and half a second was
7:26 some handling on our site. So that's how our simple uptime monitoring works. Let's now try to deploy it.