WEBVTT

00:04.910 --> 00:06.430
By everyone and welcome back.

00:06.680 --> 00:13.910
So in the last video we saw how the whole web development cycle happens from starting from when you

00:13.910 --> 00:16.640
type your out into your browser.

00:16.790 --> 00:18.980
All the way until you get your response.

00:18.980 --> 00:24.840
So in this video we're going to be basically understanding what these nine lines of code are doing.

00:24.910 --> 00:29.270
And we're going to add a little bit on it just to make sure that we understand everything.

00:29.350 --> 00:29.710
OK.

00:29.780 --> 00:30.820
Let's get started.

00:30.980 --> 00:34.910
So the first line here is we're basically importing the required libraries.

00:34.940 --> 00:36.830
We're always going to import these.

00:37.000 --> 00:41.930
So flask is the name of the package which is basically in a flask framework.

00:42.080 --> 00:48.740
And then we're importing flask the capital F which is the constructor of the flask application.

00:48.950 --> 00:54.740
And then where we're turning the flask application and calling giving the name.

00:54.770 --> 01:01.220
So you can just call it the name which is usually main is fine you can change the name to anything else

01:01.220 --> 01:02.530
that's fine.

01:02.630 --> 01:10.970
So you could also write here you know flask you know hi or whatever but we just it's a convention that

01:10.970 --> 01:14.050
we do underscore or name a French Quarter.

01:14.810 --> 01:19.860
And then here's the thing we do at app dot route slash.

01:20.350 --> 01:22.420
So what does that really mean.

01:22.430 --> 01:25.220
This means that once the server is up and running.

01:25.220 --> 01:33.300
So once we click once we come here and we write flask run once this server is running an application

01:33.320 --> 01:39.580
apt piecewise running then this application is always listening on slash.

01:39.720 --> 01:40.250
OK.

01:40.400 --> 01:43.750
It's always waiting for someone to ride.

01:44.030 --> 01:51.640
So always waiting for someone to write one to seven point zero point zero point one slot 5000.

01:51.680 --> 01:58.690
So the port that we're running on and then and then the port we're running on and then slash.

01:58.730 --> 02:05.990
So once some once they get a request at this slash right this here is basically what's under the IP.

02:05.990 --> 02:14.990
Once get a request and slash that tells flask a flask make this function handle the request and return

02:14.990 --> 02:15.890
the response.

02:15.950 --> 02:16.770
OK.

02:16.790 --> 02:19.190
So let's take let's find another one.

02:19.190 --> 02:23.670
Let's write an app loops app around.

02:24.290 --> 02:27.940
But then this time we're going to do it to slash.

02:28.130 --> 02:29.070
Hi there.

02:29.420 --> 02:30.220
OK.

02:30.710 --> 02:36.170
And then we're going to define another function called high there doesn't have to be the same name so

02:36.220 --> 02:37.620
I can use it.

02:37.970 --> 02:40.480
Hi there everyone.

02:40.660 --> 02:41.500
Okay.

02:41.960 --> 02:51.010
And then I'm going to return something else so returning I just hit slash either.

02:51.650 --> 02:52.260
OK.

02:53.120 --> 02:56.230
And now that means that flacid going to be a list.

02:56.320 --> 03:00.200
Any request that comes at slash and then it returns.

03:00.210 --> 03:07.170
Hello world or it's going to be listening of any request that comes at Hi there and then once it receives

03:07.170 --> 03:13.410
it it produces the response which is the string over a year and returns it to the browser.

03:13.440 --> 03:15.440
The browser that requested it.

03:15.690 --> 03:19.410
So we can go keep doing this you know we can do and around.

03:19.440 --> 03:28.380
And then another one was the slash by and then I'm going to find another function called and then returned

03:28.380 --> 03:29.030
by.

03:29.120 --> 03:29.590
OK.

03:29.790 --> 03:34.940
I can also add do some extra stuff like for example seek or.

03:35.990 --> 03:40.700
Two times 534.

03:41.190 --> 03:50.250
And then we can say s is equal to string C and then we can also return s if you want or whatever but

03:50.340 --> 03:55.680
I'm just going to leave it to you so you can basically do all the hyphen computation that you feel like

03:56.460 --> 03:58.050
in this area over here.

03:58.140 --> 04:06.890
And then once you're done all the competition so what you're doing here is prepare a response for other

04:06.890 --> 04:11.490
request that came to slash line.

04:11.670 --> 04:18.270
So once you're ready with a response you simply refuse the return function and return the string.

04:18.270 --> 04:21.180
Now a couple things to keep in mind right.

