Hey welcome back.
In this video we will construct the API documentation page
and the API page itself.
One of 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 you to the flask.
Now we have two pages here.
One points to our domain slash homepage and the other points to our domain.
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 dot HTML page.
So tutorial and about
were not related to our project, so I removed them from templates.
Now in home that is HTML.
I'm going to write some documentation about the API.
And in main that's API.
And 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 that HTML.
So it points to that home that HTML file and run main dot pi.
So if you click over the domain
that should take you to the homepage.
So we see this 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 H1 tag.
With the Delta API.
So this should be something about our API.
If we 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 titles should also be probably the same whether it does API.
So this is the title of this particular web page.
Next we should think
about what URL formats are we going to have.
So and 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 hosted on the server, we're going to get a domain
such as example.com or my Python API that's com.
But for now our domain name is this locally.
So we want to tell the users something like
I'm copying this HTP URL.
So we want to tell the user that's a verb you want.
A format will be something like this.
Slash API v1.
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 V1 to basically tell the users that this is version one.
Of course, you don't have to write this.
You can leave it like that.
You can also leave it like that and then continue here with the parameter
such as station and date.
But this is a good idea to have API v1.
And then.
You write the parameters station and date.
Now this part to 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.
URL example.
So here you'll understand what I'm talking about.
So basically that's the format.
That's a formula.
So to see and this would be an example.
Let's say station with the code ten and date
1988 1020 fifth.
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's you.
Earth should give data for this particular station.
Now our main.py 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 to know about page.
So let's
change this about
page to API v1.
So API v1.
So when the user visits API v1 slash station with this brackets.
So less than greater then this is a special syntax
of the URL pattern you place in flask.
And the same goes for date like that.
So these two denote that the users
can enter values for these two.
So any value that the user enters, let's suppose the user
in the earlier 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 this station.
Arguments here of this function station and date right.
So data will go here and station will go in here.
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 DF is equal to pandas.
Dot read CSV and we going to read our data
which contain all the historical weather data.
And we're going to get these 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 date.
So 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.
On the web page, one of the user clicks that you want help.
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 hardcoding 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 you the try save the main that's pure file.
Go here.
Enter that URL.
If you get this error, it says that the return type
must be a string dict, list, tuple with headers, etc.
but we returned an integer here,
so we can simply convert this into a string.
Refresh the page.
And that's once we get 23.
So one of the users ask for a particular station.
And the particular date we will calculate will find out what the temperature was
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 choose 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 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 confuse them.
And refresh.
Let's 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 24.
The station of course, the temperature will also be dynamically
calculated for every load of page, but for now it's just static.
So now we have this data and as you saw in the previous app
where we access the news 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 website 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 spar.
You don't have to create these files.
So I'll delete them.
so if I print name here
and I execute this a.py file,
I'm going to get name as output.
So this variable contains this string underscore
underscore main underscore underscore.
Now if I create a second.
b.py file.
And here I import a and then I execute b.py.
We're going to get a s 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
main.
Then we want to run the flask app.
In other words, we only want to run this flask app
when this main.py 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 where 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 name variable.
So this is a common practice.
And see how that suits.
Let's do some formatting.
So this needs to break lines here to be more readable
according to the coding styles.
And so 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.
Gitignore 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.
Gitignore and main.py
about and tutorial dot HTML files where we did some trials
and simple examples so they are not related to the project.
Therefore I'll not add them to the tracking.
So with that right click go to git
and 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 tutorials for today.
Thanks a lot.