Hi everyone, thanks for joining me today to learn about
VS Code's
implementation of a Jupyter notebook and all the great new
features we have in store that will improve your data
science workflow.
Before we get started, let's go through some overview of
today's walkthrough.
So what we will we be going over today?
We're going to go over what you will need,
what is a Jupyter notebook,
why all the changes, why is VS Code investing in
these changes, and how to get started in VS Code
and what features are supported for the notebook.
So first things first, what will you need? In order
to follow along
today you're going to need the VS Code Insiders build.
This experience is not yet available in the stable build
of VS Code.
You'll also need the Python extension and the Jupyter extension
as well as Gather.
You can actually install these extensions within VS Code itself
by coming to the left side panel of VS Code
and clicking on the extensions tab.
Here you can see all of your extensions that are
currently installed as well as the ability to search within
the VS Code marketplace.
This is where you can grab those extensions I just
mentioned.
So what is a Jupyter notebook?
A Jupyter notebook is an interactive programming and computational document
that mixes executable code,
equations, visualizations, and narrative text.
It's the ability to have all four of these components
within one document that has caused the Jupyter notebook to
become the de facto tool for data science where all of
these are very, very necessary for data analysis and data
exploration.
Why the changes? So our previous implementation of notebooks used
to be a web view and now we're migrating towards
natively supporting the ipynb file type.
What does that mean for users?
That means users can now benefit from the integration with
other VS Code extensions from our marketplace such as Vim
keyboard shortcuts or bracket colorizer.
Those are two very popular ones.
Users can also see improved notebook load times and a
new modern design.
So how do we get started with notebooks in VS
Code?
You can create a new notebook by bringing up the
command palette.
You can do so by holding down Ctrl +
+ Shift + P if you're on Windows,
or CMD + Shift + P if you're on a Mac and
typing in the keyword notebook.
Here the create new blank notebook command has popped up
and we can select it.
If you get a dropdown option,
you can select Jupyter to create a new one.
The first thing we want to do is make sure
that we're working with the correct environment so we can
come down to the global status bar and select our
kernel. I'm going to go ahead and activate my Anaconda
based environment for this notebook.
I'm working with a Python cell as indicated by the
language picker in the bottom right,
and I can click here to navigate back and forth
between Markdown in a code cell.
So earlier I mentioned a bit about the four components
in a Jupyter notebook being executable code,
equations, visualizations and narrative text.
Let me show you some basic example of those for
some newcomers.
So if I come to the input of the cell,
I can go ahead and set the value of X
to 42.
I can run this cell by clicking the run icon
to the left.
The cell status bar is going to show me about
how long it took to run.
I'm going to add another code cell.
And I'm actually going to print this value to view
it. Here
you see I'm getting some IntelliSense autocompletion suggestions and to
accept the suggestion I can hit tab.
And as I continue to write and open these brackets,
I'm going to get the function definition.
This is really helpful because I don't have to go
to Google and search what certain functions accept these parameters.
That's going to pop up as I work and make
it a lot easier for me to write my code.
So let's go ahead and input x here and run
it again.
Now I could run it by clicking the icon once
more,
or I could use the Jupyter shortcut keys that are
supported such as Shift + Enter. In here we have our
output that displays 42.
The cool thing about Jupyter notebooks is that I can
actually override certain cells and run them out of order.
So here I'm going to set X equals to 15
and come back to the second cell.
Run it once more and the output has changed from
42 to 15.
Let's talk a bit about those equations.
I can add a markdown cell and double click to
edit and enter something such as X^2 and click
shift + enter to run it.
This is going to render the LaTeX formula for me.
I can also show you an example of something a
bit more complex,
and as you can see here,
this has lit up red,
which is indicating to me this is not a markdown,
so I'm going to go ahead and switch it
to markdown. There we go.
Then let's do something maybe a bit more complicated.
We can write out, for example,
Euler's... that's completed for me.
Euler's identity, and here we have another LaTeX formula.
So next on to visualizations. For the sake of time
I have gone ahead and copied the code that I wrote
earlier.
And I'm going to go ahead and fix this.
Actually, I have a little typo here.
Perfect, now we have some of these values.
I'm going to go ahead and generate a histogram.
I can run the cell once more and this is
a really cool example of when I'm going to have
an input and an output.
As you can see here,
I've generated a really simple visualization.
As you can imagine, these can become much more detailed
and complex.
And that last component from earlier is narrative text.
Let's add one more markdown cell and show an example
of narrative text.
"My first histogram".
And those are the four basic notebook components.
What other features are going to be supported in native
notebooks?
I'm going to migrate to the Titanic notebook and this
notebook predicts survivors based on input.
Something that happens fairly often in notebooks, due again to
their playground
nature, is that the state of certain variables get lost
as we start to overwrite and rerun cells.
What we've created in order to alleviate that is the
variable explorer.
You can open it in the top right toolbar by
clicking the icon and what that's going to do is
generate the list of all the active variables within this
notebook. You can see the name,
the type, the size and the value,
and this is an essential feature when it comes to
debugging.
You can watch these variables update live.
You can also take a closer look at your data
from the variable explorer by opening it in the data
viewer.
You can click the icon to the left of a
variable.
What that's going to do is bring up the data
viewer.
This will open up an Excel like view of any
tabular data you have within your notebook,
and it makes it much easier to parse and understand.
Tfilter row at the top can help you identify
issues in your data that can then lead to steps
to take to clean your data, so the data viewer
will help you identify any issues within your data set
much quicker as opposed to writing code to identify issues
and then writing more code to resolve them.
Let's go back to our notebook and keep scrolling down
to see what else is supported.
Here we reached an ipywidget.
Native notebook supports ipywidgets which are powerful interactive visualizations
that can really help bring a notebook to life.
These widgets usually have GUI elements such as buttons or
sliders,
and can help turn notebooks even to a dynamic dashboard.
Here we have pandas profiling,
which also has some button elements. An issue that can
occur with ipywidgets or even other general notebook outputs
is that they can get quite long and take up
a lot of your window space. In order to make
sure that users can
focus on the most relevant output and input at the
time that they want,
we've introduced the ability to collapse both input and output.
You can find it in the cell toolbar
"More actions" and clicking it will bring the drop down
menu.
You can then select collapse so output or collapse cell
input and the shortcuts are located to the right.
You can also double click to the left of an
output to collapse it.
A collapsed output will be indicated by an icon to
the left and to reopen it you can simply click
it.
Now we reached the data cleaning portion.
Let me go ahead and bring this a bit more
into view.
So here we have 4 code cells that are performing
some data cleaning actions and really piggybacking off of the
last thing we talked about about making sure users only
have the most relevant code in front of them at
the right time,
we've introduced the collapse feature to the left of markdown
cells.
Clicking this chevron to the left is going to collapse
all of those code cells under this markdown in order
to ensure that if I'm scrolling through this notebook,
I don't have to be distracted by any other code
that is not relevant right now.
Clicking the chevron once more will display those code cells.
Now we're going to actually breeze through this next feature
engineering section because the next thing I want to show
you is at the bottom.
So here towards the bottom we can see two of
my cells actually ended up in the wrong spot.
This first cell is splitting inputs and outputs and this
second cell is splitting my testing and training data.
I want to make sure that they are under the
test train
split markdown of my code.
So what I can do is I can hover to
the left of the cell and click to select this
first cell.
If I want to add other cells to my selection,
I can hold down Ctrl and click to the left
of those cells as well.
You can also use the traditional shift + click.
Now I can go ahead and hover over any select
itself,
click and drag as indicated by the blue line.
I'm going to place it under the test train
split markdown cell, and now here we have the two
moved cells in their correct location.
Moving from here through the end are the models that
we use to predict survivors.
The Naive Bayes classifier, the decision tree model,
as well as a random forest.
The last cell actually compares all of the accuracy scores
of these models.
So let's say we want to move forward with the
one that was most accurate.
That appears to be the random forest with all attributes.
So to go back to that cell,
it could scroll through in the notebook and make sure
I don't miss it again,
or I can actually go ahead and use the table
of contents to navigate.
To use table of contents in native notebooks,
make sure you're in the File Explorer tab in the
top left and then come down to the outline section
at the bottom left.
Clicking that will then show you the markdown headers that
are in your notebook,
and you can use those to navigate through.
As you can see, these are all the markdowns I
have in my notebook,
and I can click random forest to return to that
spot.
And here we have our random forest.
If you're somebody who likes to see code in your
table of contents,
you can actually customize this via setting.
So go ahead and bring up the command palette one
more time and type in "settings".
You'll want to click the first option: UI.
And type in notebook outline.
Oops... outline, there we go. And then you'll want to
enable the first setting
that is show code cells.
This will enable code cells in your outline.
Let me show you what that looks like now.
Heading back to the notebook now my table of contents
has all of my code cells.
This is especially helpful if you're somebody who comments on
your code a lot.
Now back to our original task.
We wanted to go ahead and move forward with the
random forest model.
So if I want to go ahead and do this
today and clean up this notebook,
I have a couple options.
I can start copying and pasting cells into a new
notebook and hope I don't leave anything important behind.
Or I could be brave and try and delete cells
within this notebook and hope I don't delete the wrong
thing.
So what we've introduced to alleviate this
error prone and tedious process of cleaning notebooks is an
exploratory extension called Gather.
Gather is indicated by the icon located in the cell
toolbar here.
And what gather does is it's going to take all
of the necessary lines of code that will actually generate
a certain cell or a certain cells output.
In this case, we want the random forest output,
so I'm going to go ahead and click Gather.
What that's going to do is generate our gathered notebook.
As I mentioned, this is only going to have the
relevant lines of code to create that random forest model.
It's not going to have lines of code that contribute
to the decision tree or the Naive Bayes classifier,
for example. Now you may not necessarily want your final
output to be a notebook.
You may want this production ready in a script. So
you can go ahead and bring up the command palette
an go ahead and select the settings option once more.
Type in Gather.
And we'll want to look at the second option that
allows you to gather your code to script.
This way, instead of having a notebook,
you will have a Python script that's ready to go.
Let's go ahead and close these.
And coming back to our original notebook,
we've reached the end. Whenever you're happy with the state
of your notebook and you're ready to share it,
you can do so using the export feature,
which is located in the top right toolbar.
Clicking this icon will give you the three options for
export.
You can export to Python script for production ready, export
to HTML,
or even go ahead and export to PDF.
All for easy sharing purposes.
I hope you guys enjoyed seeing this quick walkthrough of
the features we're enabling in native notebooks and go try
them out today.
Follow me on Twitter @claudiaregio for more Jupyter
notebooks,
related announcements and for any feedback or feature requests,
don't hesitate to file issues on our VS Code Jupyter
GitHub repo.
Thanks!