Jupyter Notebook Jupyter Notebook Quick Start

 Jupyter Notebook (previously known as IPython notebook) is an interactive notebook that supports running over 40 programming languages. In this article, we'll cover the main features of Jupyter notebooks and why they're a powerful tool for anyone looking to write beautiful, interactive documents.

Before starting to use the notebook, we first install the library using pip in cmd  pip install jupyter 

After installation, run the command  jupyter notebook and you will see:

[I 08:34:12.265 NotebookApp] Writing notebook server cookie secret to C:\Users\zjb52\AppData\Roaming\jupyter\runtime\notebook_cookie_secret
[I 08:34:13.407 NotebookApp] Serving notebooks from local directory: C:\Users\zjb52
[I 08:34:13.408 NotebookApp] 0 active kernels
[I 08:34:13.409 NotebookApp] The Jupyter Notebook is running at:
[I 08:34:13.412 NotebookApp] http://localhost:8888/?token=ab4515092713565e3db18a62abc166573aab52689c45d6c1
[I 08:34:13.412 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 08:34:13.420 NotebookApp]

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=ab4515092713565e3db18a62abc166573aab52689c45d6c1
[I 08:34:13.946 NotebookApp] Accepting one-time-token-authenticated connection from ::1

At the same time, the main Jupyter interface will be launched in the folder where you opened the notebook, as shown below:

If you want to create a new notebook, just click Newand select the type of notebook you want to start.

 

 Here, since I only have one Python kernel, we run a Python notebook. In the newly opened tab, we will see the notebook interface, which is currently empty.

The notebook interface consists of the following parts:

  1. the name of the notebook
  2. The main toolbar provides options for saving, exporting, reloading notebooks, and restarting the kernel
  3. hot key
  4. The main area of ​​the notebook, including the content editing area of ​​the notebook

Get familiar with the menus and options slowly. If you want to learn more about specific topics about notebooks or some libraries, you can use the help menu on the right side of the menu bar.

The main area below, consists of sections called cells. Each notebook consists of multiple cells, and each cell can serve a different purpose.

Seen in the screenshot below is a code cell [ ]starting with . In this type of cell, arbitrary code can be entered and executed. For example, enter 1 + 1and press Shift + Enter. After that, the code in the cell will be calculated and the cursor will be moved to a new cell. You will get something like this:

Based on the green border line, we can easily identify the currently working cell. Next, we enter some other code in the second cell, such as:

1 for i in range(5):
2     print(i)

 

When evaluating the code above, you get:

As in the previous example, the results are displayed immediately after the code is evaluated. You should have noticed that this time there is no Out[2]text like this. This is because we printed the result and didn't return any value.

A very interesting feature of notebooks is that you can modify previous cells, recalculate them, and update the entire document. Try moving the cursor back to the first cell and 1 + 2change it to 2 + 3, then press Shift + Enterto recalculate that cell. You will find that the result is updated to 5 immediately. This feature is especially powerful if you don't want to re-run the entire script and just want to test a program with different parameters. However, you can also recalculate the entire notebook by clicking Cell ->  Run all.

Now that we know how to enter code, why not try to make this notebook more beautiful and informative? For this, we need to use other types of cells, namely Header cells and Markdown cells.

First, we add a notebook title at the top. Select the first cell and click Insert ->  Insert单元格above(Insert Cell Above). You will notice that a new cell appears immediately at the top of the document. Click on the cell type in the shortcut bar to turn it into a heading cell:

Check Heading in the drop-down options. A popup message will then appear telling you how to create different levels of headers so that you have a different type of cell:

The cell #starts with a tag, which means this is a first-level heading. If a subheading is required, it can be indicated with the following markup (explained in the popup message when changing the cell type):

# : first level title
## : secondary title
### : Level 3 heading
...

 

Write the #title of the document after and count the cell. You will find a very nice looking header. As an example and an exercise, I've also added a few other header cells:

 

 

 

 

