May 7, 2024

28 Jupyter Notebook Tips, Tricks, and Shortcuts

Jupyter Notebook

Jupyter Notebook is a powerful tool that allows you to create and share documents containing live code, equations, visualizations, and narrative text. It's an essential tool for data scientists, researchers, and anyone who wants to work with data interactively. In this post, we've collected some of the best Jupyter Notebook tips, tricks, and shortcuts to help you become a Jupyter power user in no time!

Whether you're a beginner or an experienced user, these tips will help you work more efficiently, save time, and unlock the full potential of Jupyter Notebook. From keyboard shortcuts to magic commands, debugging, and data visualization, we've got you covered. So, let's dive in and discover how to make the most of this incredible tool!

(This post is based on a post that originally appeared on Alex Rogozhnikov's blog, '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.)

interface-screenshot

The Jupyter interface.

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!)

Let's explore 28 tips and tricks to make your life working with Jupyter easier and more productive.

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 Windows).

This dialog box helps you run any command by name – useful if you don’t 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!

command-palette

The command palette.

Let's explore:

  • Esc will take you into command mode where you can navigate around your notebook with arrow keys.
  • While in command mode:
    • A to insert a new cell above the current cell, B to insert a new cell below.
    • M to change the current cell to Markdown, Y to change it back to code
    • D + D (press the key twice) to delete the current cell
  • 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.
  • Esc + O Toggle cell output.
  • Select Multiple Cells:
    • Shift + J or Shift + Down selects the next sell in a downwards direction. You can also select sells in an upwards direction by using Shift + K or Shift + Up.
    • 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.

multi-merge

Merging multiple cells.

2. Pretty Display of Variables

Jupyter Notebook only shows one output at a time, as shown below. In the example, only the last variable's output is shown.

from pydataset import data
quakes = data('quakes')
quakes.head(3)
quakes.tail(3)
lat
long
depth
mag
stations
996
-25.93
179.54
470
4.4
22
997
-12.28
167.06
248
4.7
35
998
-20.13
184.20
244
4.5
34

However, you can add this code below to show all outputs in the cell. Notice now that both variables are shown.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

from pydataset import data
quakes = data('quakes')
quakes.head(3)
quakes.tail(3)
lat
long
depth
mag
stations
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
lat
long
depth
mag
stations
996
-25.93
179.54
470
4.4
22
997
-12.28
167.06
248
4.7
35
998
-20.13
184.20
244
4.5
34

If you want to set this behavior for all instances of Jupyter (Notebook and Console), simply create a file ~/.ipython/profile_default/ipython_config.py with the following lines:

c = get_config()
# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"

This Jupyter Notebook tip demonstrates how to display multiple outputs in a single cell, which can be especially useful when working with Pandas DataFrames or comparing the results of different variables.

3. Easy Access to Documentation

Jupyter Notebook makes it easy to access documentation for commonly used libraries without leaving the notebook interface. Under the Help menu, you'll find convenient links to the online documentation for popular libraries such as NumPy, Pandas, SciPy, and `Matplotlib.



help-tab

In addition to the Help menu, you can quickly access documentation for a specific function, method, or variable by prepending it with a question mark (?).

This will display the Docstring, providing a brief explanation of the object's syntax and functionality.

?str.replace
Docstring:
S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.
Type:      method_descriptor

4. Plotting in notebooks

There are many options for generating plots in your notebooks.

  • matplotlib (the de-facto standard), activated with %matplotlib inline – Here’s a Dataquest Matplotlib Tutorial.
  • %matplotlib notebook provides interactivity but can be a little slow, since rendering is done server-side.
  • Seaborn is built over Matplotlib and makes building more attractive plots easier. Just by importing Seaborn, your matplotlib plots are made ‘prettier’ without any code modification.
  • mpld3 provides alternative renderer (using d3) for matplotlib code. Quite nice, though incomplete.
  • bokeh is a better option for building interactive plots.
  • plot.ly can generate nice plots – this used to be a paid service only but was recently open sourced.
  • Altair is a relatively new declarative visualization library for Python. It’s easy to use and makes great looking plots, however the ability to customize those plots is not nearly as powerful as in Matplotlib.
