1
00:00:00,000 --> 00:00:05,000
Hey guys, welcome to CS445 - Computational Photography. My name is Ashwin Ramesh,

2
00:00:05,000 --> 00:00:07,880
and today I'll be doing a quick tutorial on how to use Jupyter Notebook.

3
00:00:07,880 --> 00:00:11,020
It's a very important tool in python development, not just for the assignments in

4
00:00:11,020 --> 00:00:13,880
this course, which are going to be distributed as python

5
00:00:13,880 --> 00:00:17,000
notebooks, but for interactive prototyping in general.

6
00:00:17,000 --> 00:00:20,000
I'll just briefly go over some installation options,

7
00:00:20,000 --> 00:00:23,000
for Jupyter notebook, and then start on how to use it.

8
00:00:23,000 --> 00:00:26,000
Okay, so these are the Jupyter notebook docs.

9
00:00:26,000 --> 00:00:29,000
You should just be able to Google for these,

10
00:00:29,000 --> 00:00:35,792
and the installation guide is right here, so go ahead and click right there,

11
00:00:35,792 --> 00:00:41,000
and that should take you to this page.

12
00:00:41,000 --> 00:00:44,000
There are two main options to install Jupyter

13
00:00:44,000 --> 00:00:47,000
notebook based on what python package manager you

14
00:00:47,000 --> 00:00:50,000
like to use. There is Anaconda,

15
00:00:50,000 --> 00:00:52,880
and there is pip. I prefer to use pip.

16
00:00:52,880 --> 00:00:55,980
It is quite lightweight and stable. It is the default

17
00:00:55,980 --> 00:00:58,980
python package manager. So

18
00:00:58,980 --> 00:01:01,980
I'll go ahead and do that. Let's just

19
00:01:01,980 --> 00:01:04,980
copy paste this command here.

20
00:01:04,980 --> 00:01:10,340
We will got to a terminal here.

21
00:01:12,400 --> 00:01:16,260
Just clear up there. Okay, so

22
00:01:16,260 --> 00:01:21,680
first thing is first, we'll just upgrade pip.

23
00:01:25,860 --> 00:01:28,840
Should be brief.

24
00:01:28,840 --> 00:01:31,840
Okay and now we will go ahead

25
00:01:31,840 --> 00:01:34,840
and install jupyter.

26
00:01:34,840 --> 00:01:38,740
So its always good to do this because

27
00:01:38,740 --> 00:01:41,740
even if you already have jupyter because it will update any

28
00:01:41,740 --> 00:01:46,400
related packages that jupyter needs.

29
00:01:46,400 --> 00:01:49,740
Okay, and the way we will

30
00:01:49,740 --> 00:01:52,740
start our jupyter notebook server is just by

31
00:01:52,740 --> 00:01:55,740
navigating to the directory that we want to have the

32
00:01:55,740 --> 00:01:58,740
notebook in. Here as you can see I have

33
00:01:58,740 --> 00:02:01,740
it in a directory called CS445, and we'll just

34
00:02:01,740 --> 00:02:03,680
run jupyter notebook.

35
00:02:06,080 --> 00:02:10,240
What this will do is automatically open up a tab in your browser.

36
00:02:10,240 --> 00:02:13,240
But let's navigate back to

37
00:02:13,240 --> 00:02:16,240
the terminal page just in case it didn't do that.

38
00:02:16,240 --> 00:02:19,240
At the bottom it should basically tell you

39
00:02:19,240 --> 00:02:20,560
where

40
00:02:22,560 --> 00:02:27,560
at what website, or at what URL it started the notebook server.

41
00:02:27,560 --> 00:02:30,560
So you should just be able to visit this.

42
00:02:33,660 --> 00:02:38,200
So here we are, we are at the filesystem tree

43
00:02:38,200 --> 00:02:41,800
started by Jupyter notebook, we are going to go ahead and

44
00:02:41,800 --> 00:02:45,800
create a new notebook so go to "New".

45
00:02:45,800 --> 00:02:49,800
Jupyter notebook lets us pick the python kernal that

46
00:02:49,800 --> 00:02:53,640
we want to use in this notebook. So we're going to go ahead and use

47
00:02:53,640 --> 00:02:57,640
python 3. and that's going to give us a new

48
00:02:57,640 --> 00:03:00,980
python 3 kernel notebook.

49
00:03:00,980 --> 00:03:04,980
Okay so, here it says "Untitled". This is the

