avatarEryk Lewinson

Summary

nbpreview is a Python library and CLI tool that enables users to preview Jupyter Notebooks directly in the terminal with features such as syntax highlighting, rendering of DataFrames, markdown, LaTeX, images, and error outputs.

Abstract

The nbpreview tool is designed for programmers who prefer working from the terminal, providing a convenient way to inspect Jupyter Notebooks without the need to launch a full notebook environment. It supports syntax highlighting for various programming languages, including Python and Julia, and can render markdown, DataFrames, LaTeX, and images within the terminal. The tool offers different image rendering modes, such as character and braille, and allows users to customize the theme for code highlighting. Additionally, nbpreview captures stderr outputs and tracebacks, making it a comprehensive solution for reviewing notebooks from the command line. Installation is straightforward via pip, and the tool enhances the workflow for those who appreciate the efficiency of terminal-based operations.

Opinions

  • The author acknowledges that while they are not necessarily someone who does everything from the terminal, they recognize the convenience of carrying out operations like previewing Jupyter Notebooks in this way.
  • The author expresses a personal curiosity about how images are rendered in the terminal, suggesting that this feature is particularly intriguing.
  • The article implies that nbpreview is a valuable tool for the programming community, especially for those who enjoy customizing their terminal appearance and workflow.
  • The author suggests that nbpreview is not just a novelty but a practical tool by showing its ability to handle complex outputs like DataFrames and plots, which are rendered similarly to how they appear in a standard Jupyter Notebook.
  • By providing a side-by-side comparison with VS Code, the author indicates that nbpreview offers a viable alternative for quick previews, despite the difference in readability.
  • The inclusion of a purposefully thrown error to demonstrate nbpreview's capture of stderr outputs shows the author's attention to detail and commitment to a thorough review of the tool's capabilities.
  • The author encourages constructive feedback and invites readers to engage on Twitter or in the comments, indicating an openness to community dialogue and improvement.
  • The author promotes becoming a Medium member to support their work, suggesting a belief in the value of their content and the platform's role in learning and development.
Source: pixabay

Preview your Jupyter Notebooks in the terminal with nbpreview

With nbpreview, you can read and inspect Notebooks without leaving the terminal!

There are a lot of programmers who love to do as much as possible from the terminal. And while I am not necessarily one of them, it is often very convenient to carry out some operations straight from the command line. Speaking of the terminal — who has not spent a considerable amount of time tweaking what the terminal looks like (theme, fonts, etc.)…

Today, I have stumbled upon a Python library that adds one more thing that we can now easily do from the terminal — preview Jupyter Notebooks. And this includes the outputs of the cells — DataFrames and plots included!

Let me show you how to do it all with nbpreview.

nbpreview

nbpreview is a CLI and library that allows us to render Jupyter Notebooks in the terminal. It works a bit like the cat command for .ipynb files.

The library offers the following features:

  • syntax highlighting for code cells
  • it detects not only Python, but also languages such as Julia
  • renders markdown, DataFrames, LaTeX
  • draws images
  • parses HTML
  • renders stderr output and tracebacks

Installation

We can install the library by running:

pip install nbpreview

Hands-on example

For this article, we will preview a Notebook from my 2021 recap article. I chose it as it contains a few elements that we would like to preview, for example, DataFrame output, plots, etc.

Using nbpreview is very simple and intuitive. We can get preview a Notebook simply by running the following command in the terminal:

nbpreview file_name.ipynb

The path to the .ipynb file is the only required argument of the nbpreview command. Naturally, there are quite a lot of optional arguments we can play around with. But first, let’s see what the default output looks like.

Below we can see how the Notebook is rendered in the terminal. We already see that the DataFrame was nicely visualized, just as in a normal Jupyter Notebook.

Image by author

Also, outputs like DataFrames and images get a clickable link which you can follow to view the original output.

Personally, I was most curious about the images. Below, you can see how a bar chart is rendered in the terminal.

Image by author

By default, the images are drawn as blocks. We can also use one of the two other settings: character or braille.

nbpreview --image-drawing character file_name.ipynb
Image by author
nbpreview --image-drawing braille file_name.ipynb
Image by author

For the code highlighting, we can choose from dozens of available themes (including some popular ones like emacs, vim, dracula). We can change the theme by using the theme argument:

nbpreview --theme vim file_name.ipynb

In the image below, you can see the same Notebook rendered with the vim theme (upper) and default dark theme (lower).

Image by author

Lastly, let’s have a side-by-side look at the Notebook rendered in the terminal and by VS Code:

Image by author

While it might not be clearly readable without squinting, we threw an error on purpose (division by zero) to show that stderr is also captured by nbpreview.

To learn more about the functionalities offered by nbpreview, we can read the documentation on all options by running:

nbpreview --help

Or refer to the documentation available here.

Takeaways

  • nbpreview is a Python library/CLI that allows for previewing Jupyter Notebooks in the terminal,
  • it handles rendering DataFrame outputs, images, markdown, LaTeX, and more.

As always, any constructive feedback is welcome. You can reach out to me on Twitter or in the comments.

Liked the article? Become a Medium member to continue learning by reading without limits. If you use this link to become a member, you will support me at no extra cost to you. Thanks in advance and see you around!

You might also be interested in one of the following:

References

Education
Programming
Data Science
Command Line
Python
Recommended from ReadMedium