bokeh

The Jupyter interface.

5. IPython Magic Commands

Jupyter Notebook is built on the IPython kernel, which means it has access to a wide range of powerful IPython Magic commands.

These commands can greatly enhance your productivity and streamline your workflow. You've already seen an example of a magic command earlier in this article: %matplotlib inline, which enables inline plotting within the notebook.

To view a list of all available magic commands, simply run the following code in a cell:

%lsmagic

This will output a list of both line magics and cell magics:

Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed 
edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv%notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision ...

Some of these magic commands can be incredibly useful for debugging, profiling, and working with different programming languages within the notebook interface.

We encourage you to explore the official IPython Magic documentation to learn more about the available commands and how they can be used to enhance your Jupyter Notebook tips experience.

In the following tips, we'll cover some of the most useful and commonly used IPython Magic commands to help you become a more efficient and productive Jupyter Notebook user.

6. IPython Magic – %env: Set Environment Variables

When working with certain libraries in Jupyter Notebook, such as Theano, you may need to set environment variables to control their behavior. Instead of restarting the Jupyter server process each time you need to modify an environment variable, you can use the %env magic command to manage them directly within the notebook.

To list all current environment variables, simply run %env without any arguments:

%env

To set an environment variable, use the following syntax:

%env VARIABLE_NAME=value

For example, to set the OMP_NUM_THREADS environment variable to 4, you would run:

%env OMP_NUM_THREADS=4

This Jupyter Notebook tip demonstrates how to efficiently manage environment variables using the %env magic command, saving you time and effort when working with libraries that rely on environment variables for configuration.

7. IPython Magic – %run: Execute Python Code and Jupyter Notebooks

The %run magic command allows you to execute Python code from external .py files and other Jupyter Notebooks directly within your current notebook. To execute a Python file, simply provide the path to the file:

%run path/to/your/script.py

To execute another Jupyter Notebook, provide the path to the .ipynb file:

%run path/to/your/notebook.ipynb

For example, to execute a notebook named two_histograms.ipynb located in the same directory as your current notebook, use:

%run ./two_histograms.ipynb

two-hists

Keep in mind that %run executes the code in the current namespace, which is different from importing a Python module.

This Jupyter Notebook tip demonstrates how you can use the %run magic command to execute external code and notebooks, making it easier to organize and reuse code across your projects.

Here's the updated version of the tip:

8. IPython Magic – %load: Insert Code from External Scripts

The %load magic command allows you to insert code from an external script into the current cell of your Jupyter Notebook. This can be useful when you want to quickly import and run code from a file without manually copying and pasting it.

To use %load, simply provide the path to the script you want to insert:

%load path/to/your/script.py

For example, if you have a file named hello_world.py in the same directory as your notebook, you can insert its code using:

%load ./hello_world.py

Before running the %load command, your cell might look like this:

%load ./hello_world.py

After running the command, the contents of the hello_world.py file will replace the %load line in the cell:

if __name__ == "__main__":
    print("Hello World!")

When you run the cell, it will execute the inserted code:

Hello World!

In addition to local files, you can also use %load with URLs to insert code from online sources.

These Jupyter Notebook tips showcase how the %load magic command can streamline your workflow by allowing you to quickly insert code from external scripts into your notebook cells.

You're right. Let's remove the repetitive part and keep the rest of the content focused on the specific tip. Here's the updated version:

9. IPython Magic – %store: Pass Variables Between Notebooks

The %store magic command allows you to pass variables between different notebooks. This can be incredibly useful when working on complex projects that span multiple notebooks or when you need to share data between collaborators.

To store a variable for use in another notebook, simply use the %store command followed by the variable name:

