To enhance the functionality of Jupyter Notebook, here are 4 tips

(Star Python developers to improve Python skills)

Transfer from: Heart of the Machine

Jupyter Notebook is an artifact for all developers to share their work. It provides a convenient way to share Notebooks: Combining text, code and pictures to convey information to the audience more quickly. At present, Jupyter Notebook has been applied to fields such as data analysis and data science.

However, most developers only understand its fur. Developers use the basic functions of Jupyter Notebook to write Python code and display pictures. But do you know that there are a lot of custom features in Jupyter? These cool options can help you use Jupyter notebook to write code and display diagrams more efficiently.

This article will introduce 4 ways to enhance the functions of Jupyter Notebook.

1. Execute Shell command

In technical or programming text, shell means a way to interact with a computer using text. The most popular Unix shell is Bash (Bourne Again SHell), which is the default shell of the terminal on Linux machines.

When working with Python, you will often switch back and forth between writing Python code and using shell commands. For example, you want to use Python to read a file on disk, and this requires you to confirm the file name. Normally, you need to enter ls in the terminal to get a list of all files and folders in the current directory. But switching back and forth like this is very cumbersome and inefficient.

The cool thing is that Jupyter can execute Shell commands without even leaving the browser. Just add an exclamation mark! in front of the shell command and Jupyter will convert it to Bash. Add an exclamation mark! in front of any command, and they can be run in Python Jupyter Notebook.

# 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 output of a shell command to a Python variable as follows:

# Getting the current directory. 
# The variable "X" now contains ["/home/george/github/project_1"]
X = !pwd

2. Change theme

640?wx_fmt=png

Many text editors and programming IDEs have custom themes. One of the developers' favorite themes is dark themes (such as monaki), because dark themes look more comfortable for developers who stare at the screen all day. Fortunately, Jupyter has a plugin that allows users to choose themes themselves.

To install the plugin, you only need to run the following pip command in the terminal:

pip install jupyterthemes

Run the following command to get a list of available themes:

jt -l

As of this writing, the topics to choose from include:
chesterish
grade3
gruvboxd
gruvboxl
monokai
oceans16
onedork
solarizedd
solarizedl

Check out these themes, as shown in the picture below, we have a lot of different color options.

640?wx_fmt=png

solarizedd (left), gruvboxl (middle), grade3 (right).

3. Notebook extension

Jupyter Notebook extensions (nbextensions) are some JavaScript modules, you can use them to enhance the function and use of Notebook. The extension plug-in essentially modifies the Jupyter UI to achieve more robust functions.

We first install nbextensions via pip:

pip install jupyter_contrib_nbextensions 
	
jupyter contrib nbextension install

After the installation is complete, start Jupyter. You will see a new option-NBextensions. After selecting it, you will see a large number of Jupyter Notebook extension plug-in options.

640?wx_fmt=png

Through a quick search, you can view the functions of these extensions. Below I will introduce some of the most important plugins.

Table of Contents

As the name describes, Table of Contents automatically generates a table of contents based on the title created by # in the notebook. For example, I created the following title in the notebook:

# This is a super big title
## This is a big title
### This is a medium title
#### This is a small title

A directory will be generated on the left. Double-click the title to link to the corresponding chapter content. This function is very convenient when the notebook is very large and has many options!

640?wx_fmt=png

Hinterland

Code completion is a common feature that most IDEs have, such as PyCharm. Developers like this feature because it makes their work easier. Developers don't need to remember every command, the IDE will prepare everything.

Hinterland can complete code completion in Jupyter Notebook. As you type, you will see some code completion suggestions. Especially when you search for commands from external libraries (examples are shown below). This is so convenient!

640?wx_fmt=png

Split Cells

Splitting cells allows developers to view 2 cells side by side. This function is very convenient when you have two related cells (such as description and its corresponding icon).

640?wx_fmt=gif

4. Use Qgrid to explore Dataframes

The last stop is Qgrid, which allows developers to explore and edit data frames without using complex Pandas code. Qgrid can interactively render pandas data frames in Jupyter notebook, so you can perform some intuitive controls, such as scrolling, sorting and filtering, and double-clicking cells to edit data frames.

We first install Qgrid:
pip install qgrid
jupyter nbextension enable --py --sys-prefix widgetsnbextension

To use Qgrid to render a data frame, developers only need to import Qgrid, and then input the data frame into the show_grid function:

import qgrid
qgrid_widget = qgrid.show_grid(df, show_toolbar=True)
qgrid_widget

In this way, you can perform a lot of interactive operations on the data frame:

  • Add and delete rows;

  • Filter row

  • Edit the cell.

Enter more parameters into the show_grid function to perform other interactive operations. For all the functions of Qgrid, see: https://github.com/quantopian/qgrid.

640?wx_fmt=gif


The above are 4 ways to enhance Jupyter Notebook functions.

Original link: https://towardsdatascience.com/4-awesome-tips-for-enhancing-jupyter-notebooks-4d8905f926c5

Recommended reading

(Click the title to jump to read)

Think this article is helpful to you? Please share with more people

Pay attention to the "Python Developer" starred to improve Python skills

640?wx_fmt=png

Good article, I am reading ❤️

Guess you like

Origin blog.csdn.net/iodjSVf8U1J7KYc/article/details/101181741