5 Tips and Tricks to use with Jupyter Notebook to be productive and to stay organized

Almas Myrzatay
DataDrivenInvestor
Published in
8 min readNov 29, 2020

--

As a Data Scientist you probably use Jupyter Notebook on a daily basis in order to create quick prototypes or while exploring the dataset. Any seasoned Data Scientist probably found many tricks that works for them, but the goal of this article (with Jupyter Notebook demo) is to provide introductory tips and tricks to help new Data Scientists with their journey in staying organized and get the most out of the tool.

In the following notebook, we will cover the following material:

Table of Content

  1. Installation Guide
  2. Tip & Trick 1: Signature and Docstrings
  3. Tip & Trick 2: Debugging
  4. Tip & Trick 3: Execution Time
  5. Tip & Trick 4: Keyboard shortcuts
  6. Tip & Trick 5: Jupyter-nbextension

Note:

I also wrote Jupyter Notebook to demonstrate all the discussed features in this article. I think it will be helpful to demo while you are reading thru this article, and feel free to reuse it as a refresher while learning.

Here is the link

Introduction

Jupyter Notebook is an indispensable tool for any Data Scientist. It is an interactive tool used for quick prototyping and iterative development. In particular, the tool can be very handy for new learners who want to see intermediary results while parsing data, running an algorithm or visualizing. One of the main advantages and popularity of the Notebook can be attributed to the ease of algorithm or code modification without rerunning entire program all the time.

For this notebook to work you will need to install `Jupyter Notebook` and optionally `jupyter-nbextension` extension.

1. Installation

There are several ways to install Jupyter Notebook and as a popular tool, it is supported on both Windows, Linux and other major OS platforms. More info on how to install it can be found in the official Jupyter Notebook documentation

The official documentation recommends installing Jupyter Notebook using the conda package manager.

If you use python package, you can run in the terminal:

pip install jupyterlab

If you have conda installed, you can run in the terminal:

conda install -c conda-forge jupyterlab

To run you can execute in terminal: jupyter-lab

1.1 Anaconda

Personally, I like Anaconda, which is data science platform. Once installed, it comes with many libraries such as numpy, scikit-learn, pandas (and more) pre-installed. Think of it as one-stop-shop with all the tools necessary for data science work

1.2 Optional: Jupyter-nbextension installation

In this tutorial, we will be also using jupyter-nbextension library. I would recommend installing it, because it will give you more advanced enhancements

Using pip, you can run in terminal:

pip install jupyter_nbextensions_configurator

Using conda, you can run in terminal:

conda install -c conda-forge jupyter_nbextensions_configurator

Once installed, the Nbextension tab should appear on the main notebook page, along with tabs: Files, Running, Clusters

Nbextension tab once installed

As illustrated in the Figure above, there are many extensions you can install. You will just have to tick on the one you would like to activate.

Tip and Trick 1: Signature and Docstrings

It is a good practice to document your code and place comments along with explanation of your code. Often times we have to read someones code or even revisit our own from time to time; after prolonged time away from your code or reading someone code can be a bit daunting. As a result, make sure to learn about signatures and docstrings to ease future work for yourself and others.

Create new cell and past the following code:

# This is demo function
def docstring_function(run_code=5):
"""This function runs a loop and prints output"""
for index in range(run_code):
print("Running code: {}".format(index))

Now, create new cell below add ? at the end of the function name to view its docstring (or run the command below):

docstring_function?

At the bottom of the screen, you should see window pop-up showing the function signature, file, and file type.

If you add ?? after the class/function name, you can see the source code of the class/function

docstring_function??

It also works with any installed function and/or standard library functions. For instance, create new cell run the code below:

import random
random.random??

In general, when you append ? or ?? at the end of the function, you should see output at the bottom of the webpage the info about function you just questioned

The bottom of the webpage will show info about the function when ? or ?? invoked in the cell

Tip and trick 2: Debugging in Jupyter Notebook

Although, Jupyter notebook gives granularity to break down the code and run compotents separately, sometimes debugging comes in handy especially while coding an algorithm or looking for edge cases. Following is one way on how one can debug within Jupyter Notebook:

%debug — to debug a single line of code; OR
%%debug — to debug the whole cell

Run the following code below, and you will see ipdb> line along with text space to give debugging commands (i.e. continue, etc).

Let’s create a new cell and paste the following code:

%%debug
# This is demo function for debugging
def debugging_function(run_code=5):
"""This function runs a loop and prints output"""
for index in range(run_code):
print("Running code: {}".format(index))

When you run the cell above, you will see the following output illustrated in the figure below:

Debugging option in the Jupyter Notebook

Note: This is just one way to debug in the notebook. More information on how to debug, can be found here.

Tip and Trick 3: Execution Time

There are several ways to measure execution time of your code. Below we will explore two options:

First: Time it with built-in functions

Create new cell and past the following code:

%%time
# This is demo function for timing
def timing_function(run_code=5):
"""This function runs a loop and prints output"""
for index in range(run_code):
print("Running code: {}".format(index))

Alternatively, you can time only one statement. It is the same process as performed for ? and ??. Create new cell, paste the code sand run the following code:

run_code = 5
%time mult_print = [print("Running code: {}".format(index)) for index in range(run_code)]

The output should show the time it took to run your code as shown in the figure below:

Execution time measurement by built-in Jupyter Notebook

Second: Use jupyter-nbextension library

The library jyputer-nbextensions has an extension that prints out execution time along with time stamp of execution. You can find more information about the extension in the library documentation

You will have to activate this feature in the Nbextension tab of the main menu. You can always create new web browser tab and past the following URL:

http://localhost:8888/tree

Your port number 8888 might not be the same, but by default it is 8888 if you didn’t change it during configuration

Once activated, you will have timing feature appear on every cell you run as shown in the figure below.

Execution time and timestamp for each cell

Tip and Trick 4: Keyboard Shortcuts

Reaching to top-left corner to run the cell or create new cell can be very annoying. Instead, there are handful of hotkeys you can use to become proficient in Jupyter Notebook. The main ones are:

Shift + Enter — Run cell + select next cell
Press key a — Insert cell above
Press key b — Insert cell below
Press key m — Convert the cell to markdown (after your run the markdown cell, you can double click it to edit)
Press key y — Convert the cell back to code
Press key x — Cut the selected cell(s)
Press key c — Copy the selected cell(s)
Press key v — Paste the selected cell(s)

Note: you can change those shortcuts by navigating to top-left corner menu in Help->Keyboard Shortcuts

Tip and Trick 5: More enhancements using “jupyter-nbextension” extension

In the “Installation” section we installed additional library called jupyter-nbextension which is a great extension that can help when you work with notebook.

5.1 Table of Content

One of the main features of the extension is the table of the content that helps to organize the content of the notebook. Often time, as you start to experiment, you will beginning to create functions and various auxiliary helper functions that will become hard to navigate. As such, having headers for each section will help you to stay on top and left-hand-side thumbnails with table of content will be great guide.

To make sure you have table content formatted, you will need to follow markdown formatting for titles and it will automatically create headers, sub-headers and so on.

You can always show or hide it by clicking on the table of content icon located in the menu bar on the top of the webpage.

Table of content activation and formatting example

5.2 Auto-fill

The modern day IDEs all have auto-fill function that aid with function names and ease memorization or constant referral to internet for function name. Unfortunately, Jupyter Notebook doesn’t have built-in auto-fill function, which makes this extension great addition while working within a notebook

Once activated in the Nbextension menu, auto-complete will be activated as shown in the image below.

Auto fill functionality

5.3 PEP 8 (Python)

Auto PEP8 extension helps with keeping your python code within established practices and community guidelines. It is good practice to keep the code clean and keep it consistent for usability. For many, notebook is a playground where they can quickly prototype, but it doesn’t mean that one should keep the code unclean and don’t follow consistent formatting for usability purposes. For more info about PEP8, you can follow this link.

5.4 Desktop Notifications

This comes in handy if you have several notebooks running in parallel. This feature would send you notifications on your desktop app through your browser. If you are using Mac, this would mean that in the top-right corner, Chrome (for instance) would send you a reminder on behalf of Jupyter Notebook.

The figure below shows how to activate it and the example of what the pop-up will look like

5.5. Many more

There are many more extensions you could explore and use while working with Jupyter Notebook. The important point is that above mentioned features are just starting point for you to get started. I will leave it up to you to explore features that suit your needs.

Thanks for reading my article.

Check out my other stories. If you liked it, or have any comments/questions, let me know! Feel free to connect on social media: Instagram, LinkedIn

Gain Access to Expert View — Subscribe to DDI Intel

--

--