[MUSIC PLAYING]
YUFENG GUO: In previous videos, you've
seen me running Python code live on screen
and showing the results.
On this episode of "AI Adventures,"
I'll show you how to use Jupyter notebooks to achieve
the same results.
The way I've been running Python code live on screen
is by using a Python package called Jupyter.
Jupyter is built on the IPython project
and allows for interactive Python
to be run right in your browser.
But it is so much more than that.
From bash commands, to special magics, and plugins,
Jupyter enhances the Python coding experience greatly.
If you're already using Jupyter, I
hope I can improve your workflow and show you some new tricks.
And if you're not using Jupyter yet, what are you waiting for?
The easiest way to install Jupyter
is by running pip install jupyter.
Though, if you use a packaged Python
distribution, such as Anaconda, you may already have it
installed.
So be sure to activate your Python environment first.
And let's dive in.
When running Jupyter locally, you'll
connect to a locally-running web server through your browser,
typically on port 8888.
Start your notebook by running Jupyter notebook
in your working directory.
And usually, Jupyter will just automatically open
in your browser once it starts up.
But if not, point your browser over to localhost 8888.
And if you don't already have a notebook that you want to open,
you can create one by clicking on New
and selecting Python 2 or Python 3,
depending on which version of Python
you have running in your environment.
Jupyter is quite flexible and can handle many languages
and file types.
Though, we'll just stick with Python for now.
Once you have a new notebook running,
you can write some Python code in the empty cell.
And just hit Control-Enter to run it.
We can also run all our typical Python code here,
just like you might write in a Python script.
The difference is that we can run it and see
the results right away.
Notice what happens when we run a cell with Control-Enter.
The brackets on the left side of the cell
shows an asterisk when a cell is running or queued to run,
and then shows a number once it's finished,
representing the order in which cells were run during a given
session starting at 1.
The results of the final line of a code cell
will be printed as an output of that cell
but only if the value is not stored to a variable.
So for example, if I import TensorFlow and then concatenate
its version with a string, the output is shown below the cell,
even though I didn't use the Print command.
And of course, you can use the Print command.
And it would work fine just as well.
Now, this is all very useful for tinkering and seeing
how something behaves.
Another fantastic feature of Jupyter notebooks
is the ability to show the docstring of a function
that you are calling by just pressing Shift-Tab.
This allows you to call a function
with the correct arguments without needing
to look up the full documentation
every single time.
This feature also works with your local custom functions.
So if you write good docstrings, you'll be rewarded.
When you have a lot of output, you
can reduce the amount of space it takes up
by clicking on the left-hand panel of the output, which
turns it into a scrolling window.
If you double click, the output will be collapsed entirely.
One cell is useful.
But really, we want to have many cells.
To add a cell, click the plus icon in the toolbar.
There are also some cell execution commands
which can lead to new cells being created for us.
If you press Shift-Enter, it will run the current cell
and then just highlight that next cell.
But if there is no new next cell,
then a new cell is created.
On the other hand, if you want to insert a new cell
immediately after a given cell, you
can use Alt-Enter to execute the cell
and then insert a new cell directly after.
Perhaps the biggest feature that I've left out so far
is the markdown support.
My first impression of Jupyter notebooks
was its ability to provide a great way
to both write code and describe the code I was writing.
The rich semantics of markdown allow
for researchers and educators alike to easily and cleanly
communicate thoughts and ideas.
But perhaps, most importantly, it
allows past you to tell future you
what a given code cell was supposed
to do in a way that can be much more expressive than using
comment blocks.
Sometimes I just want to do a quick check on how long
one training or evaluation cycle is taking to execute.
For an easy way to time your code,
start a cell with percent percent time.
And once the cell finishes executing,
it will print out how long it took to run that cell.
It's not precision atomic timing,
but it is a great way to get some solid first impressions
with very minimal effort.
If you want to run a command line command in your notebook,
the easiest way is to put an exclamation point
in front of the command.
Now, this is only really useful for one-off commands.
If you want to run a bunch of commands,
start a cell with percent percent bash
to cause the entire cell to be interpreted as a bash script.
A great use of this feature is to kick off TensorBoard.
Typically, running TensorBoard might
involve starting a new terminal window
and running it on the command line, which
is what we would typically do if we wanted
it to run for a long time.
But if we just want to spin it up, take a peek,
and close it back down, having it in a Jupyter notebook cell
isn't such a bad idea.
Plus, you'll never forget to run it,
since it's embedded in your workflow of notebook cells.
Notice that it will occupy your notebook.
See that asterisk?
So you won't be able to run anything else
while TensorBoard is running.
To stop it, click Interrupt Kernel.
The asterisk will go away.
And you'll get back control flow.
So there you have it, some of my favorite Jupyter features
and capabilities.
This is, of course, not a comprehensive discussion
of all of Jupyter's features.
I've just covered some of my favorite
and most frequently used here.
There are many, many more waiting for you to explore.
Thanks for watching this episode of "Cloud AI Adventures."
Be sure to subscribe to the channel
to catch future episodes right when they come out.
But for now, try out Jupyter notebooks and all
that they have to offer.