1
00:00:00,449 --> 00:00:02,218
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,218 --> 00:00:05,184
As we've seen, REST was
designed to be stateless

3
00:00:05,184 --> 00:00:07,561
and for applications to interact

4
00:00:07,561 --> 00:00:10,699
with things called resources.

5
00:00:10,699 --> 00:00:12,559
In this section we're going to be creating

6
00:00:12,559 --> 00:00:13,976
a proper REST API

7
00:00:15,026 --> 00:00:18,025
that follows all of the principles.

8
00:00:18,025 --> 00:00:19,151
In order to do that

9
00:00:19,151 --> 00:00:22,067
we're also going to
introduce a new library.

10
00:00:22,067 --> 00:00:24,777
It is an extension to the Flask library

11
00:00:24,777 --> 00:00:26,445
that we've already used.

12
00:00:26,445 --> 00:00:28,001
And this extension is going to allow us

13
00:00:28,001 --> 00:00:30,668
to create REST APIs very easily.

14
00:00:32,393 --> 00:00:35,536
It is limiting in some sense of the word

15
00:00:35,536 --> 00:00:39,593
because it decreases the
flexibility of Flask.

16
00:00:39,593 --> 00:00:43,760
However it does that to make
us adhere to the REST standard

17
00:00:45,953 --> 00:00:48,817
and that is a good thing
because we will make sure

18
00:00:48,817 --> 00:00:52,984
that our application follows
what REST was designed to be.

19
00:00:53,897 --> 00:00:57,045
When other applications such
as mobile apps or web apps

20
00:00:57,045 --> 00:00:58,570
interact with our API

21
00:00:58,570 --> 00:01:02,276
it would be easy for the
developers of those applications

22
00:01:02,276 --> 00:01:04,943
to understand how our API works.

23
00:01:06,493 --> 00:01:09,062
Flask-RESTful is what we're
going to be installing

24
00:01:09,062 --> 00:01:11,418
and as I said that's an extension to Flask

25
00:01:11,418 --> 00:01:13,735
and it just gives us a few goodies

26
00:01:13,735 --> 00:01:17,029
that we can use in our application.

27
00:01:17,029 --> 00:01:19,697
In this lecture however
we're going to be setting up

28
00:01:19,697 --> 00:01:23,569
so apologies for the sort
of a bit more boring content

29
00:01:23,569 --> 00:01:24,542
this time around

30
00:01:24,542 --> 00:01:28,587
but we have to do the setup at some point.

31
00:01:28,587 --> 00:01:30,883
In order to set up for this new project

32
00:01:30,883 --> 00:01:33,179
I've just gone to a new folder.

33
00:01:33,179 --> 00:01:34,439
I've called it section4

34
00:01:34,439 --> 00:01:35,575
and you can see here

35
00:01:35,575 --> 00:01:38,157
this is the path of
the folder that I am in

36
00:01:38,157 --> 00:01:41,324
so it's just basically in My Documents

37
00:01:42,281 --> 00:01:44,476
and what I'm going to do
is I'm going to create

38
00:01:44,476 --> 00:01:46,832
a virtual environment.

39
00:01:46,832 --> 00:01:49,300
A virtual environment essentially gives me

40
00:01:49,300 --> 00:01:51,883
a clean installation of Python.

41
00:01:53,008 --> 00:01:56,036
Here's pip3.5, remember that?

42
00:01:56,036 --> 00:01:58,808
We used that to instal
libraries in the last section

43
00:01:58,808 --> 00:01:59,975
such as Flask.

44
00:02:01,176 --> 00:02:03,093
If you do pip3.5 freeze

45
00:02:04,282 --> 00:02:05,696
that is going to give you

46
00:02:05,696 --> 00:02:07,800
the libraries that you've got installed

47
00:02:07,800 --> 00:02:08,883
such as Flask

48
00:02:09,780 --> 00:02:12,654
and the versions that you've
got these libraries on.

49
00:02:12,654 --> 00:02:17,227
For example right now I've
got Flask version 0.11.1

50
00:02:17,227 --> 00:02:19,157
and so on with a bunch of other libraries.

51
00:02:19,157 --> 00:02:22,063
Most of these libraries
are installed by Flask

52
00:02:22,063 --> 00:02:24,469
when you instal Flask.

53
00:02:24,469 --> 00:02:25,302
As you can see

54
00:02:25,302 --> 00:02:29,375
these libraries have
specific version numbers

55
00:02:29,375 --> 00:02:32,769
and last section we
created our application

56
00:02:32,769 --> 00:02:35,102
using this version of Flask.

57
00:02:37,228 --> 00:02:39,504
If we now create another application