04:21.270 --> 04:27.210
The return function here takes either or it can take a couple things.

04:27.300 --> 04:33.550
But mostly we use either a string and this course we're going to be using a string or a Jason and we're

04:33.550 --> 04:40.110
going to go explain what what Jason's are in the videos or I can take also a page.

04:43.020 --> 04:46.590
So basically what this application is going to do.

04:46.620 --> 04:47.620
Let's first.

04:47.850 --> 04:53.890
And then stop this Control-C run the flask application.

04:53.920 --> 04:55.050
Good browser.

04:58.270 --> 05:05.500
And then let's say let's go to one twos seven point zero and a couple things your host here might be

05:05.500 --> 05:06.900
different than mine.

05:07.180 --> 05:07.900
It's fine.

05:07.900 --> 05:13.540
You should if your host machine is going to already install a ritual machine it might be zero.

05:13.600 --> 05:15.570
Zero zero zero is fine.

05:15.700 --> 05:17.160
Don't worry about it for now.

05:17.160 --> 05:19.980
We're going to fix this problem later.

05:20.050 --> 05:28.120
So I'm going to listen at Port 5000 and then I'm going to go to for example let's say only slash.

05:28.150 --> 05:29.940
We'll see what happens I get.

05:30.010 --> 05:30.990
Hello world.

05:31.210 --> 05:34.940
What if I didn't go to slash either.

05:35.320 --> 05:38.120
Well I just hit slash high there.

05:38.140 --> 05:40.580
So I just reached here.

05:40.780 --> 05:44.510
You can see that I got get at slash Heidi.

05:44.530 --> 05:49.650
So I got to request a time here and then this function is now responsible.

05:49.650 --> 05:55.110
Hi there everyone started handling preparing the response for this request.

05:55.180 --> 06:01.830
What if I go to slash by then I get by again.

06:02.050 --> 06:03.350
I did some computation.

06:03.360 --> 06:04.470
If I want to return.

06:04.520 --> 06:10.300
Competition's fine but I don't have to simply return and the response.

06:10.300 --> 06:12.050
Now what's this here.

06:12.070 --> 06:12.960
What's this.

06:13.090 --> 06:19.690
Here it says basically you should be familiar with this these two lines in Python to make sure that

06:19.690 --> 06:22.200
it's actually running from this directory.

06:22.360 --> 06:28.400
But it's fun usually just do it at it's name equal to mean that and they're run.

06:28.450 --> 06:35.770
Now what these two here over here if we take this over here if we take it out and then we save and then

06:35.770 --> 06:36.610
we run it.

06:36.880 --> 06:40.260
OK flask one is going to work perfectly.

06:40.260 --> 06:42.150
No no problems with the problem.

06:42.160 --> 06:45.730
So if you go here again to lie then it replay.

06:45.840 --> 06:48.230
See your question it's fine.

06:48.420 --> 06:49.900
Glasses buy.

06:49.960 --> 06:55.500
So basically what this does is it tells us where the host is.

06:55.540 --> 06:56.800
Where should we run.

06:57.040 --> 07:03.250
And usually we just leave it empty until we actually deploy so we don't have to worry about this area

07:03.250 --> 07:04.240
over here.

07:04.270 --> 07:06.630
Don't forget about it it's not important.

07:06.820 --> 07:13.990
But one of that import more important things to do is to care about debug or trip over while you're

07:13.990 --> 07:14.820
developing this.

07:14.830 --> 07:20.800
This is a very important thing to do because when you do Deeble equal to true then let's say for example

07:20.800 --> 07:27.870
I did something bad here so I say let's see I divide by zero right.

07:27.880 --> 07:36.830
So C equals to 1 divided by zero rights should give me a arithmetic error right and then return by.

07:37.060 --> 07:44.790
So if I say this and then I restart my server and then I go to slash by over here.

07:46.590 --> 07:52.290
I'm going to I'm going to get an internal air here internal server and it's going to tell me lots of

07:52.290 --> 07:53.220
errors here.

07:53.360 --> 07:56.820
So this is important because you want to know what happened wrong.

07:56.820 --> 08:01.860
It says here Zero a division tells me that can't define 0 0.

08:02.040 --> 08:03.060
So this is important.

08:03.060 --> 08:09.360
And also once we fix this host problem you're also going to notice that the this page is not going to

08:09.360 --> 08:15.030
display internal server error is actually going to display this here over here.

08:15.030 --> 08:21.450
This whole traceback is going to be shown over here but for now since we're running on a virtual machine

