WEBVTT

00:00.080 --> 00:03.290
This is now step five for this final project.

00:03.290 --> 00:06.980
And in this step we're going to create a new Python program.

00:07.010 --> 00:07.490
Okay.

00:07.490 --> 00:10.850
So we finished the first program here with step number four.

00:10.850 --> 00:17.240
And we're going to create a new one here where we're going to add a web server with flask.

00:17.240 --> 00:24.410
And what we want to be able to do is to show a web page and say how many photos were taken since the

00:24.410 --> 00:29.090
last time that you checked the web page, and then show the last photo.

00:29.090 --> 00:34.850
So in this step five, we're going to initialize the web page and say how many new photos were taken

00:34.850 --> 00:36.080
since you last checked.

00:36.080 --> 00:39.440
And displaying the photo is going to be the following step.

00:39.470 --> 00:39.830
All right.

00:39.830 --> 00:42.680
So let's get started with this new file.

00:42.710 --> 00:52.910
I'm gonna close that one and I'm going to save this one as as project step five dot pi.

00:54.380 --> 00:57.560
So to create a web server let's use flask.

00:57.560 --> 00:59.900
And let's first do some initialization.

00:59.900 --> 01:04.260
So from flask Import flask.

01:04.290 --> 01:13.170
Then we're going to create an app here is equal to flask uppercase with underscore underscore name and

01:13.170 --> 01:15.150
then underscore underscore.

01:15.180 --> 01:17.340
Then we're going to create the different routes.

01:17.340 --> 01:25.350
And at the end we do app dot run with host is equal to 0.0.0.0.

01:26.310 --> 01:26.550
All right.

01:26.580 --> 01:29.160
So that's the structure of our program.

01:29.820 --> 01:32.400
Then we want a homepage.

01:32.400 --> 01:35.910
So we want a simple default route.

01:35.910 --> 01:37.500
That's just going to say hello.

01:37.500 --> 01:46.140
So we do app dot route with a slash and we do a div index okay.

01:46.170 --> 01:47.730
We're not going to do anything on this one.

01:47.730 --> 01:50.670
Just return hello.

01:51.180 --> 01:52.920
And that's it for that okay.

01:52.920 --> 01:56.670
So I'm gonna save that and I can start to run it.

01:56.670 --> 02:01.800
So I'm gonna keep this one here, this terminal to run the first program.

02:01.800 --> 02:03.700
And I'm going to open a new terminal.

02:04.600 --> 02:13.060
Let's go to documents Python programs, and let's do Python three project.

02:13.090 --> 02:15.520
Step five.

02:16.300 --> 02:16.660
Okay.

02:16.690 --> 02:18.670
It's creating a web server.

02:18.700 --> 02:21.340
I do ctrl click on that one.

02:22.570 --> 02:22.990
All right.

02:22.990 --> 02:27.970
And we get to this web page you see with the IP address the port.

02:27.970 --> 02:29.470
And we see hello.

02:30.280 --> 02:36.130
So now I want to have this one slash check photos.

02:36.640 --> 02:36.820
Okay.

02:36.820 --> 02:40.150
So for now we have a 404 not found error.

02:40.180 --> 02:42.190
So we're going to add a new route.

02:42.220 --> 02:43.960
Let's go back to Thonny.

02:44.590 --> 02:54.190
And let's do app dot route slash check photos.

02:54.550 --> 02:56.770
I'm going to create a function here.

02:56.800 --> 02:59.800
Check photos like this.

02:59.830 --> 03:02.530
Okay so once again for URL you use dashes.

03:02.560 --> 03:07.520
It's better for Python functions, better to use underscores.

03:08.810 --> 03:12.200
And what do I do in this function?

03:12.230 --> 03:15.710
Well I will check the log file.

03:15.740 --> 03:15.920
Okay.

03:15.950 --> 03:17.720
Because that's where I'm going to get the information.

03:17.720 --> 03:20.360
So here is all the photos I have.

03:20.450 --> 03:23.660
And I have my photo logs dot txt okay.

03:23.690 --> 03:26.870
And in this you see I have two photos for now.

03:26.900 --> 03:27.080
Okay.

03:27.080 --> 03:28.820
That was the previous run of the program.