data = 'this is the string I want to pass to a different notebook'
%store data

After storing the variable, you can delete it from the current notebook's namespace using the del command:

del data

The output will confirm that the variable has been stored:

Stored 'data' (str)

To retrieve the stored variable in another notebook, use the %store -r command followed by the variable name:

%store -r data
print(data)

The output will display the value of the retrieved variable:

this is the string I want to pass to a different notebook

These Jupyter Notebook tips showcase how the %store magic command facilitates seamless variable sharing between notebooks, enhancing collaboration and making it easier to work with complex, multi-notebook projects.

10. IPython Magic – %who: List Variables in the Global Scope

The %who magic command, allow you to list all variables currently existing in the global scope. This can be helpful when you're working with a large number of variables and need to keep track of their names and types.

To use %who without any arguments, simply run the command in a cell:

%who

This will display a list of all variables in the global scope, regardless of their type.

If you want to list only variables of a specific type, you can pass the type as an argument to %who. For example, to list only string variables:

one = "for the money"
two = "for the show"
three = "to get ready now go cat go"

%who str

The output will display a list of variables that match the specified type:

one three two

The %who magic command is a crucial aspect of Jupyter Notebook tips. It can help you keep track of your variables, making it easier to manage your workspace and maintain a clear overview of your code.

11. IPython Magic – Timing Code Execution

These Jupyter Notebook tips introduce two magic commands that are useful for timing code execution: %%time and %timeit. These commands are particularly handy when you're working with slow code and need to identify performance bottlenecks.

The %%time magic command provides information about a single run of the code in your cell. It measures the time taken to execute the code and displays the result.

%%time
import time
for _ in range(1000):
    time.sleep(0.01) # sleep for 0.01 seconds

The output will show the CPU times and the total elapsed time (wall time):

CPU times: user 21.5 ms, sys: 14.8 ms, total: 36.3 ms 
Wall time: 11.6 s

On the other hand, the %timeit magic command uses the Python timeit module to run a statement multiple times (default is 100,000 times) and provides the mean of the fastest three execution times. This gives you a more accurate measure of the code's performance.

import numpy
%timeit numpy.random.normal(size=100)

The output will display the number of loops and the best execution time:

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

These tips showcase how the %%time and %timeit magic commands can help you measure and optimize the performance of your code, enabling you to identify and address bottlenecks more effectively.

12. IPython Magic – High-Resolution Plot Outputs for Retina Displays

This Jupyter Notebook tip focuses on improving plot quality when working on devices with high-resolution Retina displays, such as recent MacBook models. By using a simple IPython magic command, you can obtain double-resolution plot outputs that take full advantage of your screen's capabilities.

First, let's create a sample plot using Matplotlib:

x = range(1000)
y = [i ** 2 for i in x]
plt.plot(x, y)
plt.show();

hires-1

To enable high-resolution plot outputs for Retina displays, simply add the following magic command before your plotting code:

%config InlineBackend.figure_format ='retina'
plt.plot(x, y)
plt.show();

hires-2

Note: The high-resolution plot will only be visible on Retina displays. Standard displays will show the plot at normal resolution.

By setting the InlineBackend.figure_format configuration option to 'retina', Matplotlib will generate plots with double the resolution, resulting in sharper and more detailed images on compatible displays.

This Jupyter Notebook tip demonstrates how a simple IPython magic command can enhance the visual quality of your plots, making them look their best on high-resolution Retina displays.

13. IPython Magic – Debugging with %pdb

%pdb is a magic command that allows you to debug your code using the Python Debugger (pdb) directly within the Jupyter Notebook interface. These Jupyter Notebook tips showcase how to use %pdb by simply prepending it to your code cell:

%pdb
def pick_and_take():
    picked = numpy.random.randint(0, 1000)
    raise NotImplementedError()

pick_and_take()