50
00:03:04,980 --> 00:03:08,980
title of the notebook, or the notebook name. We can go ahead and change it to whatever

51
00:03:08,980 --> 00:03:13,980
we want here. Let's call it "jupyter_tutorial".

52
00:03:17,160 --> 00:03:22,160
So jupyter automatically saves every few seconds.

53
00:03:22,160 --> 00:03:26,660
However, its good to save often, of course.

54
00:03:26,660 --> 00:03:29,660
So the main parts of the jupyter notebook are the heading.

55
00:03:29,660 --> 00:03:32,660
Here you have your toolbars.

56
00:03:32,660 --> 00:03:37,080
And here you the code cells.

57
00:03:37,080 --> 00:03:40,800
There are several types of cells in jupyter notebook,

58
00:03:40,800 --> 00:03:44,200
But two main ones are : Markdown and Code cells.

59
00:03:44,200 --> 00:03:48,200
You can actually change the cell type by clicking on the cell.

60
00:03:48,200 --> 00:03:51,960
Here the cell is highlighted in green, and then

61
00:03:51,960 --> 00:03:56,500
going to this dropdown menu and switching it to the type you want.

62
00:03:56,500 --> 00:04:00,500
So these two are mainly the ones we're going to use.

63
00:04:00,500 --> 00:04:05,140
Let's go to markdown. So in a markdown cell you can add headings,

64
00:04:05,140 --> 00:04:09,140
You can add ordered lists. You can write any type of markdown that you want.

65
00:04:16,020 --> 00:04:24,320
So here we have this, and you can have something like a table of contents.

66
00:04:30,300 --> 00:04:35,300
It really doesn't matter. Okay, so the way you would run this

67
00:04:35,300 --> 00:04:40,300
cell and render it, i.e. have jupyter notebook render it, would be

68
00:04:40,300 --> 00:04:44,300
by selecting the cell and hitting run.

69
00:04:44,300 --> 00:04:49,300
You could alternatively just hit Shift+Enter, but let's go ahead and do that now.

70
00:04:49,300 --> 00:04:53,300
There we go we have rendered our markdown cell.

71
00:04:55,320 --> 00:05:00,080
Okay so let's look at how to use a code cell now.

72
00:05:00,460 --> 00:05:05,980
So let's do one of the first things you generally do

73
00:05:05,980 --> 00:05:09,980
when you start a python application,

74
00:05:09,980 --> 00:05:13,980
which are your imports, so let's import matplotlib.

75
00:05:15,060 --> 00:05:20,240
Okay so we're going to type in some python code, click on our cell, and hit Shift + Enter or

76
00:05:20,240 --> 00:05:24,240
click the run button. Here we go. Alright, perfect.

77
00:05:24,240 --> 00:05:29,540
So, as you can see, Jupyter notebook executed the code:

78
00:05:29,540 --> 00:05:34,900
import matplotlib, and it found that it was unable to do so.

79
00:05:34,900 --> 00:05:38,720
It returned an error, it printed below the cell.

80
00:05:38,720 --> 00:05:42,480
So this is generally how it works. You run the cell here and

81
00:05:42,480 --> 00:05:46,080
the output of the command is rendered below the cell.

82
00:05:46,080 --> 00:05:50,260
So why didn't it find matplotlib?

83
00:05:50,260 --> 00:05:55,160
Likely, it's because I don't have matplotlib

84
00:05:55,160 --> 00:05:58,660
installed for this python kernel

85
00:05:58,660 --> 00:06:02,700
this is actually convenient for this tutorial because

86
00:06:02,700 --> 00:06:09,260
usually we would have to go to the terminal and do a pip install or conda install of matpplotlib

87
00:06:09,260 --> 00:06:14,020
but Jupyter notebook actually allows us to run shell commands right here from the notebook.

88
00:06:14,020 --> 00:06:18,460
The way we would do this is to go to a code cell,

89
00:06:18,460 --> 00:06:22,920
insert an exclamation point, and we would

90
00:06:22,920 --> 00:06:27,340
then type our shell command. So here I would do a

91
00:06:27,340 --> 00:06:33,480
pip3 install matplotlib.

92
00:06:33,480 --> 00:06:37,220
Okay so let's go ahead and do a Shift + Enter for this.

93
00:06:37,220 --> 00:06:40,900
And as you can see, there is a star here by the

