Hey guys, welcome to CS445 - Computational Photography. My name is Ashwin Ramesh,
and today I'll be doing a quick tutorial on how to use Jupyter Notebook.
It's a very important tool in python development, not just for the assignments in
this course, which are going to be distributed as python
notebooks, but for interactive prototyping in general.
I'll just briefly go over some installation options,
for Jupyter notebook, and then start on how to use it.
Okay, so these are the Jupyter notebook docs.
You should just be able to Google for these,
and the installation guide is right here, so go ahead and click right there,
and that should take you to this page.
There are two main options to install Jupyter
notebook based on what python package manager you
like to use. There is Anaconda,
and there is pip. I prefer to use pip.
It is quite lightweight and stable. It is the default
python package manager. So
I'll go ahead and do that. Let's just
copy paste this command here.
We will got to a terminal here.
Just clear up there. Okay, so
first thing is first, we'll just upgrade pip.
Should be brief.
Okay and now we will go ahead
and install jupyter.
So its always good to do this because
even if you already have jupyter because it will update any
related packages that jupyter needs.
Okay, and the way we will
start our jupyter notebook server is just by
navigating to the directory that we want to have the
notebook in. Here as you can see I have
it in a directory called CS445, and we'll just
run jupyter notebook.
What this will do is automatically open up a tab in your browser.
But let's navigate back to
the terminal page just in case it didn't do that.
At the bottom it should basically tell you
where
at what website, or at what URL it started the notebook server.
So you should just be able to visit this.
So here we are, we are at the filesystem tree
started by Jupyter notebook, we are going to go ahead and
create a new notebook so go to "New".
Jupyter notebook lets us pick the python kernal that
we want to use in this notebook. So we're going to go ahead and use
python 3. and that's going to give us a new
python 3 kernel notebook.
Okay so, here it says "Untitled". This is the
title of the notebook, or the notebook name. We can go ahead and change it to whatever
we want here. Let's call it "jupyter_tutorial".
So jupyter automatically saves every few seconds.
However, its good to save often, of course.
So the main parts of the jupyter notebook are the heading.
Here you have your toolbars.
And here you the code cells.
There are several types of cells in jupyter notebook,
But two main ones are : Markdown and Code cells.
You can actually change the cell type by clicking on the cell.
Here the cell is highlighted in green, and then
going to this dropdown menu and switching it to the type you want.
So these two are mainly the ones we're going to use.
Let's go to markdown. So in a markdown cell you can add headings,
You can add ordered lists. You can write any type of markdown that you want.
So here we have this, and you can have something like a table of contents.
It really doesn't matter. Okay, so the way you would run this
cell and render it, i.e. have jupyter notebook render it, would be
by selecting the cell and hitting run.
You could alternatively just hit Shift+Enter, but let's go ahead and do that now.
There we go we have rendered our markdown cell.
Okay so let's look at how to use a code cell now.
So let's do one of the first things you generally do
when you start a python application,
which are your imports, so let's import matplotlib.
Okay so we're going to type in some python code, click on our cell, and hit Shift + Enter or
click the run button. Here we go. Alright, perfect.
So, as you can see, Jupyter notebook executed the code:
import matplotlib, and it found that it was unable to do so.
It returned an error, it printed below the cell.
So this is generally how it works. You run the cell here and
the output of the command is rendered below the cell.
So why didn't it find matplotlib?
Likely, it's because I don't have matplotlib
installed for this python kernel
this is actually convenient for this tutorial because
usually we would have to go to the terminal and do a pip install or conda install of matpplotlib
but Jupyter notebook actually allows us to run shell commands right here from the notebook.
The way we would do this is to go to a code cell,
insert an exclamation point, and we would
then type our shell command. So here I would do a
pip3 install matplotlib.
Okay so let's go ahead and do a Shift + Enter for this.
And as you can see, there is a star here by the
"In [*]", that means that the command is currently executing.
Once it's done, this will turn into a number.
There we go, so just finished, and now what we can do is
go back to our initial cell, click our initial cell and
we will hit Shift + Enter, try to import matplotlib again and this time,
the command ran successfully. Import commands don't really return any
output to stdout or stderr, so we receive nothing.
Another thing you can do between these two cells, since
the next time I use this notebook, if I want to run
cells consecutively I want the "pip3 install" to be first. I can just rearrange
these two cells. So I can navigate to this cell number 2 and just click this
up arrow which basically stands for moving this
selected cell up, so let's go ahead and do that.
Here we go, okay so here I am.
Perfect, so next let's talk about state in Jupyter notebook,
and some common pitfalls that I've seen students have.
Okay so I have a cell here, and
I have a variable called "a" that I want to know the value of, for example, let's see.
Okay, so it's telling me that "a" is not defined right? So let's keep that in mind.
I've defined a function here "func", that takes two arguments "x" and "y"
and it computes "y" as "a" times "x" plus "b" and returns that
So let's try to run this cell which will compile this function.
Okay, so as you can see, despite
"a" not being defined, python did not spit out an error.
when I ran this cell, so that's something you should always keep in mind.
when working with Jupyter notebook. However
if I were to now try to call this function with arguments as I do in this cell here,
let's see what happens.
Okay, now it spits out a NameError. Yes, so it's worth noting that
your errors will only show up at runtime if you have something like this.
This is the source of errors in multiple situations.
Okay, so let's go ahead and actually try to define "a" and "b" here with this cell.
And run this again
Okay so we have 35.
So this is also something to keep in mind. If you have
some variables you were using and you mistakenly assume that their scope is local
but they are getting changed outside the function, python is not going to
help you there. Jupyter notebook holds the state of these variables and
this function will execute, so if I change the state to 6,
i.e. change "a" to 6 and "b" to 10 here,
ran this cell and then ran "func(3,3)" again,
it's going to give me a different. Yeah, so this is just something to keep in mind.
So a useful thing to do in some situations,
is to restart your kernel, delete all the state,
and clear all the output. You can do that by going to Kernel >
Restart & Run All. Let's try that.
Okay, and it's warning us that all our variables and outputs will be lost.
We know this, so let's go ahead and do that.
It gave us a NameError here, we don't really need this cell so let's delete it.
Here we can go up here to the scissors and just cut the cell.
Okay, and then we can run the rest of these.
Okay, so we have a brief intro to state in Jupyter notebook.
Now let's talk about something you might see in some of the assignments in this course.
Namely, Jupyter magics.
Jupyter has some integration with libraries like matplotlib that make it easier to
plot and interact with plots
right here on the notebook, and one such integration is called a magic.
and it looks something like this. There is a matplotlib
magic, which is this % sign followed by matplotlib.
This specific magic here is called "inline".
What inline does is
Let's go ahead and clear the output of this cell.
We have a...
We're going to go ahead and import matplotlib.pyplot as plt,
make a dataset real quick and then we're going to try to plot the dataset right here.
And what "inline" does is allow this plot to show up right here
just below the cell
Yeah, and the other matplotlib magic is called "notebook".
This particular one isn't something that
we're using here, but you will see this as some of the plots you create in this
class will be interactive. You will have to interact with them, so
that's when matplotlib notebook will come in handy.
Okay, and the last thing that I'm going to be covering here is
how to save your Jupyter notebook as a PDF. There is two ways but,
I'm going to cover the simplest one here, so
the bare minimum and I think the easiest one is just to use your browser's print functionality.
So you can just go to File > Print
and that should give you a "Save as PDF" option.
Just make sure that none of your figures are cut-off and just hit save here.
So this should be the way to do it. There is also another way
native to Jupyter notebook here. You can go to File >
Download as and then try PDF via LaTex
but this does have some dependencies that you may have to figure out.
It's just generally a little complicated and involves
messing around with the versions, but I would look at the Jupyter docs for that.
There are also some interesting tutorials online on how to
convert your notebook to a PDF.
Okay, thank you and let me know if you guys have any questions on Piazza about Jupyter notebook.