Let's add a new URL to our web server and also connect flask with the Gpio functionality.
So what do we want to do here?
I'm going to come back to this URL.
So so the server is still working.
And let's say that after this.
So this is the homepage okay.
If I put a slash it's going to be the same thing.
Now after this I want to put slash and then push let's say push dash button.
And I want to see a text that tells me if the push button is pressed or not pressed.
So that's what I want.
As you can see now we have not found okay.
So that's the 404.
You might have seen this error previously.
Okay.
On some web pages when you cannot find a page because the page doesn't exist, or if you have provided
the wrong URL, then you have 404 error not found.
So let's implement that route.
I'm going to come back to the application here.
I'm going to I'm going to stop that with Ctrl C.
And let's also put the ports back to 5000.
So to the default one and how to add a new route.
Well, I'm just going to go back to a new line here and I'm going to do app.
So at app dot route and then inside quotes I'm going to do slash.
And this time push dash button just like that.
So important note is when you want to have several words don't add a space okay.
Better to add a dash.
Usually for URLs we use dashes.
Then I create a function.
How do I name the function?
For example push button.
But you see here in Python the convention is to use a underscore.
All right.
So depending on what you do you're going to use different conventions for URLs.
And for Python code is different.
And actually I can name it maybe check push button and then state.
All right I put the parentheses the colon I go back to a new line and let's just well let's just return
okay.
For now.
And that's it.
So let's run the script okay.
let's see what we have.
So now I'm not going to refresh it like this because I need to put the port back to 5000.
But then if I press enter you see we have okay.
All right.
So the push button URL here is found.
Great.
And now we just need to connect the flask with the Gpio.
How to do that.
It's very easy.
We're just going to initialize our push button and use it inside the function.
So let's do another import here from Gpio zero import button.
Then I'm going to initialize the button.
So I could do it before or after the app.
Here I'm going to do it before button is equal to and then button I put the Gpio which is still 26.
And I'm also going to use bounce time with 0.05 okay.
So that's something we have seen already previously in this course.
So the Gpio 26 is initialized as input for the push button.
and now we can read its state.
So what I can do is directly inside this function here.
So I already have access to the button here.
Inside this function I'm going to do if button dot is pressed okay.
So very simple application here.
We keep things minimal okay.
Just to show you the connection between flask and GPUs.
So if the button is pressed I'm going to return a button is pressed.
And then I can do else return button is not pressed.
All right.
So it's quite easy to understand and note here that I can do actually I can simplify this a bit because
when I do return what if I do return whatever is after this line in this function is not going to be
executed because we return from the function.
All right.
So instead of doing if this will return that else we return this other string, I can just remove the
else here and also remove an indentation here.
And it's going to be the same thing because if the button is pressed, we return that and then we don't
execute the next lines in the function.
But if this is not true, we just go to that line.
Okay.
So the result is going to be the same.
It's very small optimization here.
So let's run this.
And actually just before we run I'm going to save it.
So I'm going to save this file.
And just to show you that well the important thing is you could decide okay let's save it as flask.py.
But don't do that because flask is already a Python module.
So if you save a file using a name that's already used by a module, you will have a lot of issues later
on.
Okay, so use something else for example here web server.
Or you could use for example flask app, something like that.
But not just flask web server.py.
Now if you want you could run it here.
I can also run it from the terminal.
So let's do that just to show you that both are going to work.
So let's go to Python programs.
And let's do Python three web server.
And once again, you see that when we run something on Thonny and then we try to run it on the terminal,
you might get an error.
So I'm going to close Sony.
We can open it again actually just not run the program.
And then it should be able to work okay.
You see we have the same warning.
And we also have the URL here.
So let's open the browser and let's refresh this.
You see the button is not pressed.
Now what I'm going to do is I'm going to press on the button and I'm going to refresh the page.
And you can see the button is pressed.
If I refresh again button is pressed if I release button is not pressed.
So it's correctly working.
And you see with this example we have created a web application with two different routes.
And we have also combined the use of flask with the Gpios.