03:28.820 --> 03:32.330
So I'm going to check first if this file exists okay.

03:32.360 --> 03:36.410
If it doesn't exist then I'm going to say for example that no photo is available.

03:36.410 --> 03:43.460
And if it exists I'm going to open it and I'm going to count how many lines do I have so that I can

03:43.460 --> 03:49.730
say how many photos we have, and maybe also even compute the difference between the last time that

03:49.730 --> 03:50.390
we checked.

03:50.420 --> 03:52.670
Okay, so let's do that step by step.

03:52.670 --> 03:56.270
First let's add a variable here.

03:57.170 --> 04:02.690
So right here log file name.

04:02.690 --> 04:07.560
And I use it also as uppercase, so that we don't try to modify it later.

04:07.560 --> 04:13.260
And this one is going to be the same we have created in the previous program because, well, we need

04:13.260 --> 04:14.490
to read from the same file.

04:14.490 --> 04:26.340
So slash home slash pi slash photos final project and then slash photo blogs dot txt.

04:27.210 --> 04:40.410
Now the first thing I will do is check if os dot path dot exists with the log file name.

04:40.650 --> 04:47.850
So if it exists what we are going to do I'm going to put a comment for now is to count lines.

04:48.030 --> 04:51.690
If it doesn't exist, it means we don't have any photos.

04:51.690 --> 04:57.930
So I'm going to do return no photo available.

04:58.650 --> 04:58.920
All right.

04:58.920 --> 05:03.210
So we already have a case here where we're going to see no photo available.

05:03.240 --> 05:04.560
Now to count the lines.

05:04.560 --> 05:06.460
Well I first need to open the file.

05:06.460 --> 05:14.470
So let's do with open log file name with the reading permission.

05:15.190 --> 05:17.950
And then as f for the file.

05:18.040 --> 05:20.410
And there are probably different ways to do that.

05:20.410 --> 05:27.610
But I'm going to do simply for line in F okay I'm going to do a for loop to go through each line.

05:27.610 --> 05:30.520
That's also going to be useful for the next step.

05:30.520 --> 05:34.930
And then every time that I have a new line, I just increment a counter.

05:34.930 --> 05:37.000
So I will create a counter here.

05:37.030 --> 05:43.120
For example, I can do this at the beginning of the function line counter is equal to zero.

05:43.150 --> 05:51.370
So for every new line I do line counter plus equal one okay.

05:51.400 --> 05:52.900
So that's very basic.

05:52.900 --> 05:55.270
It's very basic but it works okay.

05:55.300 --> 05:57.460
So let's keep things simple for now.

05:57.490 --> 05:59.920
And then what do I want to print.

05:59.920 --> 06:05.050
So I don't want to just print how many lines we have in this file.

06:05.050 --> 06:10.220
I want to print how many new lines we have in this file since the last time that we checked.

06:10.220 --> 06:15.500
So since the last time that we requested this route.

06:15.500 --> 06:20.330
And so how to do this, we will need to keep somewhere the previous line counter.

06:20.330 --> 06:25.040
And because every time when we get this route we're going to enter the function again.

06:25.040 --> 06:28.250
But all the local variables here are going to be lost.

06:28.250 --> 06:35.810
So we need a global variable here I'm going to name it previous line counter.

06:36.950 --> 06:38.870
And let's initialize it to zero.

06:39.590 --> 06:45.080
And because I'm going to use it inside this function and I'm probably going to modify it I do global

06:46.250 --> 06:50.660
previous line counter.

06:50.660 --> 06:56.600
And what's going to be interesting for me is actually the difference between the current line counter

06:56.600 --> 06:58.340
and the previous one.

06:58.340 --> 07:00.050
So after this for loop.

07:00.050 --> 07:03.470
So here you can see the indentation is very important okay.

07:03.500 --> 07:10.650
After the for loop here actually we could even go back here because we will just open the file, count

07:10.680 --> 07:13.140
the lines, and then close the file.

07:13.140 --> 07:24.510
So right here in this indentation but still inside the if I will do difference is equal to previous

07:25.530 --> 07:26.340
line.

07:28.470 --> 07:32.910
Counter minus line counter which is the current one.

