Instructor: 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 you to Flask.
Now, we have two pages here.
One points to our domain/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.html page.
So tutorial and about were not related to our project,
so I remove them from templates.
Now, in home.html, I'm going to write some documentation
about the API and in main.py in here,
I'm removing that home name,
so that when the users visit the domain homepage
without entering any subdirectory,
they can go to this home.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.html.
So it points to that home.html file and run main.py.
So if you click over the domain,
that should take you to the homepage.
So we see the string.
Hello, so home.html has been displayed here on the homepage.
Then in the homepage, we want to put
some basic information such as an h1 tag,
Weather Data 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 title should also be probably the same,
Weather Data API,
so this is a title of this particular webpage.
Next, we should think
about what URL formats 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.com or myphythonapi.com.
But for now, our domain name is this locally.
So we want to tell the user something
like I'm copying this HTTP URL.
So we want to tell the user
that the URL format will be something
like this, /api/v1/.
So you can write whatever you like here, right?
You can write appi or appis, whatever you like.
So, but it makes sense to write something meaningful.
And so, after the slash, I'm writing v1
to basically tell 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 parameters 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, 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 a format, that's a formula so to say.
And this would be an example, let's say station
with the code 10 and date 19881025.
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 py script
should be able to handle this kind of URL.
So currently, this script is only handling this URL.
So the homepage and the about page.
Now, we don't need an about page.
So let's change this about page
to /api/v1, so /api/v1.
So when the user visits /api/v1/station,
with these 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 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 URL here enters
for example this, they place it here.
So they place 10 for station.
Now, that 10, that value 10, 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 df is equal
to pandas.read_csv, and we are 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 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 webpage 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 the try.
Save the main.py 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, et cetera.
But we returned 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,
we'll 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 data 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, 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, reload, 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 page,
but for now, it's just static.
So now, we have this data and as you saw
in the previous app where we accessed 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 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.py.
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 main as output.
So this variable contains this string, __main__.
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 as 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
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 name variable.
So this is a common practice and yeah, that's it.
Let's do some reformatting.
So this needs two 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 .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 html were 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 day 29 and this is the initial commit.
So commit the changes and we are done
with the videos, with the tutorials for today.
Thanks a lot.