Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize Jupyter

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to optimize Jupyter", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to optimize Jupyter.

1. Introduction

Jupyter Notebook is one of the most commonly used environments for solving machine learning / data science tasks in Python. Simply put, Jupyter Notebook is a client-server application for running documents in browsers that contain both code and rich text elements (such as paragraphs, equations, etc.).

two。 Shortcut key

Keyboard shortcuts are useful for speeding up code writing. There are two ways to interact with Jupyter Notebook: command mode and edit mode. Some shortcuts are valid only in one mode or another, while others are common in both modes.

Some of the common shortcuts in both modes are:

Ctrl + Enter: run all selected cells

Shift + Enter: runs the current cell and moves the next cell

Ctrl + s: save notebook

To enter Jupyter command mode, we need to press the ESC key and then press any of the following commands:

H: displays all shortcuts available in Jupyter Notebook

Shift + up / Down Arrow: select multiple notebook units at the same time (selecting multiple units and pressing Enter will make them all run!)

A: insert a new cell above

B: insert a new cell below

X: cut the selected cell

Z: undelete cells

Y: change the cell type to Code

M: change the cell type to Markdown

Space: scroll down the notebook

Shift + Space: scroll up the notebook

To enter Jupyter editing mode, we need to press Enter and any of the following commands:

Tab: complete code description

Ctrl +]: increase indentation

Ctrl + [: reduce indentation

Ctrl + z: undo

Ctrl + y: redo

Ctrl + a: select all

Ctrl + Home: move the cursor to the beginning of the cell

Ctrl + End: move the cursor to the end of the cell

Ctrl + left: move the cursor to the left

Ctrl + right: move the cursor to the right

3. Shell command and software package installation

We can run the Shell command in a Jupyter notebook cell by adding an exclamation point at the beginning of the cell, which is currently recognized by very few users. For example, using the! ls run unit returns all projects in the current working directory. Run the unit with! pwd, which will print out the file path of the current directory.

The same technique can be applied to installing Python packages in Jupyter notebooks.

! Pip install numpy

4. Jupyter theme

In addition, we can change the appearance of the Jupyter notebook and install packages with different collections of themes. The default Jupyter theme is similar to the one in the figure.

We can install the package directly in the notebook:

! Pip install jupyterthemes

We can run the following command to list the names of all available topics:

! jt-l # Cell output: # Available Themes: # chesterish # grade3 # gruvboxd # gruvboxl # monokai # oceans16 # onedork # solarizedd # solarizedl

Finally, we can select a theme using the following command (in this example, I decided to use the solarized1 theme):

! Jt-t solarizedl

As long as you run this command and then refresh the page, the Jupyter notebook will look like the figure.

If you want to return to the original Jupyter notebook theme at any time, you can run the following command and refresh the page.

! Jt-r

5. Jupyter Notebook extension

Laptop extensions can be used to enhance the user experience and provide a variety of personalization technologies. In this example, the nbextensions library is used to install all the necessary widgets (here, it is recommended that you first install the package through the terminal, and then open the Jupyter notebook). The nbextensions library uses different Javascript models to enrich the front end of Jupyter notebooks.

! Pip installs jupyter_contrib_nbextensions! Jupyter contrib nbextension installation-system

As long as you install nbextensions successfully, you will find an extra tag Nbextensions on your Jupyter laptop home page.

By clicking the Nbextensions tab, we can see the list of available widgets. Here, I enable the options shown in the figure as an example.

In addition, I listed some of my favorite extension widgets as follows:

(1) Table of Contents

Automatically generate directories from markdown headers.

(2) Snippets

Sample code for loading common libraries and creating sample diagrams that you can use as a starting point for data analysis.

(3) Hinterland

Jupyter Notebook code completion function.

In addition to the three listed above, the nbextensions library provides a lot of extension widgets, and you can try to check different widgets to explore their interesting extensions!

6. Output all command lines

By default, Jupyter Notebook outputs the result only with the last line of commands in the cell as the output line. So, if we want Jupyter Notebook to automatically output all the commands for us instead of using print () to output one command at a time, we can add the following line of code at the beginning of the code.

From IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"

In addition, you can write LaTex in the Markdown unit by enclosing the text between the dollar sign ($).

7. Notebook slides

You can create a slide presentation of an Jupyter notebook by doing the following to Jupyter Notebook: View-> Cell Toolbar-> Slideshow, and then selecting a slide configuration for each cell in the notebook.

Finally, you can create a slide presentation by going to the terminal and entering the following command.

Pip install jupyter_contrib_nbextensions # and successively: jupyter nbconvert my_notebook_name.ipynb-to slides-post serve

8. Devil's command

Python's magic command is an enhancement of Ipython based on the normal python syntax, which can be used to execute certain commands, such as inline drawing, execution time of print cells, memory consumption of print running cells, and so on.

At the same time, the python magic command is prefixed with% and begins with only a single% symbol, then it acts on a single line of input (where the command is placed); if the magic command starts with two%%, it will be applied to the entire cell.

Use the following command to print out all available magic commands:

% lsmagic here, I believe you have a deeper understanding of "how to optimize Jupyter", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report