07:33.750 --> 07:44.100
And then I also need to do previous line counter is equal to line counter.

07:45.570 --> 07:45.990
Why?

07:46.020 --> 07:51.360
Because while the line counter that we read now, the next time that we're going to enter this function

07:51.360 --> 07:53.070
is going to be the previous one.

07:53.100 --> 07:57.150
And so to be able to keep this value we need to store it okay.

07:57.150 --> 08:02.640
So every time we compare with the previous one and then we save the current one as the previous one

08:02.640 --> 08:05.280
for the next run of that function.

08:05.310 --> 08:05.700
Great.

08:05.700 --> 08:16.060
And now what we can do is we can return a string that says so the difference, for example str difference.

08:18.010 --> 08:27.790
Plus new with space photos since you last checked.

08:28.750 --> 08:32.110
All right so let me recap here on what we did.

08:32.140 --> 08:34.060
So first we initialized flask.

08:34.090 --> 08:35.530
Well nothing really new.

08:35.560 --> 08:38.380
We check if this file exists.

08:38.380 --> 08:40.660
If no we say no photo available.

08:40.660 --> 08:44.500
And if yes we are going to count the lines.

08:44.500 --> 08:47.890
So let's say that we have two lines in our file.

08:47.890 --> 08:50.830
Well, and here actually it should be the opposite.

08:50.830 --> 08:56.260
And you can see it's important to review the code because you can easily check sometimes if something

08:56.260 --> 08:57.220
is not logic.

08:57.220 --> 09:05.950
So instead of doing previous minus current we're going to do the current minus previous line.

09:09.320 --> 09:09.770
Count.

09:10.040 --> 09:12.560
So let's say we have two new lines.

09:12.560 --> 09:17.750
We do two -0 because it's zero the first time.

09:17.750 --> 09:19.910
So we have the difference of two lines.

09:19.910 --> 09:22.010
And we can say we have two new photos.

09:22.010 --> 09:26.150
Since you last checked we also record the previous as the current.

09:26.150 --> 09:28.610
So now previous line counter is going to be two.

09:28.640 --> 09:33.830
And now let's say that you check again and you have three lines inside the file.

09:33.830 --> 09:35.030
So then you will do.

09:35.060 --> 09:40.400
Difference is equal to three minus the previous and the previous is two.

09:40.430 --> 09:43.910
So this time the difference is only going to be one.

09:43.940 --> 09:44.630
Okay.

09:45.050 --> 09:47.180
So let's save that.

09:47.180 --> 09:48.020
And let's see.

09:48.050 --> 09:50.870
Well let's see what we have in this folder for now.

09:51.200 --> 09:53.810
But we are not running the script.

09:54.320 --> 09:56.510
I'm gonna just open that file.

09:56.510 --> 09:59.000
So we have two photos okay.

09:59.930 --> 10:11.940
So if I run the project step five again and I go back here, you see that this should work.

10:11.940 --> 10:13.170
And actually it's working.

10:13.170 --> 10:14.970
So we find the root.

10:14.970 --> 10:19.410
But we have an internal server error which means it's probably a Python error.

10:19.410 --> 10:21.450
And we can see the Python error here.

10:21.450 --> 10:29.670
And yes name OS is not defined because I use C, I use OS but I don't import OS.

10:30.480 --> 10:33.390
So import OS.

10:33.420 --> 10:34.710
Let's save.

10:34.740 --> 10:36.780
Let's run again.

10:38.190 --> 10:38.850
Okay.

10:39.450 --> 10:40.500
Let's go back here.

10:40.530 --> 10:42.120
Let's check again.

10:42.120 --> 10:49.050
And you see we have two new photos since you last checked because we have two photos where we have two

10:49.050 --> 10:50.880
lines inside this photo logs.

10:50.880 --> 10:57.540
But now note that if I refresh again I have zero new photos.

10:57.540 --> 11:00.810
And that's normal because we have checked twice.

11:00.810 --> 11:03.180
And between the two times we didn't take any new photos.

11:03.180 --> 11:04.890
So we have zero new photos.

11:04.920 --> 11:05.310
All right.

11:05.310 --> 11:11.340
And that's the end of this step number five, which we're going to use directly for the next one.