When an exception is raised, Jupyter will automatically enter the debugger, allowing you to use pdb commands to navigate through your code, inspect variables, and identify the cause of the exception. Some common pdb commands include:

  • n (next): Execute the next line
  • s (step): Step into a function call
  • c (continue): Continue execution until the next breakpoint or exception
  • p (print): Print the value of an expression
  • q (quit): Quit the debugger

These tips simplifies debugging by providing an interactive debugger directly within the notebook environment.

14. Suppress the Output of a Final Function

This Jupyter Notebook tip demonstrates how to suppress the output of a function on the final line of a cell, which can be particularly useful when working with plotting libraries like Matplotlib.

First, let's import the necessary libraries and create a sample plot:

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 1, 1000)**1.5
plt.hist(x)

hist

In addition to the plot, you'll notice that the output of the plt.hist(x) function is also displayed:

(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>)

To suppress this output and only display the plot, simply add a semicolon (;) at the end of the function call:

plt.hist(x);

hist

By appending a semicolon, the function's output will be suppressed, leaving only the plot visible in the notebook.

The semicolon has help us to keep the notebook clean and focused by suppressing unwanted function outputs, especially when working with plotting libraries.

15. Executing Shell Commands

Executing shell commands directly from within your Jupyter Notebook cells can be incredibly useful for managing files, installing packages, and interacting with the system shell without leaving the notebook interface. These Jupyter Notebook tips demonstrate how to do this by simply prefixing the shell command with an exclamation mark (!).

To execute a shell command, simply prefix the command with an exclamation mark (!). For example, to list all CSV files in your current working directory:

!ls *.csv

The output will display the list of matching files:

nba_2016.csv titanic.csv pixar_movies.csv whitehouse_employees.csv

You can also use shell commands to manage packages. For instance, to install a package using pip and then check if it's installed:

!pip install numpy
!pip list | grep pandas

The output will confirm the installation and display the package version:

Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
pandas (1.2.3)

With shell commands you can streamline your workflow, manage files, and handle packages directly from within your notebook.

16. Using LaTeX for Formulas

LaTeX is a powerful typesetting system that allows you to create beautifully rendered mathematical formulas within your Jupyter notebooks. These Jupyter Notebook tips highlight the ability to use LaTeX by incorporating it in Markdown cells, enabling you to create professional-looking equations and mathematical expressions with ease.

To use LaTeX for formulas, simply enclose your LaTeX code between dollar signs ($). For example:

$P(A \mid B) = \frac{P(B \mid A)P(A)}{P(B)}$

When the Markdown cell is rendered, the LaTeX code will be transformed into a properly formatted mathematical formula:

jupyter-notebook-latex

LaTeX allows you to create complex formulas and equations, including fractions, integrals, summations, and more. Here are a few more examples:

Euler's identity: $e^{i\pi} + 1 = 0$

Pythagorean theorem: $a^2 + b^2 = c^2$

Summation: $\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$</code></pre>

Rendered output:



Installation

By exploiting the expressiveness of Markdown and the typesetting capabilities of LaTeX, you can effectively communicate mathematical concepts and enhance the overall quality of your notebooks.

17. Run Code from Different Kernels in a Notebook

Jupyter Notebooks provide the flexibility to combine code from multiple kernels within a single notebook. This powerful feature allows you to leverage the strengths of different programming languages and tools, making your notebooks more versatile and efficient.

To use a specific kernel for a particular cell, simply prefix the cell with the corresponding IPython Magic command followed by the kernel name. Some commonly used kernels include:

  • %%bash for Bash shell scripts
  • %%HTML for HTML code
  • %%python2 for Python 2
  • %%python3 for Python 3
  • %%ruby for Ruby
  • %%perl for Perl

Here's an example of using the %%bash magic to run a simple Bash loop:

&#37;&#37;bash
for i in {1..5}
do
  echo "i is $i"
done

The output will be:

i is 1
i is 2
i is 3
i is 4
i is 5

You can also use other kernels in a similar fashion. For instance, to run some Ruby code:

&#37;&#37;ruby
puts "Hello from Ruby!"
(1..5).each do |i|
  puts "Square of #{i} is #{i**2}"
end

Output:

Hello from Ruby!
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25

These Jupyter Notebook tips showcase the flexibility and interoperability of Jupyter Notebooks, enabling you to combine multiple programming languages and tools seamlessly within a single notebook.

18. Install other kernels for Jupyter

One of the nice features about Jupyter is ability to run kernels for different languages. As an example, here is how to get and R kernel running.

Easy Option: Installing the R Kernel Using Anaconda

If you used Anaconda to set up your environment, getting R working is extremely easy. Just run the below in your terminal:

conda install -c r r-essentials</code>

Less Easy Option: Installing the R Kernel Manually

If you are not using Anaconda, the process is a little more complex. Firstly, you’ll need to install R from CRAN if you haven’t already.

Once that’s done, fire up an R console and run the following:

install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools'))<br class="rm" />devtools::install_github('IRkernel/IRkernel')<br class="rm" />IRkernel::installspec() <span spellcheck="true"># to register the kernel in the current R installation</span>

19. Running R and Python in the same notebook.

The best solution to this is to install rpy2 (requires a working version of R as well), which can be easily done with pip:

pip install rpy2

You can then use the two languages together, and even pass variables inbetween:

&#37;load_ext rpy2.ipython
&#37;R require(ggplot2)
array([1], dtype=int32)
import pandas as pd df = pd.DataFrame({<br class="rm" />'Letter': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],<br class="rm" />'X': [4, 3, 5, 2, 1, 7, 7, 5, 9],<br class="rm" />'Y': [0, 4, 3, 6, 7, 10, 11, 9, 13],<br class="rm" />'Z': [1, 2, 3, 1, 2, 3, 1, 2, 3]<br class="rm" />})
&#37;&#37;R -i df ggplot(data = df) + geom_point(aes(x = X, y= Y, color = Letter, size = Z))

ggplot

Example courtesy Revolutions Blog

20. Writing functions in other languages

Sometimes the speed of numpy is not enough and I need to write some fast code.
In principle, you can compile function in the dynamic library and write python wrappers…

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.

First you’ll need to install:

!pip install cython fortran-magic
&#37;load_ext Cython
&#37;&#37;cython<br class="rm" />def myltiply_by_2(float x):<br class="rm" />return 2.0 * x
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.

&#37;load_ext fortranmagic
&#37;&#37;fortran subroutine compute_fortran(x, y, z)<br class="rm" />real, intent(in) :: x(:), y(:)<br class="rm" />real, intent(out) :: z(size(x, 1))<br class="rm" />z = sin(x + y)<br class="rm" />end subroutine compute_fortran
compute_fortran([1, 2, 3], [4, 5, 6])

There are also different jitter systems which can speed up your python code. More examples can be found here.

21. Multicursor support

Jupyter supports mutiple cursors, similar to Sublime Text. Simply click and drag your mouse while holding down Alt.

multiline

Multicursor support.

22. 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.

!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

nbextensions

The nbextension configurator.

Here's a more concise version:

23. Create a Presentation from a Jupyter Notebook

These Jupyter Notebook tips introduce RISE, an extension that converts your notebook into an interactive slideshow. To install RISE, use conda or pip:

Conda:

conda install -c damianavila82 rise

Pip:

pip install RISE

Enable the extension:

jupyter-nbextension install rise --py --sys-prefix
jupyter-nbextension enable rise --py --sys-prefix

To create a presentation, open your notebook, click "Enter/Exit RISE Slideshow," and use arrow keys to navigate. Customize slide types using these shortcuts:

  • Shift-M: Regular slide (Markdown cell)
  • Shift-J: Regular slide (Code cell)
  • Shift-F: Fragment slide (Markdown cell)
  • Shift-H: Sub-slide (Markdown cell)
  • Shift-N: Skip slide (Markdown cell)

