WEBVTT

0
00:00.030 --> 00:01.800
Now that we've got our app working,

1
00:02.040 --> 00:04.770
you might think we've reached the end of today's lessons,

2
00:05.070 --> 00:08.010
but there's just one final thing I want to talk to you about.

3
00:08.550 --> 00:12.210
And that is the environment. Not the environment as a whole,

4
00:12.330 --> 00:15.510
but more precisely environment variables.

5
00:16.260 --> 00:18.510
Now what our environment variables anyways?

6
00:18.870 --> 00:20.760
If you go into your PyCharm project

7
00:20.880 --> 00:24.780
and you go into this tab where it says "Terminal", here

8
00:24.810 --> 00:27.840
if you type in env (set on Windows) and hit "Enter",

9
00:28.170 --> 00:31.020
you can see a whole bunch of variables.

10
00:31.050 --> 00:35.250
So you've got a key and a value

11
00:35.760 --> 00:39.270
and in between there is just a single equal (=) sign.

12
00:39.960 --> 00:44.790
So these are the different variables that are set in the environment in which

13
00:44.820 --> 00:45.720
your code is run.

14
00:46.350 --> 00:49.980
These variables have values which are strings

15
00:50.400 --> 00:54.300
which can be used in our applications or our code.

16
00:55.230 --> 00:56.490
Now inside Python

17
00:56.490 --> 01:00.750
anywhere you can also type env and you can see some slightly different

18
01:00.750 --> 01:05.100
environment variables which are relative to the Python anywhere environment.

19
01:05.580 --> 01:09.660
What exactly all these environment variables used for? Well,

20
01:09.840 --> 01:14.370
there's two major use cases. One is for convenience.

21
01:14.970 --> 01:18.690
Normally when you deploy a large application,

22
01:19.140 --> 01:23.340
the process is quite complicated. And once you've done it,

23
01:23.670 --> 01:28.230
you kind of don't want to mess around with the code base and update the code

24
01:28.230 --> 01:31.890
files like your main.py for example. Instead,

25
01:31.920 --> 01:36.450
you could have these environment variables, which you can change. For example,

26
01:36.510 --> 01:40.260
if you had an application that was sending you emails out to your clients,

27
01:40.500 --> 01:43.500
then your client base emails might change day to day.

28
01:44.070 --> 01:48.840
So certain variables that are being used in your code base could be set as

29
01:48.840 --> 01:49.980
environment variables

30
01:50.220 --> 01:53.640
and you can modify those variables without having to touch the code.

31
01:54.510 --> 01:57.480
A second reason might be for security.

32
01:58.140 --> 01:59.700
So when you're developing software,

33
01:59.730 --> 02:04.110
you might be uploading your code based somewhere, for example

34
02:04.110 --> 02:07.590
to store it online or to a service like PythonAnywhere.

35
02:08.520 --> 02:13.440
And it's usually not a good idea to have things like your authentication keys or

36
02:13.440 --> 02:18.440
your API keys to be stored in the same place as the rest of your code.

37
02:19.140 --> 02:21.180
That's where environment variables come in.

38
02:21.420 --> 02:26.420
So environment variables essentially allow us to separate out where we store

39
02:26.490 --> 02:28.590
our keys, our secret stuff,

40
02:28.800 --> 02:33.480
and various other variables away from where our code base is located.

41
02:34.140 --> 02:34.973
In our case,

42
02:35.010 --> 02:39.270
it doesn't really matter because we haven't got a paid account anywhere in here.

43
02:39.750 --> 02:44.370
It doesn't really matter if somebody steals our auth token or our API key,

44
02:44.490 --> 02:49.170
because none of those are linked to our payment details. Now, however,

45
02:49.170 --> 02:53.130
if we were to upgrade our accounts on open weather map's API

46
02:53.400 --> 02:54.900
or on Twilio's API,

47
02:55.200 --> 03:00.200
then we definitely want to keep these two things secret; auth token and the API

48
03:00.790 --> 03:01.623
key.

49
03:01.810 --> 03:05.860
Instead of having it located in the same place where we've got our code base

50
03:06.310 --> 03:10.210
which means you might accidentally upload it somewhere on the internet where

51
03:10.210 --> 03:12.610
other people can see it, instead,

52
03:12.640 --> 03:15.910
we can store these two things as environment variables.

53
03:16.600 --> 03:21.340
We can create an environment variable by simply typing export

