WEBVTT

00:00.050 --> 00:05.390
This is now the step six, and that's going to be the end of the Python code for the final project.

00:05.420 --> 00:06.650
Then we have a bonus step.

00:06.650 --> 00:09.740
But here we're going to finish the Python program.

00:09.740 --> 00:13.310
So what we had in the step five is we have a web server.

00:13.310 --> 00:16.730
And you can see we can have this page with check photos.

00:16.730 --> 00:22.310
And we can see how many new photos were taken since we last checked this URL.

00:22.310 --> 00:29.450
Now what I want to do is just add the file so the file name for the photo and print that photo.

00:29.480 --> 00:30.530
Let's go back here.

00:30.530 --> 00:32.390
I'm not going to change the logic here.

00:32.390 --> 00:33.530
That's going to be the same.

00:33.530 --> 00:36.110
I'm just going to add new stuff okay.

00:36.140 --> 00:38.750
So I'm going to add new stuff to that one.

00:38.750 --> 00:40.460
And to add new stuff to this.

00:40.490 --> 00:48.140
Well, instead of just returning the string, what I could do first is to say I'm going to do like this.

00:49.010 --> 00:54.920
I'm going to save that in a variable message is equal to okay.

00:54.950 --> 01:02.540
And then I can do stuff like message plus equal and add more strings.

01:02.540 --> 01:05.820
And then I can do Return message.

01:05.850 --> 01:06.180
All right.

01:06.210 --> 01:09.690
That's going to be better if we scale the application a bit.

01:09.690 --> 01:14.220
So what I want to say here is last photo.

01:14.550 --> 01:19.380
And then I want to add the last photo file name.

01:19.380 --> 01:21.780
How do I get the last photo file name.

01:21.780 --> 01:28.290
Well given the code structure we have here already, what I could do is just create a variable here.

01:29.160 --> 01:34.950
I'm going to name it last photo file name.

01:35.280 --> 01:36.870
It's going to be an empty string.

01:37.320 --> 01:47.310
And then when I go through each line I increment the counter and I do last photo file name is equal

01:47.310 --> 01:55.200
to the line that we are currently reading, and maybe dot strip to remove any unwanted spaces at the

01:55.200 --> 01:55.500
end.

01:55.500 --> 01:56.700
And why do I do this?

01:56.700 --> 02:01.770
So every time we have a new line, the last photo file name is going to be the new line.

02:01.770 --> 02:03.540
But then the for loop is going to end.

02:03.540 --> 02:06.150
So we're going to end on the last element.

02:06.150 --> 02:12.420
So simply put, after this for loop, what's going to be inside this variable is the last element of

02:12.420 --> 02:13.050
the file.

02:13.050 --> 02:17.580
So basically the last line of the file which corresponds also to the last photo.

02:17.610 --> 02:17.820
All right.

02:17.850 --> 02:23.130
So that's a quick way to just get the last photo provided this code structure.

02:23.130 --> 02:25.170
And you see I create the variable here.

02:25.170 --> 02:29.370
So same indentation as here because I'm going to use it there.

02:29.400 --> 02:29.610
Okay.

02:29.640 --> 02:31.170
So for the scope it's important.

02:31.170 --> 02:34.170
So now I have my last photo.

02:34.170 --> 02:40.410
Plus it's going to be last photo file name okay.

02:40.440 --> 02:42.060
It's already a string.

02:42.060 --> 02:44.160
So I don't need to cast to anything.

02:44.160 --> 02:48.000
So let's check that we don't need to run the first program.

02:48.000 --> 02:49.770
We can just run the second one.

02:49.770 --> 02:54.180
So let's actually once again I saved it as the previous step.

02:54.180 --> 02:57.900
So I'm going to do save as step six.

02:57.900 --> 03:01.170
Then I'm going to fix the step five okay.

03:01.170 --> 03:03.990
And we are going to run that as step six.

03:05.010 --> 03:13.120
So let's stop and then let's run Python three project step six.

03:13.330 --> 03:15.910
All right, let's open that one.

03:17.140 --> 03:19.360
And you see two new photos.

03:19.390 --> 03:19.600
Okay.

03:19.630 --> 03:20.680
So I just started the program.

03:20.680 --> 03:24.070
So of course we have two photos in that log file.

03:24.490 --> 03:26.350
So we see two new photos.

03:26.350 --> 03:30.670
So two photos here we see two new photos since you last checked.

03:30.670 --> 03:33.790
And the last one is this photo.

03:33.820 --> 03:42.520
Now you can note that if I add more stuff to this string, it's just added here without going back to

03:42.550 --> 03:44.290
a new line and how to do that.

03:44.320 --> 03:47.620
Well, here we are actually writing HTML code.