With the headers added, we're writing some explanations of what's going on in each code cell. To do this, we need to insert the cell in the corresponding place, and then change its type to Markdown. Then, calculate the new cell. That's it, your explanatory text is beautifully rendered!

Finally, you can rename the notebook by clicking Fiel ->  Renameand entering the new name. This way, the new name will appear in the upper left corner of the window, next to the Jupyter logo.

cell manipulation

Advanced cell manipulation will make writing notebooks more convenient. An example is as follows:

  • If you want to delete a cell, you can select the cell, and then click Edit ->  Delete Cell;
  • If you want to move a cell, just click Edit ->  in turn Move cell [up | down];
  • If you want to cut and paste a unit test, you can first click Edit ->  Cut Cell, and then click Edit ->  Paste Cell [Above | Below];
  • If you have many cells in your notebook that only need to be executed once, or want to execute a large piece of code at once, you can choose to merge these cells. Click Edit ->  Merge Cell [Above | below].
Remember these actions, they can save you a lot of time.

Advanced usage of Markdown cells

Let's take a look at the Markdown cell again. Although its type is markdown, this type of cell also accepts HTML code. This way, you can implement richer styles in the cell class, add images, and more. For example, if you wanted to add the Jupyter logo to your notebook, set it to 100px x 100px, and place it on the left side of the cell, you could write:

<img src="http://blog.jupyter.org/content/images/2015/02/jupyter-sq-text.png"
style="width:100px;height:100px;float:left"> 

After calculating the cell, this result will appear:

After calculating the cell, the result will appear like this

In addition, markdown cells also support LaTex syntax. E.g:

$$\int_0^{+\infty} x^2 dx$$

Evaluating the above cell yields the following LaTex equation:

LaTex equations

Export function

Another powerful feature of notebooks is their export capabilities. Notebooks can be exported to several formats:

  • HTML
  • Markdown
  • ReST
  • PDF (via LaTeX)
  • Raw Python

The export PDF function allows you to create beautiful PDF documents without writing LaTex. You can also publish notebooks as web pages on your website. You can even export to ReST format as documentation for software libraries.

Matplotlib integration

If you've ever drawn graphics in Python, you know matplotlib. Matplotlib is a Python library for creating beautiful graphs that is even better when used in conjunction with Jupyter notebooks.

To use matplotlib in a Jupyter notebook, you need to tell Jupyter to take all the graphs matplotlib generates and embed them in the notebook. For this, it is necessary to calculate:

For %matplotlib inline

to execute successfully, you need to  pip install matplotlib first  .

Running this command may take a few seconds, but it needs to be executed once in the notebook. Next, let's draw a graph to see the specific integration effect:

1 import matplotlib.pyplot as plt
2 import numpy as np
3 
4 x = np.arange(20)
5 y = x**2
6 
7 plt.plot(x, y)

 

The above code will plot the equation y=x^2 . After calculating the cells, you will get the following graph:

Plot the equation y=x^2

We see that the drawn graph is added directly to the notebook, just below the code. We can modify the code later, recalculate, and the graph will also dynamically update. This is a feature every data scientist wants: put code and images in the same file, and clearly see the effect of each piece of code.

non-native kernel

We can start Jupyter on a single computer very easily, and support multiple people connecting to the same Jupyter instance over the network. In the last article, did you notice this passage when starting Jupyter:

The IPython Notebook is running at: http://localhost:8888/

 

This means that your notebook is running locally and you can access the notebook by opening http://localhost:8888/ on your browser. You can also modify the configuration to make the notebook publicly accessible. This way, anyone who knows the notebook's address can connect to the notebook and make remote modifications.

Epilogue

From these two quickstart introductions, we can see that Jupyter notebooks are a very powerful tool for creating beautiful interactive documents, making teaching materials, and more. It is recommended that you start using Jupyter notebooks right away and explore more of the power of notebooks.

 

 Reference article:

Little_Rookie  Jupyter Notebook Quick Start

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024329&siteId=291194637