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