08:21.450 --> 08:27.270
things are a bit if you were going to get to that later we're going to fix this problem in the coming

08:27.270 --> 08:27.930
videos.

08:27.930 --> 08:34.530
But for now just know that debugging or true is important because it helps us you know once we get an

08:34.530 --> 08:39.790
error it displays the error here and then we can read and understand what's happening like here.

08:39.830 --> 08:42.070
I know ocean divided by zero.

08:42.090 --> 08:43.020
I come here.

08:43.310 --> 08:45.650
You know I come out a winner.

08:45.770 --> 08:49.190
So yeah hopefully this made sense.

08:49.290 --> 08:53.770
Make sure it's running and then I go again to Ratzinger.

08:53.880 --> 08:58.650
So hopefully this made sense.

08:58.650 --> 09:06.780
One last thing that I would like to talk about is what's the difference between a web application and

09:06.780 --> 09:07.920
a web service.

09:07.920 --> 09:17.070
So in return here we're basically returning usually here in the return in webservices we either return

09:17.230 --> 09:23.130
we we most of the time write web webservices API and mostly we return and Jason we're going to be dealing

09:23.130 --> 09:30.360
with a lot of GSM on the other hand for web applications usually return a page or return for example

09:31.050 --> 09:35.070
indexed each team out or they render something.

09:35.070 --> 09:41.800
So in I believe in flask It's called Return render template.

09:42.030 --> 09:47.900
And then you write index stuff inside him out something along that line.

09:48.090 --> 09:54.080
But basically you're returning page here and that's what a web application is you're returning a page.

09:54.210 --> 09:57.740
But in our case we're building an API or web service.

09:57.840 --> 10:01.590
So I only care about GSM we get Jason from the user.

10:01.590 --> 10:09.750
The user gives us some Jason parse his Jason and then we return him some Jason Gay.

10:10.200 --> 10:11.830
So we're gonna see how.

10:11.970 --> 10:17.430
So in this in this function here I could actually you could have actually sent me some stuff so we're

10:17.430 --> 10:23.490
going to see how we can check that the user sent us some stuff some Jason and then have to parse it.

10:23.490 --> 10:27.400
How to read this Jason and then do some computation here.

10:27.450 --> 10:27.780
Right.

10:27.780 --> 10:31.510
Do some math for example and then prepare it Jason.

10:31.590 --> 10:34.390
So Jason might look something like this.

10:34.620 --> 10:38.550
Turn Janie's son is equal to.

10:38.820 --> 10:47.510
And then for example see Field 1 and then ABC.

10:47.670 --> 10:52.160
And we're going to get we're going to get we're going to discuss Jason's in much more details.

10:52.170 --> 10:57.360
But just so that you understand the difference between a web application and

11:01.180 --> 11:05.990
a web application and web service or or or API.

11:06.410 --> 11:11.380
So yeah basically that's it.

11:11.670 --> 11:18.620
We can also return their return Jason here which might not work in Python 3 because we might need this

11:18.630 --> 11:21.210
function called use on the fly.

11:21.360 --> 11:26.660
But in previous Python versions used to work.

11:26.850 --> 11:33.750
So let's let's just just try to make it work here without the Jason F-I function.

11:33.750 --> 11:37.050
If it's if it has changed then it might give us some error.

11:37.380 --> 11:38.040
Let's see

11:41.290 --> 11:43.400
okay so it returns an error.

11:43.400 --> 11:46.390
So it's called dictionary object is not colorable.

11:46.490 --> 11:49.200
OK so we regard that later on.

11:49.830 --> 11:51.210
It also.

11:51.380 --> 11:53.390
Son if I from here.

11:53.740 --> 11:57.080
And then we can return I.

11:57.340 --> 12:00.320
This is just in the head of it.

12:00.320 --> 12:08.190
So we're going to sound ify this dictionary over here and then we rerun it shouldn't give us and years

12:11.310 --> 12:14.340
OK so here will rotate.

12:14.380 --> 12:14.700
OK.

12:14.710 --> 12:19.170
So we got this Jason back and everything's fine.

12:19.340 --> 12:22.640
Dunk just don't care about this for here Free-Net.

12:22.660 --> 12:28.330
We're going to explain it in much more details but yeah basically think of a web application we're always

12:28.330 --> 12:30.250
returning a page.

12:30.400 --> 12:37.020
But in the case of webservices or API we're returning Jason and we're taking in Jason.

12:37.210 --> 12:42.910
So hopefully this makes sense and we'll continue on in the next video.

12:42.940 --> 12:44.690
So on to the next video because it.