03:47.620 --> 03:54.250
So to do this simply here just for this example you can add this.

03:54.250 --> 03:59.080
So angle bracket and then BR slash angle bracket.

03:59.110 --> 04:01.120
This is going to go to a new line.

04:01.120 --> 04:04.120
So if I save and if I run again.

04:06.760 --> 04:08.710
Ctrl c run again.

04:09.550 --> 04:11.980
Let's go back to the web page.

04:12.880 --> 04:17.440
You can see now the last photo here is on the new line.

04:17.440 --> 04:18.910
So it's going to be more readable.

04:18.910 --> 04:24.430
And now after this I want to also print I want to display the image.

04:24.430 --> 04:26.050
So I'm going to add a.

04:26.080 --> 04:32.650
Also I'm going to do plus and add another bar tag okay.

04:32.650 --> 04:34.180
So I'm going to go back to a new line.

04:34.180 --> 04:39.550
And then I'm going to do message plus equal.

04:39.550 --> 04:44.860
And here is how to include an image with HTML okay.

04:44.860 --> 04:47.470
So you have to do image.

04:47.470 --> 04:49.360
So we're going to create an image tag.

04:49.360 --> 04:50.830
And basically I'm just going to.

04:50.860 --> 04:54.640
So I'm just going to write it here okay in a comment.

04:54.640 --> 04:57.190
So a tag would be something like this image.

04:57.190 --> 05:00.730
And then src for source which is the.

05:01.270 --> 05:05.230
So in quotes which is the path okay.

05:05.260 --> 05:08.230
And then you can close the angle bracket.

05:08.230 --> 05:11.290
So that's what we want to write okay.

05:11.320 --> 05:13.510
So I'm going to do src.

05:13.720 --> 05:17.990
And then the thing is you can see here I want to add a double quote.

05:18.020 --> 05:22.610
But if I add double quotes inside the string I'm actually closing the string.

05:22.610 --> 05:25.730
So how to add a double quote inside the string?

05:25.730 --> 05:29.810
Well, to add a double quote you can add a backslash in front of the double quote.

05:29.840 --> 05:35.630
This is going to tell the Python program that this double quote is not to finish the string, but it's

05:35.660 --> 05:37.850
actually part of the string okay.

05:37.880 --> 05:40.940
That's how you can include this character inside the string.

05:40.940 --> 05:45.500
And now if I add another double quote, this one is actually finishing the string.

05:45.530 --> 05:45.680
Okay.

05:45.710 --> 05:48.980
So that string here is the beginning here.

05:48.980 --> 05:52.550
So it's image and then space SC.

05:53.450 --> 05:58.910
And let's add an equal here and quote.

05:58.940 --> 05:59.270
All right.

05:59.300 --> 06:04.850
So this is what we have in this string with this quote here being the backslash quote.

06:05.330 --> 06:07.490
And this quote terminating the string.

06:07.490 --> 06:14.960
And I finish the string because then I need to do plus last photo file name okay I need to add the path

06:14.960 --> 06:16.820
that we got here from the file.

06:16.820 --> 06:18.260
And I'm going to do plus.

06:18.260 --> 06:27.410
And then I need to finish that string with you see a double quote and an angle bracket.

06:27.410 --> 06:33.860
So once again to add a double quote I need to do backslash double quote and then angle bracket.

06:33.860 --> 06:35.600
So you see in the end it looks like this.

06:35.600 --> 06:39.800
So you have double quote backslash double quotes angle bracket and double quote.

06:39.830 --> 06:43.220
Because those two double quotes here are going to open and close the strings.

06:43.220 --> 06:48.320
And then the backslash double quote is going to be a double quote inside the string plus the angle bracket.

06:48.320 --> 06:50.360
So in the end in this string we only have two characters.

06:50.360 --> 06:54.350
We have these which correspond to that part okay.

06:54.500 --> 06:56.900
So I'm going to now remove this.

06:59.270 --> 07:01.010
And that should be it.

07:01.010 --> 07:02.480
So I'm going to run it.

07:02.510 --> 07:03.440
It's not going to work.

07:03.440 --> 07:06.770
And then I'm going to show you what to do to fix that.

07:06.980 --> 07:08.660
So let's run.

07:10.190 --> 07:15.290
Let's first see if we have any error okay.

07:15.290 --> 07:15.890
So it's working.

07:15.890 --> 07:18.500
As you can see we have this icon here.

07:18.500 --> 07:19.820
We don't have the image.

07:19.820 --> 07:27.720
And this is because while you need to go back to the code and in this app here, when we initialize

07:27.720 --> 07:31.140
the flask app, you need to add two extra arguments.

07:31.140 --> 07:34.740
So this is not really something you can just come up with from nowhere.

