Hey, welcome back.
In this video, we will construct the API documentation page and the API page itself where the users
can get JSON data.
Let's begin.
So far we worked on some examples which were not related to our app, so I just wanted to introduce
the flask.
Now we have two pages here.
One points to our domain slash home page and the other points to our domain slash about page.
So what I want to do now is first I want to move these HTML pages.
Out of the templates folder because in templates now I'll insert.
Another home that HTML page so tutorial and about where not related to our project.
So I remove them from templates.
Now in home that is HTML.
I'm going to write some documentation about the API and in main that API.
In here I'm removing that home name so that when the users visit the domain home page without entering
any subdirectory, they can go to this home dot HTML page.
So let's try that.
Let's put some Hello string here and go to Main and this should go and be changed to home dot html.
So it points to that home that email file and run main dot pie.
So if you click over the domain, that should take you to the home page.
So we see the string.
Hello.
So home that HTML has been displayed here on the home page.
Then in the home page we want to put some basic information such as an H one tag.
With a Delta API.
So this should be something about our API.
If I refresh the page should display in here.
So let me put this on the right.
This on the left.
So we see what's going on.
And the titles should also be probably the same, whether there's an API.
So this is the title of this particular web page.
Next, we should think about what URL format are we going to have.
So in this paragraph here.
We can tell the users, the visitors, the type of URL they can enter.
Later on, when we deploy this app on a host on a server.
We're going to get a domain such as example dot com or my python API dot com.
But for now, our domain name is this locally, so we want to tell the user something like I'm copying
this HTP URL.
So we want to tell the user that the URL format will be something like this slash API v one.
So you can write whatever you like here, right?
You can write API or APIs, whatever you like.
So but it makes sense to to write something meaningful.
And so after the Slash, I'm writing V one to basically tell users that this is version one.
Of course, you don't have to write this.
You can live like that.
You can also leave it like that and then continue here with the parameters such as station and date.
But this is a good idea to have API v one and then.
You write the parameters, station and date.
Now, this part, the station and the date will be dynamically entered by the user.
So if you still don't understand what I'm doing, let me write a new paragraph.
Your example.
So here you'll understand what I'm talking about.
So basically, that's a format, that's a formula.
So to say, and this would be an example, let's say station with the code ten and date 1988, 10/25.
So 25th of October 1988.
We are asking for data for this station.
So let me refresh the page here.
Yeah, that's what the user is going to see.
Then they can try this URL, for example, put it on the address bar, and that URL should give data
for this particular station.
Now our main that per script should be able to handle this kind of URL.
So currently this script is only handling this URL.
So the home page and the about page.
Now we don't need an about page, so let's change this about page to API v one.
So api v one.
So when the user visits api v one slash station with this brackets.
So less than greater than this is a special syntax of the URL pattern you place in flask.
And the same goes for date like that.
So this to denote that the users can enter values for these two.
So any value that the user enters, let's suppose the user in the URL there enters for example, this
like place, it's here.
So they place ten for station.
Now that ten that value ten is going to be stored in the station argument here of this function station
and date, right?
So date will go here and station will go in here and then what we will do here, but we will do that
tomorrow.
But I can give you some clue now to help you understand what's going on.
So in here, then we will do something like the F is equal to pandas, Dot reads CSV and we're going
to read our data which contain all the historical weather data, and we're going to get this data and
then we're going to get the temperature for a particular station, for that station, let's say station
like that for a particular data, I'm going to show you the exact syntax, but for now, I'm just trying
for you to understand what's going on.
So we're going to get a temperature and then we're going to return the temperature here on the website
or the web page when the user clicks that URL.
So basically, let's suppose for now that we were able to extract the temperature of 23.
So we are just supposing the temperature is 23, I'm hard coding this value.
Of course we will get it from the actual data, but for now let's keep things simple.
So once we have the temperature, then we can return the temperature directly like that.
Let's give it a try.
Save the main that spy file.
Go here, enter that URL.
If you get this error, it says that the return type must be a string dict list with headers etc. but
we returns an integer here so you can simply convert this into a string, refresh the page and that's
what we get.
23.
So when the users ask for a particular station and a particular date, we will calculate will find out
what the temperature was at that particular date for that station and return it here.
But a better idea would be to construct a dictionary here so something like station and tell the user
what station they chose and then date.
So this is good to have.
It's not necessary, but it's good to give the users a complete dictionary, also telling them what
they chose.
So they have a complete view of the data and the date as well.
So the station is this variable date is that variable.
And finally, temperature, which is equal to the temperature value.
So these are strings, right?
These are variables.
Don't.
Infuse them and refresh.
Save first refresh.
And now we finally get some JSON data here.
So that's the date and that's the actual date that the user entered in the URL.
So if I change this to 26 reloads, we get 26.
If I change the station to 20, we get 20 for the station.
Of course, the temperature will also be dynamically calculated for every load of the page.
But for now it's just static.
So now we have this delta, and as you saw in the previous app where we access the new API data, you
can build any app you can imagine to extract these data and then maybe show those data on a website
or send them by email, construct some graphs with the temperature values of different days.
You can do anything.
So that was about the tutorials for today.
We focused on FLASK.
Now, just before I close this video, I want to do a last change here for today in the app.
So it's a good practice to not use websites here as a name for the Flask app, but to use name name
is a special variable.
I want to demonstrate you what exactly name is.
I'll create a file.
A spy.
You don't have to create these files, so I'll delete them.
So if I print name here and I execute this ADP file, I'm going to get main as output.
So this variable contains this string.
Underscore, underscore, main, underscore, underscore.
Now, if I create a second.
Be that p file.
And here I import a and then I execute b that p.
We're going to get a output.
So what happens is that when I execute B inside B, we are importing A, But when A is imported, A
is executed, that means print name is executed.
So in this case, name had the value of a.
So in other words, when this a script is imported from some other script, the value of the name variable
will have the value of the name of the script.
But when the script is executed directly.
The value of name will be main.
We can take advantage of this fact in here and say that if.
Name is equal to Maine.
Then we want to run the Flask app.
In other words, we only want to run this flask app when this main Looper script is executed directly.
But if we are importing this script from some other scripts, let's say we want to import Main from
this script.
In that case, our purpose perhaps is to use some functions of the main script, which we may define
later on.
So our purpose is not to run this app, right?
If we import that app from somewhere, our purpose is just to use some elements of that app.
So that's why we have this condition here.
So we only run the websites when this script is executed directly and not when the script is imported
from somewhere else.
That's the entire purpose of this expression and that's name variable.
So this is a common practice.
And see how that sits.
Let's do some reformatting.
So this needs to break lines here to be more readable according to the coding styles.
And that's it.
I'll delete the two A and B files.
Then I will enable version control.
VCs enable version control so that we can commit our first changes.
So let's create a dot get ignore file.
Add it to the repository then and that idea will be excluded.
Then we want to add to the repository, the static folder, the templates folder get ignored and main
that py about and tutorial that HTML were files where we did some trials and simple examples so they
are not related to the project.
Therefore I will not add them to the tracking.
So with that right click go to get an add.
So all these files are in green now and they are ready to be committed.
So this is the 29.
And this is the initial commit.
So commit the changes and we're done with the videos, with the tools for today.
Thanks a lot.