94
00:06:40,900 --> 00:06:46,300
"In [*]", that means that the command is currently executing.

95
00:06:46,300 --> 00:06:49,880
Once it's done, this will turn into a number.

96
00:06:49,880 --> 00:06:55,060
There we go, so just finished, and now what we can do is

97
00:06:55,060 --> 00:07:00,300
go back to our initial cell, click our initial cell and

98
00:07:00,300 --> 00:07:05,400
we will hit Shift + Enter, try to import matplotlib again and this time,

99
00:07:05,400 --> 00:07:09,560
the command ran successfully. Import commands don't really return any

100
00:07:09,560 --> 00:07:13,740
output to stdout or stderr, so we receive nothing.

101
00:07:13,740 --> 00:07:17,900
Another thing you can do between these two cells, since

102
00:07:17,900 --> 00:07:21,980
the next time I use this notebook, if I want to run

103
00:07:21,980 --> 00:07:26,200
cells consecutively I want the "pip3 install" to be first. I can just rearrange

104
00:07:26,200 --> 00:07:30,460
these two cells. So I can navigate to this cell number 2 and just click this

105
00:07:30,460 --> 00:07:34,900
up arrow which basically stands for moving this

106
00:07:34,900 --> 00:07:38,800
selected cell up, so let's go ahead and do that.

107
00:07:38,800 --> 00:07:44,400
Here we go, okay so here I am.

108
00:07:44,400 --> 00:07:49,520
Perfect, so next let's talk about state in Jupyter notebook,

109
00:07:49,520 --> 00:07:54,240
and some common pitfalls that I've seen students have.

110
00:07:54,240 --> 00:07:59,560
Okay so I have a cell here, and

111
00:07:59,560 --> 00:08:03,760
I have a variable called "a" that I want to know the value of, for example, let's see.

112
00:08:03,760 --> 00:08:08,460
Okay, so it's telling me that "a" is not defined right? So let's keep that in mind.

113
00:08:08,460 --> 00:08:13,480
I've defined a function here "func", that takes two arguments "x" and "y"

114
00:08:13,480 --> 00:08:18,720
and it computes "y" as "a" times "x" plus "b" and returns that

115
00:08:18,720 --> 00:08:23,980
So let's try to run this cell which will compile this function.

116
00:08:23,980 --> 00:08:28,060
Okay, so as you can see, despite

117
00:08:28,060 --> 00:08:33,640
"a" not being defined, python did not spit out an error.

118
00:08:33,640 --> 00:08:37,580
when I ran this cell, so that's something you should always keep in mind.

119
00:08:37,580 --> 00:08:41,540
when working with Jupyter notebook. However

120
00:08:41,540 --> 00:08:46,560
if I were to now try to call this function with arguments as I do in this cell here,

121
00:08:46,560 --> 00:08:50,080
let's see what happens.

122
00:08:50,080 --> 00:08:56,900
Okay, now it spits out a NameError. Yes, so it's worth noting that

123
00:08:56,900 --> 00:09:02,440
your errors will only show up at runtime if you have something like this.

124
00:09:02,440 --> 00:09:06,220
This is the source of errors in multiple situations.

125
00:09:06,220 --> 00:09:11,440
Okay, so let's go ahead and actually try to define "a" and "b" here with this cell.

126
00:09:11,440 --> 00:09:14,540
And run this again

127
00:09:14,540 --> 00:09:17,840
Okay so we have 35.

128
00:09:17,840 --> 00:09:22,220
So this is also something to keep in mind. If you have

129
00:09:22,220 --> 00:09:28,360
some variables you were using and you mistakenly assume that their scope is local

130
00:09:28,360 --> 00:09:32,540
but they are getting changed outside the function, python is not going to

131
00:09:32,540 --> 00:09:36,620
help you there. Jupyter notebook holds the state of these variables and

132
00:09:36,620 --> 00:09:41,280
this function will execute, so if I change the state to 6,

133
00:09:41,280 --> 00:09:46,240
i.e. change "a" to 6 and "b" to 10 here,

134
00:09:46,240 --> 00:09:50,480
ran this cell and then ran "func(3,3)" again,

135
00:09:50,480 --> 00:09:55,000
it's going to give me a different. Yeah, so this is just something to keep in mind.

136
00:09:55,000 --> 00:10:00,580
So a useful thing to do in some situations,

137
00:10:00,580 --> 00:10:05,600
is to restart your kernel, delete all the state,

