This is now step five for this final project.
And in this step we're going to create a new Python program.
Okay.
So we finished the first program here with step number four.
And we're going to create a new one here where we're going to add a web server with flask.
And what we want to be able to do is to show a web page and say how many photos were taken since the
last time that you checked the web page, and then show the last photo.
So in this step five, we're going to initialize the web page and say how many new photos were taken
since you last checked.
And displaying the photo is going to be the following step.
All right.
So let's get started with this new file.
I'm gonna close that one and I'm going to save this one as as project step five dot pi.
So to create a web server let's use flask.
And let's first do some initialization.
So from flask Import flask.
Then we're going to create an app here is equal to flask uppercase with underscore underscore name and
then underscore underscore.
Then we're going to create the different routes.
And at the end we do app dot run with host is equal to 0.0.0.0.
All right.
So that's the structure of our program.
Then we want a homepage.
So we want a simple default route.
That's just going to say hello.
So we do app dot route with a slash and we do a div index okay.
We're not going to do anything on this one.
Just return hello.
And that's it for that okay.
So I'm gonna save that and I can start to run it.
So I'm gonna keep this one here, this terminal to run the first program.
And I'm going to open a new terminal.
Let's go to documents Python programs, and let's do Python three project.
Step five.
Okay.
It's creating a web server.
I do ctrl click on that one.
All right.
And we get to this web page you see with the IP address the port.
And we see hello.
So now I want to have this one slash check photos.
Okay.
So for now we have a 404 not found error.
So we're going to add a new route.
Let's go back to Thonny.
And let's do app dot route slash check photos.
I'm going to create a function here.
Check photos like this.
Okay so once again for URL you use dashes.
It's better for Python functions, better to use underscores.
And what do I do in this function?
Well I will check the log file.
Okay.
Because that's where I'm going to get the information.
So here is all the photos I have.
And I have my photo logs dot txt okay.
And in this you see I have two photos for now.
Okay.
That was the previous run of the program.
So I'm going to check first if this file exists okay.
If it doesn't exist then I'm going to say for example that no photo is available.
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
say how many photos we have, and maybe also even compute the difference between the last time that
we checked.
Okay, so let's do that step by step.
First let's add a variable here.
So right here log file name.
And I use it also as uppercase, so that we don't try to modify it later.
And this one is going to be the same we have created in the previous program because, well, we need
to read from the same file.
So slash home slash pi slash photos final project and then slash photo blogs dot txt.
Now the first thing I will do is check if os dot path dot exists with the log file name.
So if it exists what we are going to do I'm going to put a comment for now is to count lines.
If it doesn't exist, it means we don't have any photos.
So I'm going to do return no photo available.
All right.
So we already have a case here where we're going to see no photo available.
Now to count the lines.
Well I first need to open the file.
So let's do with open log file name with the reading permission.
And then as f for the file.
And there are probably different ways to do that.
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.
That's also going to be useful for the next step.
And then every time that I have a new line, I just increment a counter.
So I will create a counter here.
For example, I can do this at the beginning of the function line counter is equal to zero.
So for every new line I do line counter plus equal one okay.
So that's very basic.
It's very basic but it works okay.
So let's keep things simple for now.
And then what do I want to print.
So I don't want to just print how many lines we have in this file.
I want to print how many new lines we have in this file since the last time that we checked.
So since the last time that we requested this route.
And so how to do this, we will need to keep somewhere the previous line counter.
And because every time when we get this route we're going to enter the function again.
But all the local variables here are going to be lost.
So we need a global variable here I'm going to name it previous line counter.
And let's initialize it to zero.
And because I'm going to use it inside this function and I'm probably going to modify it I do global
previous line counter.
And what's going to be interesting for me is actually the difference between the current line counter
and the previous one.
So after this for loop.
So here you can see the indentation is very important okay.
After the for loop here actually we could even go back here because we will just open the file, count
the lines, and then close the file.
So right here in this indentation but still inside the if I will do difference is equal to previous
line.
Counter minus line counter which is the current one.
And then I also need to do previous line counter is equal to line counter.
Why?
Because while the line counter that we read now, the next time that we're going to enter this function
is going to be the previous one.
And so to be able to keep this value we need to store it okay.
So every time we compare with the previous one and then we save the current one as the previous one
for the next run of that function.
Great.
And now what we can do is we can return a string that says so the difference, for example str difference.
Plus new with space photos since you last checked.
All right so let me recap here on what we did.
So first we initialized flask.
Well nothing really new.
We check if this file exists.
If no we say no photo available.
And if yes we are going to count the lines.
So let's say that we have two lines in our file.
Well, and here actually it should be the opposite.
And you can see it's important to review the code because you can easily check sometimes if something
is not logic.
So instead of doing previous minus current we're going to do the current minus previous line.
Count.
So let's say we have two new lines.
We do two -0 because it's zero the first time.
So we have the difference of two lines.
And we can say we have two new photos.
Since you last checked we also record the previous as the current.
So now previous line counter is going to be two.
And now let's say that you check again and you have three lines inside the file.
So then you will do.
Difference is equal to three minus the previous and the previous is two.
So this time the difference is only going to be one.
Okay.
So let's save that.
And let's see.
Well let's see what we have in this folder for now.
But we are not running the script.
I'm gonna just open that file.
So we have two photos okay.
So if I run the project step five again and I go back here, you see that this should work.
And actually it's working.
So we find the root.
But we have an internal server error which means it's probably a Python error.
And we can see the Python error here.
And yes name OS is not defined because I use C, I use OS but I don't import OS.
So import OS.
Let's save.
Let's run again.
Okay.
Let's go back here.
Let's check again.
And you see we have two new photos since you last checked because we have two photos where we have two
lines inside this photo logs.
But now note that if I refresh again I have zero new photos.
And that's normal because we have checked twice.
And between the two times we didn't take any new photos.
So we have zero new photos.
All right.
And that's the end of this step number five, which we're going to use directly for the next one.