Now that we've got our app working,
you might think we've reached the end of today's lessons,
but there's just one final thing I want to talk to you about.
And that is the environment. Not the environment as a whole,
but more precisely environment variables.
Now what our environment variables anyways?
If you go into your PyCharm project
and you go into this tab where it says "Terminal", here
if you type in env (set on Windows) and hit "Enter",
you can see a whole bunch of variables.
So you've got a key and a value
and in between there is just a single equal (=) sign.
So these are the different variables that are set in the environment in which
your code is run.
These variables have values which are strings
which can be used in our applications or our code.
Now inside Python
anywhere you can also type env and you can see some slightly different
environment variables which are relative to the Python anywhere environment.
What exactly all these environment variables used for? Well,
there's two major use cases. One is for convenience.
Normally when you deploy a large application,
the process is quite complicated. And once you've done it,
you kind of don't want to mess around with the code base and update the code
files like your main.py for example. Instead,
you could have these environment variables, which you can change. For example,
if you had an application that was sending you emails out to your clients,
then your client base emails might change day to day.
So certain variables that are being used in your code base could be set as
environment variables
and you can modify those variables without having to touch the code.
A second reason might be for security.
So when you're developing software,
you might be uploading your code based somewhere, for example
to store it online or to a service like PythonAnywhere.
And it's usually not a good idea to have things like your authentication keys or
your API keys to be stored in the same place as the rest of your code.
That's where environment variables come in.
So environment variables essentially allow us to separate out where we store
our keys, our secret stuff,
and various other variables away from where our code base is located.
In our case,
it doesn't really matter because we haven't got a paid account anywhere in here.
It doesn't really matter if somebody steals our auth token or our API key,
because none of those are linked to our payment details. Now, however,
if we were to upgrade our accounts on open weather map's API
or on Twilio's API,
then we definitely want to keep these two things secret; auth token and the API
key.
Instead of having it located in the same place where we've got our code base
which means you might accidentally upload it somewhere on the internet where
other people can see it, instead,
we can store these two things as environment variables.
We can create an environment variable by simply typing export
and then the name of the variable which I'll call OWM
_API_KEY. And then it's really important that we have no spaces,
but just a single equal sign.
And then we're going to store everything that's in between the quotation marks.
So I'm going to copy that from over here,
and I'm going to paste that in here. Once I've exported that
and then if I hit "env" again, you'll see
now in this updated environment
you can see that environment variable go in right there.
And now we can tap into that environment variable in any of the code that we run
from this particular environment. To do that we have to use this os module that
we've already got.
So I'm going to delete everything that's currently stored in the API key.
And instead, I'm going to tap into os.environ,
and then I'm going to use a method called get to get the value of a particular
environment variable.
The name is everything that's before the equal sign.
So let's go ahead and paste it in here. And now when I run this code,
so using python3 and then the name of the file,
you can see that it successfully runs queuing our message into Twilio.
Now go ahead and do the same thing with the auth token.
You want to save this as an environment
variable code auth_token in all caps,
similar to what we've got here.
And you want to store this value in the environment variable.
Pause the video and give that a go. All right.
So we're going to follow the exact same thing that we did before. We use the
export keyword and then we type in the name of the environment variable
which is going to be AUTH_TOKEN. And then really importantly,
we just have an equal sign, no spaces,
and we're going to store this value without the double-quotes
and we're going to put it in here. Now in our environment variables,
you can see we've got our open weather map API key.
And if we scroll up a bit more, you can see our auth token as well.
So now we can replace this string with our os.environ.get
and then we pass in the name of the key which is AUTH_TOKEN.
And now let's run our code again with python3 main.py.
And you can see it's still works as before.
But now if somebody comes across this particular code base on the internet,
they won't be able to use our Twilio account or our open weather map account
because both of these keys are now hidden. Let's go ahead and hit save.
So now we're going to do the same thing,
but at the point where we run our code from our task scheduler.
So go ahead and right-click on this snake and then open the link in a new tab so
that we can go to our tasks section.
And here we're going to edit our command. Instead of just running Python main
.py, we're going to export those two environment variables.
This is why we've got two screens open up so we can actually copy the actual
values. The first value is going to be our open weather map
API key. So I'm going to copy that. And then here,
I'm going to write export and then paste in that first key. Now,
afterward, I'm going to use a semi-colon to denote a new line
and then I'm going to export the next key. Scrolling up,
we can see our auth token right here so we're going to copy that as well
and we're going to export that as the other environment variable.
And then finally, we're going to cap it off with a semicolon.
So now you should have your export OWM_API_KEY
and then your exporting your auth token as well. And then finally,
on the last line, we're running our python3 main.py.
Now let's change this to run at the current time.
So it's going to be UTC time which happens to be 10 o'clock
and then instead of 46, it's now 48
so I'm going to change that to 49. And then I'm gonna hit the tick.
And now we're going to wait to see if it actually works
even though we've taken out the API keys from our main.py
because we're exporting it in our command.
Let's see if we still get the notification message.
There you have it. It's still working as before.
But now this is invisible to people
if they happen to access our main.py.
Now don't worry. The service of Python anywhere are pretty secure.
Nobody's really going to hack into your account
just to look into your main.py.
The bigger problem is when you upload your own code to places like Git
Hub or a BitBucket,
basically places that are essentially a Dropbox for code.
And when you have it on there, because your code could be public,
that's where the problem happens.
If people look at your code file and they see that you've got API keys in there,
then they might steal them. So whenever you're uploading your code publicly,
always be careful that you strip it of all of the API keys and instead use
environment variables like we have here to pull it from the environment.
All that's left to do for you is to update your latitude and longitude to your
actual local latitude and longitude instead of the rainy place that we're
testing. And you've now completed the rain alert application.
Now I've showed you quite a few different APIs,
but there's a whole world of APIs for you to explore.
And if you have the patience to read their documentation and understand how to
work with their APIs, then you now know how to work with their endpoints,
how to pass parameters and also how to authenticate yourself with their servers.
So here's a list of fun APIs that you can use to build applications with.
I've shown you how you can make a rain alert and send yourself SMS messages,
or how to get the location of the ISS or the sunrise and sunset times,
but there's a whole bunch of other APIs for you to explore. For example,
things like the open movie database API,
which contains a whole bunch of movie related data,
or looking up songs and artists using the Spotify API and a whole lot more.
So I'll leave you to explore that.
And if you come up with something innovative and interesting,
be sure to share it with the rest of us in the Q&A below this lesson so that
we can all check it out and admire your hard work.