54
03:21.760 --> 03:26.050
and then the name of the variable which I'll call OWM

55
03:26.410 --> 03:31.270
_API_KEY. And then it's really important that we have no spaces,

56
03:31.480 --> 03:33.640
but just a single equal sign.

57
03:34.360 --> 03:37.720
And then we're going to store everything that's in between the quotation marks.

58
03:38.020 --> 03:39.880
So I'm going to copy that from over here,

59
03:40.900 --> 03:45.430
and I'm going to paste that in here. Once I've exported that

60
03:46.180 --> 03:49.330
and then if I hit "env" again, you'll see

61
03:49.330 --> 03:51.370
now in this updated environment

62
03:51.430 --> 03:55.090
you can see that environment variable go in right there.

63
03:55.510 --> 04:00.100
And now we can tap into that environment variable in any of the code that we run

64
04:00.160 --> 04:05.160
from this particular environment. To do that we have to use this os module that

65
04:05.740 --> 04:06.573
we've already got.

66
04:06.940 --> 04:10.780
So I'm going to delete everything that's currently stored in the API key.

67
04:11.230 --> 04:15.250
And instead, I'm going to tap into os.environ,

68
04:16.030 --> 04:21.030
and then I'm going to use a method called get to get the value of a particular

69
04:21.070 --> 04:22.060
environment variable.

70
04:22.960 --> 04:26.500
The name is everything that's before the equal sign.

71
04:26.980 --> 04:31.960
So let's go ahead and paste it in here. And now when I run this code,

72
04:32.320 --> 04:36.010
so using python3 and then the name of the file,

73
04:36.760 --> 04:41.470
you can see that it successfully runs queuing our message into Twilio.

74
04:42.250 --> 04:45.730
Now go ahead and do the same thing with the auth token.

75
04:46.360 --> 04:48.520
You want to save this as an environment

76
04:48.520 --> 04:52.510
variable code auth_token in all caps,

77
04:52.900 --> 04:54.430
similar to what we've got here.

78
04:55.060 --> 04:58.390
And you want to store this value in the environment variable.

79
04:58.630 --> 05:02.850
Pause the video and give that a go. All right.

80
05:02.880 --> 05:06.090
So we're going to follow the exact same thing that we did before. We use the

81
05:06.090 --> 05:10.920
export keyword and then we type in the name of the environment variable

82
05:11.190 --> 05:15.420
which is going to be AUTH_TOKEN. And then really importantly,

83
05:15.450 --> 05:17.670
we just have an equal sign, no spaces,

84
05:18.090 --> 05:21.840
and we're going to store this value without the double-quotes

85
05:22.440 --> 05:26.970
and we're going to put it in here. Now in our environment variables,

86
05:27.000 --> 05:30.120
you can see we've got our open weather map API key.

87
05:30.540 --> 05:35.280
And if we scroll up a bit more, you can see our auth token as well.

88
05:36.750 --> 05:41.750
So now we can replace this string with our os.environ.get

89
05:43.560 --> 05:47.580
and then we pass in the name of the key which is AUTH_TOKEN.

90
05:48.630 --> 05:53.630
And now let's run our code again with python3 main.py.

91
05:54.090 --> 05:56.580
And you can see it's still works as before.

92
05:57.320 --> 06:01.730
But now if somebody comes across this particular code base on the internet,

93
06:02.030 --> 06:07.030
they won't be able to use our Twilio account or our open weather map account

94
06:07.460 --> 06:12.350
because both of these keys are now hidden. Let's go ahead and hit save.

95
06:13.760 --> 06:15.410
So now we're going to do the same thing,

96
06:15.680 --> 06:19.340
but at the point where we run our code from our task scheduler.

97
06:19.880 --> 06:24.590
So go ahead and right-click on this snake and then open the link in a new tab so

98
06:24.590 --> 06:27.470
that we can go to our tasks section.

99
06:27.920 --> 06:32.480
And here we're going to edit our command. Instead of just running Python main

100
06:32.480 --> 06:36.740
.py, we're going to export those two environment variables.

101
06:37.280 --> 06:41.810
This is why we've got two screens open up so we can actually copy the actual

102
06:41.900 --> 06:46.430
values. The first value is going to be our open weather map

103
06:46.460 --> 06:49.790
API key. So I'm going to copy that. And then here,

104
06:49.820 --> 06:54.470
I'm going to write export and then paste in that first key. Now,

