How to use Jypyter Notebook,Python Development

スポンサーリンク
Python_アイキャッチ_IDE_JupyterNotebookの使い方入門_En_rev0.1 DevTools(English)

Japanese | English

The use of an IDE is indispensable for efficient Python development.

Currently, several IDEs are available, including free and paid versions. (Figure.1)

For example, “Visual Studio” for full-scale development “Visual Studio Code” for installing various plug-ins and “Anaconda (including spyder and Jupyter Notebook)” for easy experimentation.

There are also platforms such as “Colabotatory” that can be developed in the cloud.

Python開発環境の種類について
Fig1. Examples of Python IDEs

Each IDE has its own strengths and ease of use, but it is best to select an IDE that best suits your development style and objectives.


Among these, this article focuses on “Jupyter Notebook (*)”, the IDE with the most ease of use and suitable for beginner developers.

(* In the following explanation, it is expressed as “Jupyter”)

The reasons (features) for its popularity can be summarized as follows.

Features
  1. Simple GUI and intuitive operation
  2. Coding and execution can be done in block unit (cell)
  3. Execution results are displayed inline
  4. Major libraries and modules already installed

Supplementary information on the features of 4.

Jupyter is a development tool included in the “Anaconda” distribution.

Anaconda can be used free of charge* and comes with a set of major libraries and modules pre-installed. (*There is also a paid version for commercial use.)

This has the great advantage of making it easy for beginners to get started by eliminating the troublesome environment construction process.

It is currently one of “the author’s most recommended IDEs“.

Please stay with us until the end of this article as you will be able to “do and understand” the following

What you will learn in this article
  • Understand the basics of how to use Jupyter Notebook
  • For tips on coding with Jupyter
  • Menu items and useful shortcuts

Now, we will start explaining the basic operations of Jupyter from the next section.

スポンサーリンク

1. Creating a Notebook ~ Executing Code

This section describes the basic operations of starting Jupyter and creating a project called “Notebook”, as well as writing and executing a program.

This section assumes that Jupyter is bundled with the Anaconda distribution.

Please refer to the following article for an explanation of how to install Anaconda.

Note that we have confirmed the operation on Windows10(64bit), Anaconda(2022.10), and JupyterNotebook(6.5.2).

1.1 Start Jupyter and create a project

This section explains how to start Jupyter and create a new project (Notebook).

Select Anaconda3 from the Windows Start menu list. And then select Jupyter Notebook in the Anaconda3 folder. (Figure 2.)

Fig2. List of Windows App

The console shown in Figure 3 will then appear and Jupyter will begin to run. This console only displays the status of JupyterNotebook, so it is not particularly necessary to proceed with development.

If it gets in the way, you can minimize it, but do not close it.

Fig3. Console Window

After a while, the top page of the web application “Jupyter” will appear. The recommended browser for Jupyter is “Google Chrome” for Windows and “Microsoft Edge” for Windows.

Fig4. JupyterNotebook Top Page

To start an existing project (Notebook) or create a new one, select a storage location from the folder tree (Figure 4.).

To create a new project, select python3 from the New pull-down list in the upper right corner. (Figure 4.)

You will be redirected to the Jupyter Notebook development window (Figure. 5).

The development window is lined with ”Menus”, ”Toolbars” and ”Cell Areas”.

JupyterNotebook_開発ウィンドウ_rev0.1
Fig5. Development Window

1.2 Cell(Code, Comment) and two Modes

In Jupyter, program is written in area called “Cell”.

There are two types of cells, one is “an area for writing Python code” and the other is “an area for writing comments in Markdown format“.

The area type can be selected by “Code/Markdown” in the menu or by using the shortcut keys ( y / m ) after selecting “Command Mode (see below)”.

JupyterNotebook_セル領域_rev0.1
Fig6. Change of Code/Markdown mode

Furthermore, there are two types of cell states: “Command Mode” and “Edit Mode“.

Command Mode” allows you to manipulate cells in the entire notebook. For example, move, delete, duplicate, etc. can be performed on a cell unit.

Command Mode can be entered by pressing the Esc key while a cell is selected. Command Mode is when the frame turns blue as shown below. (Figure 7)

python_IDE_JupyterNotebook Commandモード
Fig7. Command Mode

Edit Mode” is the state in which Python code or comments can be written in the selected cell.

Edit Mode can be entered by pressing the Enter key while a cell is selected. Edit Mode is when the frame turns green as shown below. (Figure 8)

python_IDE_JupyterNotebook Edit_mode
Fig8. Edit Mode

1.3 Manipulate Cell and Execute Code

Now, let’s actually manipulate the Cell.

The “Toolbar” summarizes the most frequently used functions related to cell manipulation. (Figure 9)

  • Delete, Copy, and Paste Cells
  • Move Cell Up and Down
  • Cell Execution, Interruption, Kernel Restart ・・・

Many other operations are grouped by function in the “Menu bar”. Most of them can be operated with shortcut keys, so once you get used to them, you can perform cell editing work smoothly.

python_IDE_JupyterNotebook Tool_bar
Fig9.Tool bar

Next, put the cell in code mode and try to enter and run the program.

If necessary, add a new cell and set Mode to “Code” (). Also, at this time, make sure the cell is in “Edit Mode” and enter the program ().

When you are done, press the Run button () or the shortcut Ctr + Enter to execute. The script is then immediately executed and the result is displayed in the output window directly below the cell ( text, shape, graph, etc.).

Jupyter provides many shortcuts to assist in coding. See <2.1.5 Help menu> after this.

python_IDE_JupyterNotebook Basic_Usage
Fig10. Code Entry and Execution Procedure

Jupyter can check the operation of each cell (minimum execution unit).

