Are you interested in working with Jupyter notebooks, but you
just don't even understand all that they can do? Then
you've come to the right place, because today I'm going
to walk you through using Jupyter notebooks in VS code.
I'm going to be using a public GitHub repo so
you can follow along right with me. This is the
repo that we're going to be working with. It was
created by Claudia, a PM at Microsoft, and to follow along
you can go ahead
and find it at this link.
If you scroll down to the readme, this is going
to detail some of the setup steps that you're going
to need to have this smoothly run within VS Code. Specifically,
you're going to need to install Anaconda and run a
couple of commands and then connect to that conda environment from
within VS code. Now I won't be detailing that set
up in this video, but you're in luck because we
do have another video that walks you through setting up
Anaconda with your Jupyter notebooks. And just a quick note,
you don't have to use Anaconda to run Jupyter notebooks
in VS Code, but it does help
in a lot of instances where your notebook might require
a lot of dependencies or common Python packages, so in
this instance it's really helpful. All right, so now let's
head on over to VS Code and take a look at
this notebook. So for this to run in my VS Code
setup, I need to have the Python extension installed, and
in turn that's going to also install the Jupyter notebook
extension. And then I'm going to navigate over to the
Titanic notebook file, and I also am already connected to
the conda environment that I created with that initial setup,
and that's going to be called the golden scenario environment.
Before we start diving into running this notebook, I want
to do a really quick overview of the notebook experience
in VS Code and what this specific notebook does. So not
the most upbeat of topics, but this notebook is going
to be using machine learning algorithms to try to predict
whether someone would survive the sinking of the Titanic based
on attributes in a data set.
If you take a look at our files, this Titanic
data CSV file is going to be the data set
that we are reading from. And yes, this is real
world data.
Now, looking at the notebook, there are several sections to
this notebook which you can see in this outline view
in the bottom left of the screen.
And if we take a quick scroll through these, we
can see that this outline is generated from the markdown
headers within this notebook. So if I click on one
of these headers, we can see that it is just
a markdown cell. And to show that rendered markdown you
can just hit this check mark here. Now when you're
working with Jupyter notebooks in VS Code, a lot of your
time is going to be spent with this top toolbar.
Up here, you can add a new code cell and
when I do that, it automatically detects that I'm using
Python as my language.
You can add a new markdown cell, which again is
going to show that markdown is the language in the
bottom right. You can run all cells, which we are
going to see in just a little bit. Clear the
outputs of all your cells. You can restart or interrupt
your kernel. You can open your variable explorer and your
outline view. And in the more actions, tere's a couple of
customizable options there and you can also export files. Finally,
in the top right you will see the kernel that
you are currently connected to and if you were to
click that, you could change that to the kernels
that you have available. I'm going to go ahead and
kick off running this notebook by clicking the Run All
button on this toolbar.
So there are a lot of different cells in this
notebook and I don't really want to have to scroll
through to figure out what cell is running. So you
can see that in this toolbar, as soon as I
started running cells, this nice Go To running cell button
popped up. So I just click that and it's going
to take me right to where the currently running cell
is. So a very quick way to navigate while you
are running a notebook with multiple cells.
Now that this is finished running, one thing that we
can do is open the variable explorer, and what this
is going to do is pull up an entire list
of all the variables that were defined during our notebook
run. So you can sort them by name, by type,
however makes it easiest for you to visualize all of
your variables. And the other cool thing is that we
have something called the data viewer. So if we take
a look at any of these variables that have this
little pop out icon next to it, we can
actually drill even deeper into that variable to see what
exists in that variable. So for example, I'm going to
take a closer look at this Titanic data frame,
click this pop out here, and the data viewer will
open in a new tab and you can see I
can now visualize this entire
variable, everything that is inside of it. I can sort
on various columns in the data frame. I can filter
on any of the different columns, so that way it
only gives us the data we're looking for. And really,
this is just a great way to drill even deeper
into the variables that you have access to from your
notebook. VS Code provides great data visualization functionality for things like
charts, graphs, and trees in Jupyter notebooks. I've skipped ahead
to the Decision Tree section of this notebook and if
we scroll down
just a little, you'll see we have this visualize_tree
method which prints out a nice image of our decision
tree. If you hover over then in the top right
you'll see the copy to clipboard option, the expand image,
or you can save this image to your file system.
I'm going to expand this image and we can see
that a new tab pops up with that image in
it, and when that pops up you can interact with
that image. You can zoom out, zoom in, and drag
around so that you can further inspect
everything that is in this decision tree. Alright, so let's
head on back to the notebook. So when I ran
this notebook, I did it using the Run All functionality,
which ran all the cells in the notebook. But you
probably don't always want to run every single cell in
your notebook. I'm going to show you a couple of
other ways that you can run your cell.
The first being that to the left you have this
nice little play icon which lets you just execute that
cell, and then over to the right here we have
a run by line option which, when I click that
icon, we can see that it highlights the current line
that I'm on and then I can either hit F10
or just continue hitting that run icon to then step through
that entire cell, so another really easy way to skip
through there. And then if you just want to stop
running line by line you can click the continue execution
to just go through the rest of the cell.
Other options you have here for running is this execute
above cell, so that's going to execute all the cells
above your currently highlighted cell, and then you have the
option to execute the current cell and all the cells
below. You can also do things like add breakpoints,
and then debug your cell, so that way you can
run through and it will stop on any breakpoints
that you have specified. Now if you ever want to
move any cells around it's very easy. All you would
do is just grab and hold this blue bar next
to the cell and then drag it to wherever you
need to go. So one more time I'm clicking this
blue bar, I'm going to drag this up and stick
it back up there. And then if you want to
collapse the cell, all you have to do is then
just single click that blue bar and that works for
both the cell
and that cell's outputs.
So I've been using the outline view in order to
easily navigate among this notebook, but that's not the only
navigation technique that we have available in your notebook. You
can also use search functionality. So I'm on a Windows
machine, so I'm going to hit control F, but you
can also hit command F if you're on a Mac,
and that's going to pop up this find navigation item
in the top right, and then I can just type
in whatever search term I have, and that's going to
give me various results.
And you can also filter this based off of what
exactly you want to be searching for. So right now
it's just going to be looking for that cell source
wherever that "decision" happens.
If we add the cell output into that filter, we
can see we now have 10 results instead of seven
results. And that's now going to also show anything in
the output and not just the source. And then we
can also add rendered markdown to that and we can
see again that changes the number of search results available
to us because we are changing that filter. Now, one
of the coolest things that we have available with VS Code
and Jupyter notebooks is Intellisense. So I'm going to create
a new code cell here and just show you what
that Intellisense looks like. So I can start typing
titanic_df.
You can see I'm already getting Intellisense popped up there.
You can just do dot and then let's say I
want to do group by. If I begin typing that,
it says oh hey, do you mean group by? Yep,
I sure do. I can sort and that will pop
up any sort method that I have available to me
on this variable and I can do things like getting
different values. So it just makes it really easy for
you to have that native Intellisense that's available to you
in normal Python programming, but it's available to you now
within these Jupyter Notebooks.
So it makes it a lot easier to track any
of your errors and just really quickly code up a
new cell. Alright, so I've made these changes to this
notebook file and I want to take a look at
what that looks like in the Source Control Explorer. So
VS Code does a great job of tracking changes made to
notebook files within source control. If we scroll down, the
first change that was tracked is this output, which just
resulted from some warnings I got when I ran through
the import statements, and this differs from the file checked
in, so it's
being tracked as a change. And it's really easy to
either revert this change with just a click, or I
can simply just collapse that output and continue through viewing
the differences.
We scroll down a little further, you can see this
new cell that I added is very clearly marked. You
can see that there's no cell before it and now
we have the green to indicate that this was a
cell that was added. So finally, after you've created this
data analysis, everything looks good, you might want to be
able to export it as a different file so that
you can easily share it with others. So I can
easily do that in VS Code with these three dots that
go to the more actions in the toolbar and clicking
the Export option and you can see I have been
prompted
to export it as a Python script, HTML or PDF
file. So this is a very quick and easy way
for you to save your notebook in a different format.
Now that's just the start of everything that you can
do with VS Code and Jupyter notebooks, from data visualization, to
easy navigation, to Intellisense. If you're working with Jupyter notebooks,
VS Code is the place to be. And we have plenty
of other Jupyter content on our channel, like 5 things
you didn't know that you can do with Jupyter notebooks.
So make sure to like this video and subscribe to
our channel so that you can stay in the loop
of all things VS Code. Happy coding!