58
00:02:39,504 --> 00:02:41,658
and Flask has been updated,

59
00:02:41,658 --> 00:02:45,491
for example imagine it
is now in version 0.12,

60
00:02:46,676 --> 00:02:50,912
we may go into pip3.5 and update
Flask to the latest version

61
00:02:50,912 --> 00:02:54,031
so we can get any bug
fixes and any new features

62
00:02:54,031 --> 00:02:55,992
that they've implemented,

63
00:02:55,992 --> 00:02:57,069
but in doing so

64
00:02:57,069 --> 00:03:00,826
we may break section 3's application

65
00:03:00,826 --> 00:03:03,743
if Flask has changed substantially.

66
00:03:04,627 --> 00:03:08,044
So updating isn't really the best option.

67
00:03:09,391 --> 00:03:10,987
Instead what we want to do

68
00:03:10,987 --> 00:03:15,070
is to let the application
in section 3 use 0.11.1

69
00:03:17,550 --> 00:03:21,717
and we can create our new
project using the newest version.

70
00:03:22,812 --> 00:03:24,426
In order to do that we have to make sure

71
00:03:24,426 --> 00:03:28,101
that our projects don't share libraries.

72
00:03:28,101 --> 00:03:30,756
If we went ahead and just
created a new project

73
00:03:30,756 --> 00:03:33,006
and ran it using Python 3.5

74
00:03:34,454 --> 00:03:38,192
that would be using
these libraries up here.

75
00:03:38,192 --> 00:03:39,066
So what we have to do

76
00:03:39,066 --> 00:03:42,052
is we have to get a clean
installation of Python

77
00:03:42,052 --> 00:03:43,881
with no libraries at all

78
00:03:43,881 --> 00:03:46,976
and use that for this project.

79
00:03:46,976 --> 00:03:49,294
If we then go ahead and
create a new project later on

80
00:03:49,294 --> 00:03:52,841
we will also do a fresh
installation of Python again

81
00:03:52,841 --> 00:03:54,508
and so on and so on.

82
00:03:55,868 --> 00:03:58,095
However, fortunately we
don't have to download Python

83
00:03:58,095 --> 00:04:01,241
and reinstall it and so on every time.

84
00:04:01,241 --> 00:04:04,799
What we want to do is to
use virtual environments.

85
00:04:04,799 --> 00:04:07,583
A virtual environment
mimics reinstalling Python

86
00:04:07,583 --> 00:04:09,083
with no libraries.

87
00:04:10,123 --> 00:04:10,966
The first thing you wanna do

88
00:04:10,966 --> 00:04:13,633
is do pip3.5 instal virtualenv.

89
00:04:16,076 --> 00:04:17,171
And then here as you can see

90
00:04:17,171 --> 00:04:19,753
I've already got this
requirement satisfied

91
00:04:19,753 --> 00:04:20,586
so it tells me that

92
00:04:20,586 --> 00:04:23,970
but in your case it will instal virtualenv

93
00:04:23,970 --> 00:04:26,823
and virtualenv is the tool
that we're going to use

94
00:04:26,823 --> 00:04:29,680
to create these virtual environments.

95
00:04:29,680 --> 00:04:31,273
Once that's done

96
00:04:31,273 --> 00:04:35,440
all we'll do is say virtualenv
venv --python=python3.5.

97
00:04:39,725 --> 00:04:41,919
Now this is calling the
virtualenv programme

98
00:04:41,919 --> 00:04:43,554
that we've installed

99
00:04:43,554 --> 00:04:44,764
and what's going to happen

100
00:04:44,764 --> 00:04:48,396
is it's going to create a folder
inside our current folder,

101
00:04:48,396 --> 00:04:50,916
inside this section4 folder that I'm in.

102
00:04:50,916 --> 00:04:54,038
It's going to create a folder called venv

103
00:04:54,038 --> 00:04:58,385
and in it it's going to put
a fresh Python installation.

104
00:04:58,385 --> 00:04:59,655
And that fresh Python instal

105
00:04:59,655 --> 00:05:01,822
is going to be Python 3.5.

106
00:05:03,243 --> 00:05:04,950
If you wanted to use Python 2.7

107
00:05:04,950 --> 00:05:07,609
you may put python2.7 there and so on.

108
00:05:07,609 --> 00:05:10,090
So just press enter at the end

109
00:05:10,090 --> 00:05:13,137
and then it will instal a
few things that it needs.

110
00:05:13,137 --> 00:05:15,025
This is doing the Python instal.

111
00:05:15,025 --> 00:05:18,174
And there it is, it's now finished.

112
00:05:18,174 --> 00:05:20,091
The next step is to do,