When coding, keep in mind the following points.

  1. Do not enter a large amount of script in a single cell
  2. Try to separate cells by function (method, class, processing unit).
  3. Check the operation of each cell and compare with the expected value.
  4. Use comment cells as appropriate to write descriptions

The project will automatically generate and save source files (*.ipynb) and .ipynb_checkpoints folder. The .ipynb_checkpoints folder contains backup information (rollback points).

スポンサーリンク

2. Menu Items and Useful Usage

The menu bar of JupyterNotebook provides various functions. Compared to other IDEs, it is more intuitive to use. In addition, shortcut keys are assigned to each function for efficient development.

This section provides an overview of the menu bar items and their convenient usage.

2.1 Menu Bar Items

The Menu bar contains the following items. (Figure 11) This section describes the special features of each menu item.

メニュー項目
Fig11. Menu Item
  1. File … Creating and saving projects, etc
  2. Edit … Manipulation of cells in Book
  3. View … Show/hide headers, toolbars, line numbers, etc.
  4. Insert … Inserting Cells
  5. Cell … Cell Execution
  6. Kernel … Restarting the Python engine (interpreter), etc.
  7. Widgets … Restarting the Python engine (interpreter), etc.
  8. Help … Manuals, shortcut key lists, etc.

2.1.1 File Menu

This section describes the contents of the File menu. The File menu contains basic project(book) operations such as “Create,” “Open,” and “Save” ().

In addition, Downloads as allows you to convert the JupyterNotebook itself (*.ipynb) into various formats for output and storage. For example, you can output HTML, Markdown, etc., converted to Python extension (*.py) for use with other IDEs as well.(Figure12, )

python_IDE_JupyterNotebook File_Menue
Fig12. File Menu

2.1.2 Edit Menu

Next, the contents of the Edit menu are explained. This section mainly describes cell operations.

The Edit menu contains items that can also be operated with the toolbar, such as duplicating and deleting cells.You do not even need to use the Edit menu, but you can split the cells into smaller cells ( Split Cell), debug them, and then merge them together ( Merge Cell * ). (Figure 13)

Others include moving cells () and search and replace. ()

python_IDE_JupyterNotebook Edit-mode
Fig13. Edit Mene Items

2.1.3 Cell Menu

Next, the contents of the Cell menu will be explained. This section summarizes the functions related to cell execution and output results. “Run selected cells + Options ()”, “Run all cells + Options ()” etc… are lined up.

CellType also switches cell types such as Code (coding), Markdown (.md format), etc. The Current(All) Outpus can be used to display, hide, or delete output results. (Figure 14)

python_IDE_JupyterNotebook Cell_Menue
Fig14. Cell Menu Item

2.1.4 Kernel Menu

Next, the contents of the Kernel menu are described. This menu summarizes operations related to the interpreter of the python scripting language. (Figure 15)

The kernel restart is required if the process stops during execution (for example, if it enters an infinite loop) or to reflect changes in IDE settings. Try Interrupt () or Restart (kernel restart) + option ().

You can also select the version of the interpreter to be used with Change Kernel and stop the project with Shutdown.

python_IDE_JupyterNotebook Kernel_Menu
Fig15. Kernel Menu item

2.1.5 Help Menu

Finally, the contents of the Help menu are explained. This menu provides access to Tips information for coding.

Specifically, the list of shortcut keys () and links to the official documentation of commonly used libraries(). Figure 16

python_IDE_JupyterNotebook Help_Menu
Fig16. Help Menu item

Let’s introduce some carefully selected shortcuts that you definitely want to remember when operating Jupyter.


The shortcuts to remember for use in ”Command Mode” are as follows.

【Cell Execution】

  Shift+Enter: Go to next cell after current cell is executed

  Ctrl+Enter:  Execute the current cell

【Insert and delete cells】

  A(a): Insert a new cell above the current cell

  B(b): Insert a new cell below the current cell

  X(x): Delete selected cell

【Save】

  Ctrl+S(s): Save and Checkpoint update

【Display Related】

  L(l): Toggle display/non-display of line number

  O(o): Toggle display/hide of output results

python_IDE_JupyterNotebook shortcut_CommandMode
Fig17. Keyboard Shortcuts(Command Mode)

The shortcuts to remember for use in ”Edit Mode” are as follows.

【Code entry assistance】

  Tab: Input support, insert indent

  Shift+Tab: Remove indentation

  Ctrl+Z(z): Undo

  Ctrl+/: Comment the selected line

  Ctrl+D(d): Delete selected line

【Cell Execution】(Same as Command mode)

  Shift+Enter: Go to next cell after current cell is executed

  Ctrl+Enter: Execute current cell

python_IDE_JupyterNotebook shortcut_EditMode
Fig18. Keyboard Shortcuts(Edit Mode)

In addition, in the Help menu, there are links to the official websites of Numpy, Pandas, Matplotlib, which are essential libraries for machine learning and numerical analysis.

スポンサーリンク

3. Summary

How was it?

In this article, we introduced how to use the Python development environment “JupyterNotebook“. Advantages and features of using “Jupyter” is the following.

  1. Simple GUI and intuitive operation
  2. Coding and execution in block (Cell)
  3. Execution results are displayed inline
  4. Major libraries and modules already installed

This is the IDE I recommend most for beginners.

We hope this article will help you to develop your Python skills.


There is also a cloud service called “Google Colaboratory” by Google that provides the same operability as JupyterNotebook. The advantage is that you can try out the GPU without a local environment.

If you are familiar with Jupyter, you may want to try “Colaboratory”. The following article explains how to use it. Please refer to it as well.

Thank you for reading to the end.

Japanese | English

タイトルとURLをコピーしました