138
00:10:05,600 --> 00:10:09,560
and clear all the output. You can do that by going to Kernel >

139
00:10:09,560 --> 00:10:13,600
Restart & Run All. Let's try that.

140
00:10:13,600 --> 00:10:17,880
Okay, and it's warning us that all our variables and outputs will be lost.

141
00:10:17,880 --> 00:10:21,120
We know this, so let's go ahead and do that.

142
00:10:25,920 --> 00:10:29,960
It gave us a NameError here, we don't really need this cell so let's delete it.

143
00:10:29,960 --> 00:10:33,960
Here we can go up here to the scissors and just cut the cell.

144
00:10:33,960 --> 00:10:39,160
Okay, and then we can run the rest of these.

145
00:10:39,160 --> 00:10:43,700
Okay, so we have a brief intro to state in Jupyter notebook.

146
00:10:45,500 --> 00:10:50,020
Now let's talk about something you might see in some of the assignments in this course.

147
00:10:50,020 --> 00:10:53,780
Namely, Jupyter magics.

148
00:10:53,780 --> 00:11:01,320
Jupyter has some integration with libraries like matplotlib that make it easier to

149
00:11:01,320 --> 00:11:05,700
plot and interact with plots

150
00:11:05,700 --> 00:11:11,380
right here on the notebook, and one such integration is called a magic.

151
00:11:11,380 --> 00:11:15,820
and it looks something like this. There is a matplotlib

152
00:11:15,820 --> 00:11:21,300
magic, which is this % sign followed by matplotlib.

153
00:11:21,300 --> 00:11:25,540
This specific magic here is called "inline".

154
00:11:25,540 --> 00:11:28,480
What inline does is

155
00:11:28,480 --> 00:11:32,620
Let's go ahead and clear the output of this cell.

156
00:11:32,620 --> 00:11:35,760
We have a...

157
00:11:35,760 --> 00:11:40,260
We're going to go ahead and import matplotlib.pyplot as plt,

158
00:11:40,260 --> 00:11:46,740
make a dataset real quick and then we're going to try to plot the dataset right here.

159
00:11:46,740 --> 00:11:51,360
And what "inline" does is allow this plot to show up right here

160
00:11:51,360 --> 00:11:54,100
just below the cell

161
00:11:54,100 --> 00:12:00,360
Yeah, and the other matplotlib magic is called "notebook".

162
00:12:00,360 --> 00:12:05,040
This particular one isn't something that

163
00:12:05,040 --> 00:12:09,740
we're using here, but you will see this as some of the plots you create in this

164
00:12:09,740 --> 00:12:14,660
class will be interactive. You will have to interact with them, so

165
00:12:14,660 --> 00:12:18,360
that's when matplotlib notebook will come in handy.

166
00:12:18,360 --> 00:12:23,060
Okay, and the last thing that I'm going to be covering here is

167
00:12:23,060 --> 00:12:28,640
how to save your Jupyter notebook as a PDF. There is two ways but,

168
00:12:28,640 --> 00:12:32,960
I'm going to cover the simplest one here, so

169
00:12:32,960 --> 00:12:39,480
the bare minimum and I think the easiest one is just to use your browser's print functionality.

170
00:12:39,480 --> 00:12:44,040
So you can just go to File > Print

171
00:12:44,040 --> 00:12:50,040
and that should give you a "Save as PDF" option.

172
00:12:50,040 --> 00:12:57,640
Just make sure that none of your figures are cut-off and just hit save here.

173
00:12:57,640 --> 00:13:03,000
So this should be the way to do it. There is also another way

174
00:13:03,000 --> 00:13:07,160
native to Jupyter notebook here. You can go to File >

175
00:13:07,160 --> 00:13:12,000
Download as and then try PDF via LaTex

176
00:13:12,000 --> 00:13:17,360
but this does have some dependencies that you may have to figure out.

177
00:13:17,360 --> 00:13:22,280
It's just generally a little complicated and involves

178
00:13:22,280 --> 00:13:27,480
messing around with the versions, but I would look at the Jupyter docs for that.

179
00:13:27,480 --> 00:13:30,640
There are also some interesting tutorials online on how to

180
00:13:30,640 --> 00:13:33,980
convert your notebook to a PDF.

181
00:13:33,980 --> 00:13:40,700
Okay, thank you and let me know if you guys have any questions on Piazza about Jupyter notebook.