Hey, welcome back.
We are back to building our rest API with Flask.
So this is what we have so far.
And let me give you a quick overview of what we have done and what this does, because we did some playing
around with pandas, so it's good to give you an overview.
So this is a main IP file and that's what we run to start our web app.
So that's the web page of the app.
And if you click that URL, it takes you to the home page.
So that's the documentation.
But if you visit a URL end point such as this one, this is just an example.
Then we get that because in the URL we entered ten for the station ID and the date and so station ID
and date are passed to.
Station and date respectively.
Then we returned that dictionary.
So that's the dictionary.
For now, the temperature value is 23.
So it is hardcoded in here.
You see, I've just declared the variable and then that variable is displayed next to the temperature
string.
So the temperature string and the value.
Our purpose now is to get this temperature from the see as the data.
So to be able to do that, we need to get the sea as we delta and place them in here.
So I already gave you the data in the previous video.
If you don't have them, please go to the previous video and download the data from there.
So there you'll have a delta zip file and a delta small zip file.
The data the zip file has 6500 text files and the delta small.
That zip file has only 100 text files.
So I do suggest you to get the small zip file that would be more than enough for the purpose of this
project.
So this is the folder I worked on previously.
Here I have the Jupyter documents and the data small folder with these 100 files plus the metadata inside
stations that txt.
So I'm going to copy that into our folder and place it in my route directory here in Port Charm.
So now Delta Small is in the same directory with main dot P.
Why do we want to add this to the git repository?
Yes, we do want to add them.
So press on that and these should be colored in green.
Otherwise you can just see right click here, go to git and go to add if they are not colored in green
in your case.
All right.
So how should we get the data from these files and display them in this page?
What's the logic behind that?
Well, as usual, think about how do we go from the input data to the output data.
So how do we get this data into Python?
First of all, we do get them using pandas.
So in the import statements here, import pandas as speedy and pandas should be installed for this particular
virtual environment.
So make sure to install it.
Now it's installing in my environment.
Then once you have pandas, where do we load a dataframe?
Is it here like PD write C?
Is V like that?
Yeah, but the question is which of the CC files should we, should we read here?
Is it the first one?
The second one, the third one.
Well, we should read anything that the user wants.
Right.
How do the users ask for a particular file.
Well they ask for a particular file through the URL they enter.
So for example, in this case here, the user entered station with ID ten and they ask for the temperature
of that particular date year 1988, 25th of October.
Station eight.
So that number ten then goes in this function here.
So station will be equal to ten.
In this case, I can prove that by printing out the station variable in here.
Save the script and then go to the page.
Reload this.
Oh, we got an error because PANDAS is still being installed.
So let's wait a few seconds.
So the process was interrupted because of the aerosol.
Execute this again.
We get this other error here because I entered this line.
So let me comment that out.
So this is missing the argument.
That's why we got an error.
Let's run again.
I want to make my points here.
So for now, nothing is printed out because this function has not been yet executed.
This function is only executed when the user loads a URL, an end point.
For example, this one in here.
Let's try it again.
So there we get the data.
But what happens in the command line is this.
So temperature station was printed out.
So that means we do have the station ID in this function.
Therefore.
We should read the data in here.
PD That's right.
See, it's V and that would be Delta underscore small slash T, g, And so that path.
Let me copy the accurate path by going to copy path reference and then you want to copy path from content
to route.
So that one is there.
So I will delete that paste this here, put it around quotes.
So that's however for one station.
So that's station one and it's still hardcoded.
How can we inject that station into here?
That's the question.
Well, we should do something like, you know, this should be a separate string and then we should
concatenate that string with a station ID.
Now with to to get those zeros before a number.
Let's say station is ten and then you can use str station dot z fill six.
That should construct a string of six digits.
So one, two, three, four, five, six.
So let's try again.
If station is 99 this time, then this should construct that string.
If station is.
1001.
Then we should get that string.
So that's the format of these file names.
Right.
So let's apply that here.
Plus STR station.
What is it, Phil?
Six.
Plus the text extension as a string.
So all of these are strings, right?
That is a string.
And that field is a method of strings.
So it basically adds zeros to a string.
The string should be representing a number, but it's not a number.
So we are converting that into a string.
So it's actually like the string version of number ten.
So basically we're doing ten.
Does that feel six?
That's what we're doing here, just to be clear about that.
So that will give us the actual file that the user wounds and we read that file with pandas.
And so that's the first argument.
I would prefer, though, to have this argument in a separate variable, and then I can just pass the
variable here file name.
So file name represents the string.
And here that's the first argument of the read CSV method.
Then the second argument remember that we had.
I already did some exploration of the data in Jupiter, so you should open your Jupiter program and
refer to the code we wrote already.
So here you have also the Cyprus Rows method.
So Skip Rose is equal to 20 because each file starts at Route 21.
That's the header.
All these are metadata which should be ignored.
So that's what the skip rows method does.
And pass dates should be equal to a list of columns which should be treated as dates.
We only have one column here, the date column.
So copy that column name, including the spaces which is four spaces and place it inside quotes like
that.
One, two, three, four leading space and date in capital letters.
Then we should extract the temperature out of that data frame for a particular date.
And again.
You should be referring to the Jupyter notebook.
And I did teach you how to get certain cells in the get certain cells section here.
So that's the code, basically.
So you might want to copy that cell.
And then paste it in here.
So that should be the temperature.
But of course we need to modify it slightly here.
So the date in here is whatever the user enters and that date is stored in the date variable as a string.
So when the user enters the date, it's in this format, the year, the month and the day of the month.
But the dates of the date column are in this format with these dashes.
So we have two options here.
Either we get the user date and we add dashes between the digits to make it comply with the date column,
or we force the user to enter dates with dashes in the URL.
So what that means, we would need to modify the home page a little bit.
So if you go to template home that HTML, you need to tell the users to enter a date in this format.
So give the example like that.
So I would prefer to have this option and then in here.
We just compare the date that the user enters, which is supposed to be with those dashes now.
So we check for values of that date in the tJie column.
We squeeze it and divided by ten, and that should be the temperature.
So now we can delete the hardcoded temperature and yeah, let's give this a try.
So let's run Main.
And go here.
Let's try viewing it now with dashes.
Don't forget, you see, the date is with dashes placed there.
And there we go.
So for station ten, the temperature was -0.9.
For this date in October, temperature was about minus one degree Celsius, which is below the water
freezing point.
So quite cold.
And let's try another date like 1999.
So that is 9.5.
Ten years later, temperature was quite higher.
Let's try another station like 19.
Then we get an error.
So t id 0000 19.
The text is not in here.
Is that the case?
Yes.
So some sections are missing.
And to address that issue, we're going to improve the home page of our API.
So here next, we're going to display the list of available stations with IDs along with city names
so that the user knows so what they can query through the URL and points.
So if they want Rome, for example, they can see the station ID for Rome and they can query that particular
city.
So that's something that we will do next.
For now, that was the code.
So let's commit the changes and.
Yeah.
Commit.
That was day three.
Return temperature for any station and date.
So we're committing home that email main that p y and all the data.
On version files Here are above the HTML and tutorial that HTML.
These are files not related to our project.
If you remember, I worked on those files when I was teaching you flask.
So commit the changes and that's it for.
This video.
Thanks a lot.