105
06:54.500 --> 06:58.790
afterward, I'm going to use a semi-colon to denote a new line

106
06:59.210 --> 07:03.140
and then I'm going to export the next key. Scrolling up,

107
07:03.170 --> 07:07.220
we can see our auth token right here so we're going to copy that as well

108
07:07.550 --> 07:11.330
and we're going to export that as the other environment variable.

109
07:11.780 --> 07:14.480
And then finally, we're going to cap it off with a semicolon.

110
07:14.930 --> 07:18.860
So now you should have your export OWM_API_KEY

111
07:19.430 --> 07:23.750
and then your exporting your auth token as well. And then finally,

112
07:23.750 --> 07:26.960
on the last line, we're running our python3 main.py.

113
07:27.770 --> 07:31.550
Now let's change this to run at the current time.

114
07:31.550 --> 07:35.690
So it's going to be UTC time which happens to be 10 o'clock

115
07:35.720 --> 07:38.600
and then instead of 46, it's now 48

116
07:38.600 --> 07:43.340
so I'm going to change that to 49. And then I'm gonna hit the tick.

117
07:44.060 --> 07:47.120
And now we're going to wait to see if it actually works

118
07:47.330 --> 07:51.410
even though we've taken out the API keys from our main.py

119
07:51.650 --> 07:53.960
because we're exporting it in our command.

120
07:54.200 --> 07:56.870
Let's see if we still get the notification message.

121
07:58.490 --> 08:01.310
There you have it. It's still working as before.

122
08:02.180 --> 08:04.760
But now this is invisible to people

123
08:05.180 --> 08:07.910
if they happen to access our main.py.

124
08:08.510 --> 08:12.020
Now don't worry. The service of Python anywhere are pretty secure.

125
08:12.050 --> 08:14.000
Nobody's really going to hack into your account

126
08:14.300 --> 08:16.130
just to look into your main.py.

127
08:16.760 --> 08:20.390
The bigger problem is when you upload your own code to places like Git

128
08:20.390 --> 08:21.890
Hub or a BitBucket,

129
08:22.160 --> 08:25.580
basically places that are essentially a Dropbox for code.

130
08:26.150 --> 08:29.120
And when you have it on there, because your code could be public,

131
08:29.420 --> 08:31.280
that's where the problem happens.

132
08:31.580 --> 08:35.660
If people look at your code file and they see that you've got API keys in there,

133
08:35.660 --> 08:40.130
then they might steal them. So whenever you're uploading your code publicly,

134
08:40.310 --> 08:45.310
always be careful that you strip it of all of the API keys and instead use

135
08:45.560 --> 08:50.450
environment variables like we have here to pull it from the environment.

136
08:51.440 --> 08:56.070
All that's left to do for you is to update your latitude and longitude to your

137
08:56.070 --> 09:00.810
actual local latitude and longitude instead of the rainy place that we're

138
09:00.810 --> 09:05.550
testing. And you've now completed the rain alert application.

139
09:06.210 --> 09:08.970
Now I've showed you quite a few different APIs,

140
09:09.000 --> 09:12.300
but there's a whole world of APIs for you to explore.

141
09:12.810 --> 09:16.680
And if you have the patience to read their documentation and understand how to

142
09:16.680 --> 09:20.700
work with their APIs, then you now know how to work with their endpoints,

143
09:20.940 --> 09:25.560
how to pass parameters and also how to authenticate yourself with their servers.

144
09:26.100 --> 09:31.100
So here's a list of fun APIs that you can use to build applications with.

145
09:32.700 --> 09:37.700
I've shown you how you can make a rain alert and send yourself SMS messages,

146
09:38.520 --> 09:43.110
or how to get the location of the ISS or the sunrise and sunset times,

147
09:43.410 --> 09:48.090
but there's a whole bunch of other APIs for you to explore. For example,

148
09:48.090 --> 09:50.460
things like the open movie database API,

149
09:50.910 --> 09:53.970
which contains a whole bunch of movie related data,

150
09:54.390 --> 09:59.390
or looking up songs and artists using the Spotify API and a whole lot more.

151
10:00.840 --> 10:02.760
So I'll leave you to explore that.

152
10:03.090 --> 10:06.000
And if you come up with something innovative and interesting,

153
10:06.240 --> 10:10.380
be sure to share it with the rest of us in the Q&A below this lesson so that

154
10:10.380 --> 10:12.810
we can all check it out and admire your hard work.