Jupyter Notebook can do that?

Soup up your cells with a couple of key extensions

Jesse Markowitz
CodeX
5 min readAug 22, 2021

--

Wait, not those cells. | Photo by CDC on Unsplash

As a newbie to data science and coding in general, I’m still getting the hang of Jupyter Notebook. I learned the very basics of coding in an IDE from Flatiron’s free online courses and Harvard’s CS50 on edX (love you, David!), but Jupyter Notebook is a totally different animal. Cells that have to run in order? Markdown what?

Keyboard shortcuts were a good first step toward getting comfortable. I quickly learned to:

  • add cells (A for above, B for below)
  • remove cell (X)
  • switch between code (Y) and markup (M)
  • switch between command (esc) and code (return)
  • run a cell (shift + tab)

Those few shortcuts alone made my workflow much quicker and cleaner, but I knew there had to be more out there.

Behold: Extensions!

With a little light googling, I discovered Jupyter Extensions, a library of user-created add-ons that greatly enhance the experience of coding in a Jupyter Notebook. If you’ve had the thought, “I wish I could ____ in Jupyter Notebook,” then chances are there’s an extension for that.

Once you install Jupyter Extensions, you’ll see a new tab labeled “Nbextensions” available when you launch Jupyter Notebook. Here you can click on each extension to read a brief description or how-to:

So many extensions!

Here are my current favorites:

Snippets

No matter how many times I’ve had to type import matplotlib.pyplot as plt, there are still moments when I forget how to spell it. Before Snippets, I would have to either check through my other Notebooks or google it. But now I can click on the Snippets menu, hover over Matplotlib, then click “Setup for notebook” et voilà:

The `from __future__ …` business is to ensure compatibility

Granted, there are some extra lines there that I don’t need, but deleting them is still quicker than googling.

Other times, my fingers hover over the keyboard as I stare off into space, brain blank, and try desperately to remember the syntax for a list comprehension. Does the iterable come before the output? Where does the in go? Wonder no more:

It’s like magic!

Obviously Snippets doesn’t know what I’m working on, what variables I’m using, or what operation I want to make the list from. It just fills in a low-level example I can then edit, in this case a list of squares of x from -10 through 10. I need to replace x**2 with the operation I want to perform on each item x from the iterable (and probably change the variable from x to something more specific/descriptive). There’s also a snippet for a list comprehension with a conditional statement.

Snippets does not write your code for you! It’s only there to provide editable templates for boilerplate code. It’s probably slower than writing it out yourself if you’re confident, but definitely faster than googling or sifting through Stack Overflow responses. It’s a great tool for someone like me who is still learning the basics.

Variable Inspector

I frequently assign a variable, but then forget to return its value from the cell or add a print function to see it, so I edit and re-run the cell. But if the code in the cell operates on the variable, then running it a second time performs the operation again and changes the value of the variable…and well now I have to run everything from the top. So frustrating and inefficient!

There’s got to be a better way!

With Variable Inspector, even if I forget to return or print a variable, I can easily peer into my code to see its current state:

It’s like X-ray glasses for your code!

Scratchpad

When I’m working Pandas DataFrames, I often find I end up with a half-dozen cells cluttering up my Notebook, filled with code like df.head(), df.info(), df.shape, df.describe()…you get the idea. While there are times I might want these in my Notebook, often I’m just using them to ensure that a function I want to use actually returns what I think it will. I’m not the kind of coding maniac who recklessly throws an inplace=True into a function call without checking first! But I also don’t want to scroll through a disorganized Notebook littered with cells I’ll end up deleting anyway. Enter Scratchpad:

It’s like unwrapping a present before you’re supposed to, without leaving a paper trail…

Scratchpad only provides one cell for you to use, but the idea is to use it for one-off checks and tests. You can even display charts with matplotlib. But be warned: any operations you perform or variables you assign can and will affect the state of your notebook! In other words, running x=5 in the Scratchpad cell creates a global variable x in the kernel with an assigned value of 5 that is accessible in your other cells. Beware of changing your variables without a paper trail!

There’s more out there…

Those are just my top 3 Jupyter Notebook extensions right now. Here are some others I’ve experimented with as well:

  • Python Markdown allows you to insert variable values dynamically into markdown cells (like string formatting)
  • Live Markdown Preview is pretty much what it sounds like and especially useful when dealing with MathJax formatting
  • Runtools provides keyboard shortcuts and labeling options for running multiple cells as once
  • Move Selected Cells also does what it sounds like and replaces the cumbersome but otherwise necessary select →cut →new cell →paste workflow
  • Gist-it provides a shortcut to save the current Notebook as a Github Gist, which might come in handy for future Medium posts…

Tell me in the comments which Extensions you use or if you have an even better way to harness the power of Jupyter Notebook. Not to mention the whole wide world of Jupyter Labs (which has a great and fairly new debugger), but I’ll save that for another post. Happy extending!

Jupyter’s Great Red Kernel | Photo by NASA on Unsplash

--

--

Jesse Markowitz
CodeX

Data scientist with a background in science education and a passion for creative problem solving for public good. New York, NY