24. The Jupyter output system

Notebooks are displayed as HTML and the cell output can be HTML, so you can return virtually anything: video/audio/images.

In this example I scan the folder with images in my repository and show thumbnails of the first 5:

import os<br class="rm" />from IPython.display import display, Image names = [f for f in os.listdir('../images/ml_demonstrations/') if f.endswith('.png')]<br class="rm" />for name in names[:5]:<br class="rm" /> display(Image('../images/ml_demonstrations/' + name, width=100))

img1

img2

img3

img4

img5

We can create the same list with a bash command, because magics and bash calls return python variables:

names = !ls ../images/ml_demonstrations/*.png names[:5]
['../images/ml_demonstrations/colah_embeddings.png',<br class="rm" />'../images/ml_demonstrations/convnetjs.png',<br class="rm" />'../images/ml_demonstrations/decision_tree.png',<br class="rm" />'../images/ml_demonstrations/decision_tree_in_course.png',<br class="rm" />'../images/ml_demonstrations/dream_mnist.png']

25. Enhance Interactivity with ipywidgets

Ipywidgets is a powerful library that allows you to create interactive widgets within your notebooks. By incorporating sliders, buttons, and other interactive elements, you can manipulate data in real-time and create more engaging and dynamic notebooks.

To get started with ipywidgets, first make sure you have it installed:

!pip install ipywidgets

Then, import the library and the display function from IPython.display:

import ipywidgets as widgets
from IPython.display import display

Now, let's create a simple slider widget:

slider = widgets.FloatSlider(
    value=5.0,
    min=0.0,
    max=10.0,
    step=0.1,
    description='Value:'
)
display(slider)

This code creates a slider widget with a default value of 5.0, a minimum value of 0.0, a maximum value of 10.0, and a step size of 0.1. The description parameter adds a label to the slider.

You can access the current value of the slider using the value attribute:

print(slider.value)

In addition to sliders, ipywidgets provides various other interactive widgets, such as buttons, checkboxes, and dropdown menus. For example, let's create a button that prints a message when clicked:

button = widgets.Button(description='Click me!')
output = widgets.Output()

def on_button_clicked(b):
    with output:
        print("Hello, Jupyter!")

button.on_click(on_button_clicked)
display(button, output)

By exploiting the power of ipywidgets, you can create more engaging and informative notebooks that encourage exploration and experimentation.

26. Big data analysis

A number of solutions are available for querying/processing large data samples:

27. Set an Alarm for Code Completion

Adding just a few lines of code can enable Python to play a sound or even speak to you when your code execution is complete. These Jupyter Notebook tips demonstrate how you can implement this functionality, providing a convenient way to stay informed about the progress of your long-running tasks without constantly monitoring the notebook.

For Windows users, you can use the winsound module to create a beep:

import winsound
duration = 1000  # milliseconds
freq = 440  # Hz
winsound.Beep(freq, duration)

This code snippet will play a beep sound at a frequency of 440 Hz for 1000 milliseconds (1 second) when your code has finished running.

Mac users can take advantage of the built-in say command using the os module:

import os
os.system('say "your program has finished"')

This will make your Mac speak the phrase "your program has finished" once the code execution is complete.

These Jupyter Notebook tips demonstrate how you can add simple audio notifications to your code, helping you stay informed about the progress of your long-running tasks without constantly monitoring the notebook.

28. Sharing notebooks

The easiest way to share your notebook is simply using the notebook file (.ipynb), but for those who don’t use Jupyter, you have a few options:

  • 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 a blog using Pelican from your Jupyter notebooks.

What are your favorites?

Let me know what your favorite Jupyter notebook tips are.

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.

I also recommend the links below for further reading:

Celeste Grupman

About the author

Celeste Grupman

Celeste Grupman is the CEO at Dataquest She is passionate about creating affordable access to high-quality skills training for students across the globe.