By everyone and welcome back.
So in the last video we saw how the whole web development cycle happens from starting from when you
type your out into your browser.
All the way until you get your response.
So in this video we're going to be basically understanding what these nine lines of code are doing.
And we're going to add a little bit on it just to make sure that we understand everything.
OK.
Let's get started.
So the first line here is we're basically importing the required libraries.
We're always going to import these.
So flask is the name of the package which is basically in a flask framework.
And then we're importing flask the capital F which is the constructor of the flask application.
And then where we're turning the flask application and calling giving the name.
So you can just call it the name which is usually main is fine you can change the name to anything else
that's fine.
So you could also write here you know flask you know hi or whatever but we just it's a convention that
we do underscore or name a French Quarter.
And then here's the thing we do at app dot route slash.
So what does that really mean.
This means that once the server is up and running.
So once we click once we come here and we write flask run once this server is running an application
apt piecewise running then this application is always listening on slash.
OK.
It's always waiting for someone to ride.
So always waiting for someone to write one to seven point zero point zero point one slot 5000.
So the port that we're running on and then and then the port we're running on and then slash.
So once some once they get a request at this slash right this here is basically what's under the IP.
Once get a request and slash that tells flask a flask make this function handle the request and return
the response.
OK.
So let's take let's find another one.
Let's write an app loops app around.
But then this time we're going to do it to slash.
Hi there.
OK.
And then we're going to define another function called high there doesn't have to be the same name so
I can use it.
Hi there everyone.
Okay.
And then I'm going to return something else so returning I just hit slash either.
OK.
And now that means that flacid going to be a list.
Any request that comes at slash and then it returns.
Hello world or it's going to be listening of any request that comes at Hi there and then once it receives
it it produces the response which is the string over a year and returns it to the browser.
The browser that requested it.
So we can go keep doing this you know we can do and around.
And then another one was the slash by and then I'm going to find another function called and then returned
by.
OK.
I can also add do some extra stuff like for example seek or.
Two times 534.
And then we can say s is equal to string C and then we can also return s if you want or whatever but
I'm just going to leave it to you so you can basically do all the hyphen computation that you feel like
in this area over here.
And then once you're done all the competition so what you're doing here is prepare a response for other
request that came to slash line.
So once you're ready with a response you simply refuse the return function and return the string.
Now a couple things to keep in mind right.
The return function here takes either or it can take a couple things.
But mostly we use either a string and this course we're going to be using a string or a Jason and we're
going to go explain what what Jason's are in the videos or I can take also a page.
So basically what this application is going to do.
Let's first.
And then stop this Control-C run the flask application.
Good browser.
And then let's say let's go to one twos seven point zero and a couple things your host here might be
different than mine.
It's fine.
You should if your host machine is going to already install a ritual machine it might be zero.
Zero zero zero is fine.
Don't worry about it for now.
We're going to fix this problem later.
So I'm going to listen at Port 5000 and then I'm going to go to for example let's say only slash.
We'll see what happens I get.
Hello world.
What if I didn't go to slash either.
Well I just hit slash high there.
So I just reached here.
You can see that I got get at slash Heidi.
So I got to request a time here and then this function is now responsible.
Hi there everyone started handling preparing the response for this request.
What if I go to slash by then I get by again.
I did some computation.
If I want to return.
Competition's fine but I don't have to simply return and the response.
Now what's this here.
What's this.
Here it says basically you should be familiar with this these two lines in Python to make sure that
it's actually running from this directory.
But it's fun usually just do it at it's name equal to mean that and they're run.
Now what these two here over here if we take this over here if we take it out and then we save and then
we run it.
OK flask one is going to work perfectly.
No no problems with the problem.
So if you go here again to lie then it replay.
See your question it's fine.
Glasses buy.
So basically what this does is it tells us where the host is.
Where should we run.
And usually we just leave it empty until we actually deploy so we don't have to worry about this area
over here.
Don't forget about it it's not important.
But one of that import more important things to do is to care about debug or trip over while you're
developing this.
This is a very important thing to do because when you do Deeble equal to true then let's say for example
I did something bad here so I say let's see I divide by zero right.
So C equals to 1 divided by zero rights should give me a arithmetic error right and then return by.
So if I say this and then I restart my server and then I go to slash by over here.
I'm going to I'm going to get an internal air here internal server and it's going to tell me lots of
errors here.
So this is important because you want to know what happened wrong.
It says here Zero a division tells me that can't define 0 0.
So this is important.
And also once we fix this host problem you're also going to notice that the this page is not going to
display internal server error is actually going to display this here over here.
This whole traceback is going to be shown over here but for now since we're running on a virtual machine
things are a bit if you were going to get to that later we're going to fix this problem in the coming
videos.
But for now just know that debugging or true is important because it helps us you know once we get an
error it displays the error here and then we can read and understand what's happening like here.
I know ocean divided by zero.
I come here.
You know I come out a winner.
So yeah hopefully this made sense.
Make sure it's running and then I go again to Ratzinger.
So hopefully this made sense.
One last thing that I would like to talk about is what's the difference between a web application and
a web service.
So in return here we're basically returning usually here in the return in webservices we either return
we we most of the time write web webservices API and mostly we return and Jason we're going to be dealing
with a lot of GSM on the other hand for web applications usually return a page or return for example
indexed each team out or they render something.
So in I believe in flask It's called Return render template.
And then you write index stuff inside him out something along that line.
But basically you're returning page here and that's what a web application is you're returning a page.
But in our case we're building an API or web service.
So I only care about GSM we get Jason from the user.
The user gives us some Jason parse his Jason and then we return him some Jason Gay.
So we're gonna see how.
So in this in this function here I could actually you could have actually sent me some stuff so we're
going to see how we can check that the user sent us some stuff some Jason and then have to parse it.
How to read this Jason and then do some computation here.
Right.
Do some math for example and then prepare it Jason.
So Jason might look something like this.
Turn Janie's son is equal to.
And then for example see Field 1 and then ABC.
And we're going to get we're going to get we're going to discuss Jason's in much more details.
But just so that you understand the difference between a web application and
a web application and web service or or or API.
So yeah basically that's it.
We can also return their return Jason here which might not work in Python 3 because we might need this
function called use on the fly.
But in previous Python versions used to work.
So let's let's just just try to make it work here without the Jason F-I function.
If it's if it has changed then it might give us some error.
Let's see
okay so it returns an error.
So it's called dictionary object is not colorable.
OK so we regard that later on.
It also.
Son if I from here.
And then we can return I.
This is just in the head of it.
So we're going to sound ify this dictionary over here and then we rerun it shouldn't give us and years
OK so here will rotate.
OK.
So we got this Jason back and everything's fine.
Dunk just don't care about this for here Free-Net.
We're going to explain it in much more details but yeah basically think of a web application we're always
returning a page.
But in the case of webservices or API we're returning Jason and we're taking in Jason.
So hopefully this makes sense and we'll continue on in the next video.
So on to the next video because it.