This is now the step six, and that's going to be the end of the Python code for the final project.
Then we have a bonus step.
But here we're going to finish the Python program.
So what we had in the step five is we have a web server.
And you can see we can have this page with check photos.
And we can see how many new photos were taken since we last checked this URL.
Now what I want to do is just add the file so the file name for the photo and print that photo.
Let's go back here.
I'm not going to change the logic here.
That's going to be the same.
I'm just going to add new stuff okay.
So I'm going to add new stuff to that one.
And to add new stuff to this.
Well, instead of just returning the string, what I could do first is to say I'm going to do like this.
I'm going to save that in a variable message is equal to okay.
And then I can do stuff like message plus equal and add more strings.
And then I can do Return message.
All right.
That's going to be better if we scale the application a bit.
So what I want to say here is last photo.
And then I want to add the last photo file name.
How do I get the last photo file name.
Well given the code structure we have here already, what I could do is just create a variable here.
I'm going to name it last photo file name.
It's going to be an empty string.
And then when I go through each line I increment the counter and I do last photo file name is equal
to the line that we are currently reading, and maybe dot strip to remove any unwanted spaces at the
end.
And why do I do this?
So every time we have a new line, the last photo file name is going to be the new line.
But then the for loop is going to end.
So we're going to end on the last element.
So simply put, after this for loop, what's going to be inside this variable is the last element of
the file.
So basically the last line of the file which corresponds also to the last photo.
All right.
So that's a quick way to just get the last photo provided this code structure.
And you see I create the variable here.
So same indentation as here because I'm going to use it there.
Okay.
So for the scope it's important.
So now I have my last photo.
Plus it's going to be last photo file name okay.
It's already a string.
So I don't need to cast to anything.
So let's check that we don't need to run the first program.
We can just run the second one.
So let's actually once again I saved it as the previous step.
So I'm going to do save as step six.
Then I'm going to fix the step five okay.
And we are going to run that as step six.
So let's stop and then let's run Python three project step six.
All right, let's open that one.
And you see two new photos.
Okay.
So I just started the program.
So of course we have two photos in that log file.
So we see two new photos.
So two photos here we see two new photos since you last checked.
And the last one is this photo.
Now you can note that if I add more stuff to this string, it's just added here without going back to
a new line and how to do that.
Well, here we are actually writing HTML code.
So to do this simply here just for this example you can add this.
So angle bracket and then BR slash angle bracket.
This is going to go to a new line.
So if I save and if I run again.
Ctrl c run again.
Let's go back to the web page.
You can see now the last photo here is on the new line.
So it's going to be more readable.
And now after this I want to also print I want to display the image.
So I'm going to add a.
Also I'm going to do plus and add another bar tag okay.
So I'm going to go back to a new line.
And then I'm going to do message plus equal.
And here is how to include an image with HTML okay.
So you have to do image.
So we're going to create an image tag.
And basically I'm just going to.
So I'm just going to write it here okay in a comment.
So a tag would be something like this image.
And then src for source which is the.
So in quotes which is the path okay.
And then you can close the angle bracket.
So that's what we want to write okay.
So I'm going to do src.
And then the thing is you can see here I want to add a double quote.
But if I add double quotes inside the string I'm actually closing the string.
So how to add a double quote inside the string?
Well, to add a double quote you can add a backslash in front of the double quote.
This is going to tell the Python program that this double quote is not to finish the string, but it's
actually part of the string okay.
That's how you can include this character inside the string.
And now if I add another double quote, this one is actually finishing the string.
Okay.
So that string here is the beginning here.
So it's image and then space SC.
And let's add an equal here and quote.
All right.
So this is what we have in this string with this quote here being the backslash quote.
And this quote terminating the string.
And I finish the string because then I need to do plus last photo file name okay I need to add the path
that we got here from the file.
And I'm going to do plus.
And then I need to finish that string with you see a double quote and an angle bracket.
So once again to add a double quote I need to do backslash double quote and then angle bracket.
So you see in the end it looks like this.
So you have double quote backslash double quotes angle bracket and double quote.
Because those two double quotes here are going to open and close the strings.
And then the backslash double quote is going to be a double quote inside the string plus the angle bracket.
So in the end in this string we only have two characters.
We have these which correspond to that part okay.
So I'm going to now remove this.
And that should be it.
So I'm going to run it.
It's not going to work.
And then I'm going to show you what to do to fix that.
So let's run.
Let's first see if we have any error okay.
So it's working.
As you can see we have this icon here.
We don't have the image.
And this is because while you need to go back to the code and in this app here, when we initialize
the flask app, you need to add two extra arguments.
So this is not really something you can just come up with from nowhere.
Okay.
And I gave this to you also in the PDF.
So this is something you need to add if you want to add photos.
So you need to do static URL path.
And that's going to correspond to where the file is I'm going to create here again a camera folder path
which is slash home slash pi slash photos final project.
And I'm going to give this variable.
So this path to the static URL path argument okay.
And I need another one.
It's going to be called static folder like that.
And I give the same argument okay.
So that's something you have to do and then it's going to work.
Now one thing is because I have created this variable here instead of writing the log file name with
that at the beginning, I could also do camera folder path plus slash photo logs dot txt okay.
So I don't rewrite the same thing twice.
So let's save that and let's go back.
So I need to actually start.
So stop and start again.
If not we are still running the previous version.
And make sure you see that you are running the step six.
Let's go back here.
Great.
And we see here the last photo.
So this photo of my ceiling here, that's the last photo that corresponds to this image file.
So the program is finished.
As a quick recap, as you can see we first import the different modules.
Then we have a few global variables.
We create the flask application.
We create our route and we run flask.
And in these check photos function we check if the log file exists.
If yes, then we're going to open it.
We're going to count how many lines.
We're going to get the last auto file name.
And then we compute the difference of lines between last time.
And we also print the path to the last photo and we display the photo.
And now there's just one thing I want to show you to finish.
So you are not surprised with some result you might get.
So now we have the step six running.
And I'm going to start here on step four again okay.
So that's the one that's going to monitor the movement and take a photo.
And I'm going to take a photo okay.
So we took a photo.
And then I have my step six still running.
So the two programs are running I go back here I refresh and you can see the last photo.
But then you can see the counter says minus one new photos.
Why is that?
Because well, we have still the this program is still running okay.
And we have actually checked the URL before when we had two photos, so we had two lines in this file.
Okay so cancel.
So the variable here um which is previous line counter was two.
And now because we started it again and we went back to zero and then one, then of course if you compute
the difference you're going to do one minus two and you're going to get minus one.
So that's the only kind of thing we could see here that's a bit weird.
So basically what I recommend is just you start the two programs at the same time, okay.
So you start the first program and you start the second program okay.
And so you won't have any issue with the counter.
And now if I run again you can see no photo available because well, the log file has been removed.
But then it's going to well, when you have more photos it's going to correctly count the photos.
All right.
And that's the end of the final project.
But I still have a bonus step for you right in the next video.