28 Jupyter Notebook Tips, Tricks, and Shortcuts
28 Jupyter Notebook Tips, Tricks, and Shortcuts
Jupyter Notebook
Jupyter notebook, formerly known as the IPython notebook, is a flexible tool that helps
you create readable analyses, as you can keep code, images, comments, formulae and
plots together. In this post, we’ve collected some of the top Jupyter notebook tips to
quickly turn you into a Jupyter power user!
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
(This post isFor
based on a post
a limited time,that
saveoriginally appearedPremium!
big on Dataquest on Alex Rogozhnikov’s
GET 50%blog,
OFF
‘Brilliantly Wrong’. We have expanded the post and will continue to do so over time — if
you have a suggestion please let us know. Thanks to Alex for graciously letting us
republish his work here.)
Jupyter is quite extensible, supports many programming languages and is easily hosted
on your computer or on almost any server — you only need to have ssh or http access.
Best of all, it’s completely free. Now let’s dive in to our list of 28 (and counting!) Jupyter
notebook tips!
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Project Jupyter was born out of the IPython project as the project evolved to become a
notebook that could support multiple languages – hence its historical name as the
IPython notebook. The name Jupyter is an indirect acronyum of the three core languages
it was designed for: JUlia, PYThon, and R and is inspired by the planet Jupiter.
When working with Python in Jupyter, the IPython kernel is used, which gives us some
handy access to IPython features from within our Jupyter notebooks (more on that later!)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
We’re goingFor
to show youtime,
a limited 28 tips andbig
save tricks to make your
on Dataquest life workingGET
Premium! with50%
Jupyter
OFF easier.
1. Keyboard Shortcuts
As any power user knows, keyboard shortcuts will save you lots of time. Jupyter stores a
list of keybord shortcuts under the menu at the top: Help > Keyboard Shortcuts , or
by pressing H in command mode (more on that later). It’s worth checking this each time
you update Jupyter, as more shortcuts are added all the time.
Another way to access keyboard shortcuts, and a handy way to learn them is to use the
command palette: Cmd + Shift + P (or Ctrl + Shift + P on Linux and
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Windows). This dialog box helps you run any command by name – useful if you don’t
For a limited time, save big on Dataquest Premium! GET 50% OFF
know the keyboard shortcut for an action or if what you want to do does not have a
keyboard shortcut. The functionality is similar to Spotlight search on a Mac, and once you
start using it you’ll wonder how you lived without it!
Some of my favorites:
Esc will take you into command mode where you can navigate around your
A to insert a new cell above the current cell, B to insert a new cell below.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
D + D (press the key twice) to delete the current cell
For a limited time, save big on Dataquest Premium! GET 50% OFF
Enter will take you from command mode back into edit mode for the given cell.
Shift + Tab will show you the Docstring (documentation) for the the object you
have just typed in a code cell – you can keep pressing this short cut to cycle through
a few modes of documentation.
Ctrl + Shift + - will split the current cell into two from where your cursor is.
Esc + F Find and replace on your code but not the outputs.
Once cells are selected, you can then delete / copy / cut / paste / run them as
a batch. This is helpful when you need to move parts of a notebook.
You can also use Shift + M to merge multiple cells.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
What is known less, is that you can alter a modify the ast_note_interactivity
kernel option to make Jupyter do this for any variable or statement on its own line, so you
can see the value of multiple statements at once.
quakes = data('quakes')
quakes.head()
quakes.tail()
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
lat long depth mag stations
For a limited time, save big on Dataquest Premium! GET 50% OFF
1 -20.42 181.62 562 4.8 41
2 -20.62 181.03 650 4.2 15
3 -26.00 184.10 42 5.4 43
4 -17.97 181.66 626 4.1 19
5 -20.42 181.96 649 4.0 11
If you want to set this behaviour for all instances of Jupyter (Notebook and Console),
simply create a file ~/.ipython/profile_default/ipython_config.py with the
lines below.
c = get_config()
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
3. Easy links to
documentation
Inside the Help menu you’ll find handy links to the online documentation for common
libraries including NumPy, Pandas, SciPy and Matplotlib.
Don’t forget also that by prepending a library, method or variable with ? , you can access
the Docstring for quick reference on syntax.
?str.replace()
Docstring:
S.replace(old, new[, count]) -> str
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
4. Plotting in notebooks
There are many options for generating plots in your notebooks.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Project resources
For a limited and
time, save big inspiration
on Dataquest Premium! Sign
GET
up50% OFF
now!
Industry news and updates
%lsmagic
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
%pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch
For a limited time, save big on Dataquest Premium! GET 50% OFF
%psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall
%rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir
%run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias
%%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
I recommend browsing the documentation for all IPython Magic commands as you’ll no
doubt find some that work for you. A few of my favorites are below:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
# Running %env without any arguments
# lists all environment variables# The line below sets the environment
# variable
%env OMP_NUM_THREADS%env OMP_NUM_THREADS=4
env: OMP_NUM_THREADS=4
Note that using %run is not the same as importing a python module.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
# Before Running
%load ./hello_world.py
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
# After Running
# %load ./hello_world.py
if __name__ == "__main__":
print("Hello World!")
Hello World!
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Stored 'data' (str)
%store -r data
print(data)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
one = "for the money"
%%time will give you information about a single run of the code in your cell.
%%time
import time
for _ in range(1000):
time.sleep(0.01) # sleep for 0.01 seconds
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
CPU times: user 21.5 ms, sys: 14.8 ms, total: 36.3 ms Wall time: 11.6
s
%%timeit uses the Python timeit module which runs a statement 100,000 times (by
default) and then provides the mean of the fastest three times.
import numpy
%timeit numpy.random.normal(size=100)
The slowest run took 7.29 times longer than the fastest. This could
mean that an intermediate result is being cached.
100000 loops, best of 3: 5.5 µs per loop
external script
Using the %%writefile magic saves the contents of that cell to an external
file. %pycat does the opposite, and shows you (in a popup) the syntax highlighted
contents of an external file.
%%writefile pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
Writing pythoncode.py
%pycat pythoncode.py
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
%prun some_useless_slow_function()
'mtrand.RandomState' objects}
1 0.006 0.006 0.556 0.556 :6(some_useless_slow_function)
6320 0.001 0.000 0.001 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.556 0.556 :1()
1 0.000 0.000 0.556 0.556 {built-in method exec}
%pdb
def pick_and_take():
picked = numpy.random.randint(0, 1000)
raise NotImplementedError()
pick_and_take()
--------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
in ()
5 raise NotImplementedError()
6
----> 7 pick_and_take()
in pick_and_take()
3 def pick_and_take():
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
----> 5 raise NotImplementedError()
For a limited time, save big on Dataquest Premium! GET 50% OFF
6
7 pick_and_take()
NotImplementedError:
> (5)pick_and_take()
3 def pick_and_take():
4 picked = numpy.random.randint(0, 1000)
----> 5 raise NotImplementedError()
6
7 pick_and_take()
ipdb>
x = range(1000)
y = [i ** 2 for i in x]
plt.plot(x,y)
plt.show();
plt.plot(x,y)
plt.show();
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
%matplotlib inline
from matplotlib import pyplot as plt
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
import numpyx = numpy.linspace(0, 1, 1000)**1.5
For a limited time, save big on Dataquest Premium! GET 50% OFF
(array([ 216., 126., 106., 95., 87., 81., 77., 73., 71., 68.]),
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
<a list of 10 Patch objects>)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
!ls *.csv
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-
packages pandas (0.18.1)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
This:
Becomes this:
Just use IPython Magics with the name of your kernel at the start of each cell that you
want to use that Kernel for:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium!
%%bash GET 50% OFF
%%HTML
%%python2
%%python3
%%ruby
%%perl
%%bash
for i in {1..5}
do echo "i is $i"
done
i is 1
i is 2
i is 3
i is 4
i is 5
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Once that’sFor
done, fire uptime,
a limited an R console
save bigand run the following:
on Dataquest Premium! GET 50% OFF
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec() # to register the kernel in the current R
installation
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
You can then use the two languages together, and even pass variables inbetween:
%load_ext rpy2.ipython
%R require(ggplot2)
array([1], dtype=int32)
})
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
%%R -i df ggplot(data = df) + geom_point(aes(x = X, y= Y, color =
Letter, size = Z))
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
But it is much better when this boring part is done for you, right?
You can write functions in cython or fortran and use those directly from python code.
%load_ext Cython
%%cython
def myltiply_by_2(float x):
return 2.0 * x
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
myltiply_by_2(23.)
Personally I prefer to use fortran, which I found very convenient for writing number-
crunching functions. More details of usage can be found here.
%load_ext fortranmagic
There are also di erent jitter systems which can speed up your python code. More
examples can be found here.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Multicursor support.
24. Jupyter-contrib
extensions
Jupyter-contrib extensions is a family of extensions which give Jupyter a lot more
functionality, including e.g. jupyter spell-checker and code-formatter .
The following commands will install the extensions, as well as a menu based configurator
that will help you browse and enable the extensions from the main Jupyter notebook
screen.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
!pip install https://github.com/ipython-
contrib/jupyter_contrib_nbextensions/tarball/master !pip install
jupyter_nbextensions_configurator !jupyter contrib nbextension install
--user !jupyter nbextensions_configurator enable --user
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
You can install
For RISE usingtime,
a limited conda:
save big on Dataquest Premium! GET 50% OFF
Or alternatively pip:
And then run the following code to install and enable the extension:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In this example
For aI limited
scan thetime,
folder with
save bigimages in my repository
on Dataquest Premium!and show
GET thumbnails
50% OFF of the
first 5:
import os
from IPython.display import display, Image names = [f for f in
os.listdir('../images/ml_demonstrations/') if f.endswith('.png')]
for name in names[:5]:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
We can create the same list with a bash command, because magics and bash calls return
python variables:
['../images/ml_demonstrations/colah_embeddings.png',
'../images/ml_demonstrations/convnetjs.png',
'../images/ml_demonstrations/decision_tree.png',
'../images/ml_demonstrations/decision_tree_in_course.png',
'../images/ml_demonstrations/dream_mnist.png']
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
pyspark
For a limited time, save big on Dataquest Premium! GET 50% OFF
spark-sql magic %%sql
Convert notebooks to html files using the File > Download as > HTML Menu
option.
Upload your .ipynb file to Google Colab.
Share your notebook file with gists or on github, both of which render the
notebooks. See this example.
If you upload your notebook to a github repository, you can use the
handy mybinder service to allow someone half an hour of interactive
Jupyter access to your repository.
Setup your own system with jupyterhub, this is very handy when you organize
mini-course or workshop and don’t have time to care about students machines.
Store your notebook e.g. in dropbox and put the link to nbviewer. nbviewer will
render the notebook from whichever source you host it.
Use the File > Download as > PDF menu to save your notebook as a PDF. If
you’re going this route, I highly recommend reading Julius Schulz’s excellent
article Making publication ready Python notebooks.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Create a blog using Pelican from your Jupyter notebooks.
For a limited time, save big on Dataquest Premium! GET 50% OFF
At Dataquest, our interactive guided projects use Jupyter notebooks to building data
science projects and get a job in data. If you’re interested, you can sign up and do our first
module for free.
IPython built-in magics
Nice interactive presentation about jupyter by Ben Zaitlen
Advanced notebooks part 1: magics
and part 2: widgets
Profiling in python with jupyter
4 ways to extend notebooks
IPython notebook tricks
Jupyter vs Zeppelin for big data
Making publication ready Python notebooks.
Josh Devlin
Data Scientist at Dataquest.io. Loves Data and Aussie Rules Football. Australian
living in Texas.
TAGS beginner, Jupyter, jupyter notebook, jupyter notebooks, Learn Python, Learn R, Resources, tips and tricks, tutorial, Tutorials
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save big on Dataquest Premium! GET 50% OFF
You Need Data Skills to Future-Proof Your Tutorial: Web Scraping with Python Using
Career Beautiful Soup
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For a limited time, save bigSign up now Premium!
on Dataquest GET 50% OFF
Or, visit our pricing page to learn about our Basic and Premium plans.
We are committed to protecting your personal information and your right to privacy. Privacy For Academia
Policy last updated June 13th, 2020 – review here. About Dataquest
Success Stories
Careers
Pricing
Contact Us
Community
Privacy Policy
Terms of Use
A iliate Program
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Blog
Resource List
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD