参考：4 Awesome Tips for Enhancing Jupyter Notebooks
Jupyter Notebooks have been an awesome tool for all developers looking to share their work. They provide an easy way to share Notebooks — a combination of text, code, and graphics designed to enhance the way we convey information to our audience. They’re extensively used in fields such as Data Analysis and Data Science.
Yet, most of us are really only scratching the surface of Jupyter Notebooks. We use the basic features of writing Python code and displaying graphs. But did you know that there’s a lot of customizable features in Jupyter that can enhance its functionality?! Cool options that can make your notebook writing more efficient and presentation more effective.
Here are 4 awesome tips for enhancing Jupyter Notebooks!
(1) Executing Shell Commands
A shell in technical / programming context is a way of interacting textually (using text) with a computer. The most popular Unix shell is Bash (Bourne Again SHell ). Bash is the default shell that you’ll find with your terminal on any Linux machine.
It’s quite common that when working with Python, you’ll be switching back and forth between writing the Python code and using the shell. For example, you might want to read a certain file from disk using Python and as such need to check what the exact name is. You’d normally just go to your terminal and type in ls to get a list of all files and folders in the current directory. That back and forth can be quite tedious and inefficient
The really cool thing is that Jupyter has the ability to execute shell commands without you ever having to leave your browser. All you have to do is place an extra exclamation mark
!before the shell command and Jupyter will interpret it as Bash. Any command that works at the command-line can be used in a Python Jupyter Notebook by prefixing it with the
# Listing folder contents >>> !ls mynotebook.ipynb stuff.txt# Getting the current directory >>> !pwd /home/george/github/project_1# Printing from Bash >>> !echo "Pizza is delicious!" Pizza is delicious!
We can also assign the outputs of shell commands to Python variables as in the code below.
# Getting the current directory. # The variable "X" now contains ["/home/george/github/project_1"] X = !pwd
(2) Changing Themes
Many text editors and programming IDEs have customizable themes. A favorite one amoung develops are the darker themes such as monaki, since it’s more comfortable for the eyes when you’re looking at a screen all day. Fortunately, Jupyter has a plugin that allows for theme selection.
To install the plugin simply run the pip command in your terminal:
pip install jupyterthemes
You can list the available themes like so:
As of the writing of this article, the following themes are available:
chesterish grade3 gruvboxd gruvboxl monokai oceans16 onedork solarizedd solarizedl
Check out a few of them shown below. Lots of great colour options!
(3) Notebook Extensions
We’ll start by installing nbextensions via pip:
pip install jupyter_contrib_nbextensions jupyter contrib nbextension install
Once the installation is complete, start up Jupyter. You’ll see a new tab called NBextensions. Once you select it, you’ll see the many Jupyter Notebook extension options!
You can look up most of these extensions to see what they do with a quick Google search. I’ve highlighted some of the most useful ones below.
(1) Table of Contents
The table of contents, as its name describes, automatically generates a table of contents for your notebook based on the titles created by the hashtags
#in the notebook. For example, the titles I created in the example image below are:
# This is a super big title ## This is a big title ### This is a medium title #### This is a small title
And the table of contents is generated quite nicely on the left. Each title in the table has a link that will take you right to that section on a double click. This is super convenient when your notebook starts to get big and you have many sections!
Code completion is a very common feature in most IDEs, especially PyCharm for Python. Developers love it because it makes their job so much easier, not having to remember each and every command since they know their IDE will clean things up for them.
Hinterland enables code autocompletion within Jupyter Notebooks. as you type, the suggestions are laid out before you. This is most notable when you’re searching for a command from an external library as shown in the examples below. Super convenient!
(3) Split Cells
Split cells allows you to view 2 cells side-by-side. This is super convenient when you have 2 cells that are related, such as a description and the visualisation it refers to.
(4) Explore Dataframes with Qgrid
Our final stop is Qgrid — a tool that allows you to explore and edit your dataframes without any complex Pandas code.
Qgrid renders pandas dataframes within your Jupyter notebooks in an interactive way. With this rendering, you’re given intuitive controls like scrolling, sorting and filtering, as well as being able to edit your dataframe by double-clicking the desired cell.
Let’s get started by installing Qgrid:
pip install qgrid jupyter nbextension enable --py --sys-prefix widgetsnbextension
To render a dataframe with Qgrid, simply import it and then pass your dataframe to the
show_grid function like so:
import qgrid qgrid_widget = qgrid.show_grid(df, show_toolbar=True) qgrid_widget
Doing so will show the dataframe with a number of interactive options:
- Adding and removing rows
- Filtering rows
- Editing cells
There are also several more interactive options which can be enabled by passing more of the arguments to the
show_gridfunction. You can read more details about the full functionality of Qgrid on the official GitHub page.
So there you have it! 4 Awesome Tips for Enhancing Jupyter Notebooks.
If you’re hungry for more, not to worry! There are many more Jupyter widgets and extensions available to play around with. As recommended reading, the official Jupyter Notebook Documentation is a great place to start.