07:34.770 --> 07:35.220
Okay.

07:35.250 --> 07:38.400
And I gave this to you also in the PDF.

07:38.400 --> 07:41.220
So this is something you need to add if you want to add photos.

07:41.220 --> 07:45.900
So you need to do static URL path.

07:45.900 --> 07:56.820
And that's going to correspond to where the file is I'm going to create here again a camera folder path

07:56.820 --> 08:04.650
which is slash home slash pi slash photos final project.

08:04.650 --> 08:07.950
And I'm going to give this variable.

08:07.950 --> 08:14.220
So this path to the static URL path argument okay.

08:14.220 --> 08:15.510
And I need another one.

08:15.510 --> 08:19.590
It's going to be called static folder like that.

08:19.590 --> 08:22.800
And I give the same argument okay.

08:22.830 --> 08:25.620
So that's something you have to do and then it's going to work.

08:25.650 --> 08:31.350
Now one thing is because I have created this variable here instead of writing the log file name with

08:31.350 --> 08:40.020
that at the beginning, I could also do camera folder path plus slash photo logs dot txt okay.

08:40.050 --> 08:42.510
So I don't rewrite the same thing twice.

08:42.510 --> 08:45.630
So let's save that and let's go back.

08:45.780 --> 08:48.570
So I need to actually start.

08:48.600 --> 08:49.680
So stop and start again.

08:49.680 --> 08:52.440
If not we are still running the previous version.

08:52.530 --> 08:55.680
And make sure you see that you are running the step six.

08:57.270 --> 08:58.560
Let's go back here.

08:59.760 --> 09:00.180
Great.

09:00.180 --> 09:02.280
And we see here the last photo.

09:02.280 --> 09:07.950
So this photo of my ceiling here, that's the last photo that corresponds to this image file.

09:07.950 --> 09:10.020
So the program is finished.

09:10.050 --> 09:13.410
As a quick recap, as you can see we first import the different modules.

09:13.410 --> 09:16.020
Then we have a few global variables.

09:16.020 --> 09:18.210
We create the flask application.

09:18.210 --> 09:21.270
We create our route and we run flask.

09:21.360 --> 09:26.160
And in these check photos function we check if the log file exists.

09:26.230 --> 09:28.720
If yes, then we're going to open it.

09:28.750 --> 09:30.490
We're going to count how many lines.

09:30.490 --> 09:33.010
We're going to get the last auto file name.

09:33.010 --> 09:36.580
And then we compute the difference of lines between last time.

09:36.580 --> 09:41.260
And we also print the path to the last photo and we display the photo.

09:41.260 --> 09:44.470
And now there's just one thing I want to show you to finish.

09:44.470 --> 09:48.280
So you are not surprised with some result you might get.

09:48.280 --> 09:51.280
So now we have the step six running.

09:51.760 --> 09:57.580
And I'm going to start here on step four again okay.

09:57.580 --> 10:01.180
So that's the one that's going to monitor the movement and take a photo.

10:01.180 --> 10:03.220
And I'm going to take a photo okay.

10:03.250 --> 10:04.570
So we took a photo.

10:04.570 --> 10:07.660
And then I have my step six still running.

10:07.660 --> 10:14.920
So the two programs are running I go back here I refresh and you can see the last photo.

10:14.920 --> 10:19.420
But then you can see the counter says minus one new photos.

10:19.420 --> 10:20.260
Why is that?

10:20.260 --> 10:26.140
Because well, we have still the this program is still running okay.

10:26.140 --> 10:37.660
And we have actually checked the URL before when we had two photos, so we had two lines in this file.

10:38.110 --> 10:39.910
Okay so cancel.

10:39.940 --> 10:45.280
So the variable here um which is previous line counter was two.

10:45.310 --> 10:51.670
And now because we started it again and we went back to zero and then one, then of course if you compute

10:51.670 --> 10:55.030
the difference you're going to do one minus two and you're going to get minus one.

10:55.030 --> 10:59.020
So that's the only kind of thing we could see here that's a bit weird.

10:59.020 --> 11:04.120
So basically what I recommend is just you start the two programs at the same time, okay.

11:04.120 --> 11:09.640
So you start the first program and you start the second program okay.

11:11.350 --> 11:14.710
And so you won't have any issue with the counter.

11:14.710 --> 11:22.090
And now if I run again you can see no photo available because well, the log file has been removed.

11:22.090 --> 11:27.010
But then it's going to well, when you have more photos it's going to correctly count the photos.

11:27.040 --> 11:27.430
All right.

11:27.430 --> 11:29.230
And that's the end of the final project.

11:29.230 --> 11:32.710
But I still have a bonus step for you right in the next video.