113
00:05:27,190 --> 00:05:29,375
sorry, apologies, not activate,

114
00:05:29,375 --> 00:05:33,542
source venv/bin/activate, there we go.

115
00:05:35,471 --> 00:05:39,769
Now this will only work
if you're on Mac or Linux.

116
00:05:39,769 --> 00:05:41,519
If you are on Windows

117
00:05:42,521 --> 00:05:46,688
what you want to do is
./venv/Scripts/activate.bat.

118
00:05:52,894 --> 00:05:56,994
This is what you want to
run if you are on Windows

119
00:05:56,994 --> 00:05:59,577
using the Windows command line.

120
00:06:00,682 --> 00:06:01,515
Okay?

121
00:06:01,515 --> 00:06:04,305
But for me I'm going to do
source venv/bin/activate

122
00:06:04,305 --> 00:06:07,497
and notice how now my
prompt is going to change

123
00:06:07,497 --> 00:06:11,265
and it's going to show
the bracket venv at first.

124
00:06:11,265 --> 00:06:15,320
This means that I am now
using the virtual environment.

125
00:06:15,320 --> 00:06:16,965
Whenever I run Python

126
00:06:16,965 --> 00:06:19,781
this is going to run Python 3.5

127
00:06:19,781 --> 00:06:23,060
which is the sort of Python
instal that I've done.

128
00:06:23,060 --> 00:06:25,861
Notice how in the past
I had to run Python 3.5

129
00:06:25,861 --> 00:06:28,008
to get this Python version.

130
00:06:28,008 --> 00:06:30,915
As long as I am within
the virtual environment

131
00:06:30,915 --> 00:06:35,088
I just have to run
Python to get the version

132
00:06:35,088 --> 00:06:38,351
that is installed in
the virtual environment.

133
00:06:38,351 --> 00:06:42,360
Okay so staying in the virtual
environment is very easy

134
00:06:42,360 --> 00:06:43,744
you just don't have to do anything

135
00:06:43,744 --> 00:06:45,491
and if you ever want to
leave the virtual environment

136
00:06:45,491 --> 00:06:47,481
and stop using the virtual environment

137
00:06:47,481 --> 00:06:49,064
just do deactivate.

138
00:06:50,489 --> 00:06:53,223
Notice how now if I do python --version

139
00:06:53,223 --> 00:06:54,715
I go back to the system version

140
00:06:54,715 --> 00:06:56,382
which is Python 2.7.

141
00:06:57,246 --> 00:07:00,688
Okay so I'm going to back into
the virtual environment now

142
00:07:00,688 --> 00:07:03,438
and do pip instal Flask-RESTful.

143
00:07:06,478 --> 00:07:08,400
Okay so what is going to happen now

144
00:07:08,400 --> 00:07:10,879
is that I am in the virtual environment

145
00:07:10,879 --> 00:07:13,417
and I'm going to instal this library

146
00:07:13,417 --> 00:07:14,834
that is required.

147
00:07:19,880 --> 00:07:23,029
Notice how installing this
library will also instal Flask

148
00:07:23,029 --> 00:07:25,142
so you don't have to do that
separately if you don't want

149
00:07:25,142 --> 00:07:28,129
but you can instal Flask
separately if you prefer

150
00:07:28,129 --> 00:07:31,368
just by doing pip instal Flask.

151
00:07:31,368 --> 00:07:34,203
Okay now we can do pip freeze

152
00:07:34,203 --> 00:07:36,610
and this well tell us
what we've got installed

153
00:07:36,610 --> 00:07:38,694
and as you can see we've
got a bunch of things

154
00:07:38,694 --> 00:07:41,192
including Flask, the
same version as before,

155
00:07:41,192 --> 00:07:42,717
the version hasn't changed yet,

156
00:07:42,717 --> 00:07:44,320
but it might in the future.

157
00:07:44,320 --> 00:07:47,246
And we've also got a thing
called Flask-RESTful.

158
00:07:47,246 --> 00:07:49,685
Now Flask-RESTful is
what's going to allow us

159
00:07:49,685 --> 00:07:52,475
to very easily create rest APIs.

160
00:07:52,475 --> 00:07:54,611
So that's us all set up

161
00:07:54,611 --> 00:07:56,013
and setting up the virtual environment

162
00:07:56,013 --> 00:07:57,487
and installing Flask-RESTful

163
00:07:57,487 --> 00:07:58,553
and in the very next video

164
00:07:58,553 --> 00:08:01,652
we're going to talk about
how to use Flask-RESTful

165
00:08:01,652 --> 00:08:03,379
to create our API.

166
00:08:03,379 --> 00:08:04,856
I'll see you there.

