Let's create a first web server with flask.
We will do a very basic and minimalistic application to get started.
So first of all, flask is a Python module that you can import in your code, just like we imported
any other Python module.
And good news, it's already installed for you on the Raspberry Pi OS, so you don't need to do anything,
you can just import it.
And what we're going to import is not the full module but one thing inside.
So I'm going to do from flask like this all lowercase import and then flask with an f uppercase okay.
So that's what we need to import.
Let's just run that to see if it works.
And you see that we don't have any error.
So it means that it was correctly imported.
Great.
Then we will need to create.
So we're going to create an app.
So flask app and let's just name it app.
That's going to be the web application.
You do app is equal to.
And then flask with open and close parentheses.
And inside this we will need to write exactly this.
So underscore underscore name and then underscore underscore.
So you have two underscores here on each side okay.
So there is not much to understand here.
It's just something that you have to write to start an application with flask.
So we have our app variable.
Then what we will need to do is to create some routes.
And I'm going to come back to routes in just one minute.
So when you have a web server you will have URLs.
You will have routes to different functionalities.
So for example if you go to a website so example.com slash something okay the slash something is going
to be a route.
And for each route we're going to create a new function.
All right.
Let's skip that for in just one minute.
Now I'm going to do app dot run okay.
So you create the app.
Then you're going to create some routes.
And then that's going to be the last thing you do is app dot Run.
And inside the parentheses you're going to put host is equal to 0.0.0.0.
So you just write this and then you close the parentheses.
All right.
So this is going to run the application.
And it's going to pause the program here.
And when you press Ctrl C it's going to exit.
Let's just try this okay.
And you see well you might think that we have an error here.
So you can run it here from Tony and from the terminal.
It's going to be the same.
And we have a warning here in red in in Tony.
So you see the app is kind of running and you have a warning because this is a development server.
ET cetera.
But this is fine okay.
We are just using flask for small applications and for tests.
So it's not really a production environment okay.
So we can just ignore this warning.
And then I make sure that I select the shell and I press control C and you see we exit.
Great.
So the app is working.
Now let's add a route.
So to add a route you will need to add this specific syntax with at app dot route open close parentheses.
And inside we're going to write for example slash okay.
So the slash is going to be the home page kind of the homepage.
If you go to example.com slash well you don't add any slash.
It's just going to be the home page.
And after we have this we need to create a function I'm going to name it index.
So usually the function for the home page is going to be named index.
You can name it as you want okay.
It doesn't really matter.
And what do I need to do from this function.
Well I can do whatever I want and then I need to return a string.
The string is going to be what is going to be displayed on this route here on the home page.
And well, you can return anything.
You just need to return a string.
So it can be a very simple string.
For example, hello from flask like that.
That's valid.
But then also, of course, you can create a complete HTML page and just return the HTML as a string.
But for now, let's keep things very simple.
So that's going to be enough for our first application.
Let's run that.
And well we still have all of those logs.
And you can see that we have here.
You can click on for example the first URL here we click here that's going to open a web browser.
And as you can see so in a web browser we have hello from flask.
So that's correctly working.
We can get the result from the web server.
Now I'm going to give you a few more details so you understand a bit more about all that.
So first you can see that when we have a request.
So basically when we open a page it's going to make a request to the server here.
And the server is going to return whatever for example in this function.
So here it's going to return some HTML or some text.
If I refresh you can look at the bottom here if I refresh, you see we have a get and then 200.
So this is going to get some HTML here which is just a string.
And 200 is the code that basically means success.
So if you see 200 it means you could correctly get the result from the server.
Right.
So every time I refresh I do a new request and I get the string.
Now as you can see here I have.
So that's an IP address.
And then I have a colon.
And then I have another number.
This number is the port.
So you have the combination of IP address and port.
And the port is 5000 okay.
That's the default port for flask.
But let's say I write something else for example 5001.
You see that we cannot reach the web server because this doesn't exist.
If I go back to 5000 it's working okay.
And what is this here?
127 .0.0.1 this.
You will get it every time.
So this is an IP address that's just corresponds to this machine.
So in any basically any device, if you write this IP address that's going to correspond to this machine,
it's also called local host.
And actually you can just write instead of this.
You could write local host and press enter.
And you see it's also working.
All right.
So the 127 .0.0.1 and localhost basically can be useful if you want to test a URL directly from the
same device.
So if you run the server on the Raspberry Pi, then you have a web browser on the Raspberry Pi and you
can just type this address.
Okay.
But now if I come back to the logs, you see I have another address so I can run that too.
And it's also working.
And this you can see this is the IP address of the Raspberry Pi.
Actually I can find that with the terminal as well.
If I do hostname dash I upper case.
You see, that's my IP address.
All right, so if you type this IP address in a web browser and then call on with the port which is
5000, then you will be able to get hello from flask.
And this not just from the Raspberry Pi, but from any device that's connected on the same network.
So here I'm using VNC.
I could disconnect from VNC and just open a web browser on my windows, and I will be able to get access
directly to the web server from the Raspberry Pi, or also from the smartphone here, because I'm using
the smartphone as a hotspot.
So the smartphone is on the same network.
I can just open my smartphone, open a web browser and type this exactly, and I will also get the same
results.
So if you have another device connected to the Raspberry Pi, I encourage you to test that, to see
that you can access the URL from outside of the Raspberry Pi.
Okay.
And that's going to be quite powerful because then you can run the server on the Raspberry Pi, and
you can access to the functionalities from another device on the same network, not from the Raspberry
Pi.
Okay, and the last thing here this I'm going to come back to the ports.
This is 5000.
If you want to change it you can here in App.run you have host and then you can do port is equal to.
And let's say for example 8500.
So you can put any value you want but it needs to be minimum 1024 because all the previous values are
reserved port okay.
So 1500 for example is a valid one.
I'm going to control C here.
I'm going to run again.
And you see we have the new URL here with the port.
But if I go back here you see that with 5000 it's not working I have to put 8500 okay.
And now it's working great.
So you see, with just a few lines, you have created your first web server on the Raspberry Pi.