Jupyter Notebook Quick Start [go], jupyternotebook
Jupyter Notebook (formerly known as IPython notebook) is an interactive Notebook that supports running more than 40 programming languages. In this article, we will introduce the main features of Jupyter notebook and why it is a powerful tool for people who want to write beautiful interactive documents.
Before using notebook, We need to install the Library first. You can find the complete steps on the Jupyter official website.
Note: As longpip install jupyter
You can.
jupyter notebook
After running the preceding command, you will see an output similar to the following:
[I 20:06:36.367 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret[I 20:06:36.813 NotebookApp] Serving notebooks from local directory: /home/your_username[I 20:06:36.813 NotebookApp] 0 active kernels[I 20:06:36.813 NotebookApp] The IPython Notebook is running at: http://localhost:8888/[I 20:06:36.813 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
At the same time, the main interface of Jupyter will be started in the folder where you open notebook, as shown below:
To create a new notebook, clickNew
Select the notebook type you want to start.
Here, we run a Python notebook because I only have one Python kernel. On the newly opened tab, we will see the notebook interface, which currently contains nothing.
The notebook interface consists of the following parts:
Familiarize yourself with these menus and options. If you want to learn more about notebook or some libraries, you can use the Help menu on the right of the menu bar.
The main area below consists of a part called a cell. Each notebook consists of multiple cells, and each cell can have different purposes.
The following shows a code cell.[ ]
. In this type of cells, you can enter any code and execute it. For example, enter1 + 2
And pressShift + Enter
. Then, the code in the cell is calculated, and the cursor is moved to a new cell. You will get the following results:
Based on the Green Border line, we can easily identify the current working cell. Next, we enter some other code in the second cell, for example:
for i in range(5): print(i)
When you evaluate the code above, you will get:
As in the previous example, the result is displayed immediately after the code is calculated. You should have noticed that this time there is no similarOut[2]
Such text. This is because the result is printed and no value is returned.
Notebook has a very interesting feature, that is, you can modify the previous cells and recalculate them so that you can update the entire document. Try to move the cursor back to the first cell and1 + 2
Modify2 + 3
And then pressShift + Enter
Recalculate the 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 want to test a program with different parameters. However, you can re-calculate the entire notebook as long as you clickCell
->Run all
You can.
Now we know how to input code. Why don't we try to make this notebook more beautiful and richer? To do 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 then clickInsert
->Insert cell above
(Insert a cell above ). You will find that a new cell is immediately displayed at the top of the document. Click the cell type in the shortcut key bar to change it to a header cell ):
Select Heading from the drop-down list. A pop-up message will pop up to show you how to create titles of different levels, so that you have a different type of cell:
This Cell uses#
It indicates that this is a level-1 title. If you need a sub-title, you can use the following mark (when changing the cell type, there is an explanation in the pop-up message ):
#: Level 1 title #: Level 2 Title ###: Level 3 title...
In#
Then write down the document title and calculate the cell. You will find a title with a very nice style. As an example and exercise, I have added several other title cells:
After adding the title, we will write some explanations to introduce the situation in each code cell. To this end, we need to insert a cell in the corresponding place and change its type to Markdown. Then, calculate the new cell. In this way, your interpretation text is beautifully rendered!
Finally, you can rename the notebook and clickFiel
->Rename
And enter a new name. In this way, the new name will appear in the upper left corner of the window, next to the Jupyter logo.
Cell operations
Advanced Cell operations make writing notebook easier. Example:
- To delete a cell, select the cell and click
Edit
->Delete Cell
;
- To move a cell, click
Edit
->Move cell [up | down]
;
- To cut a unit test, click
Edit
->Cut Cell
And then clickEdit
->Paste Cell [Above | Below]
;
- If you only need to execute many cells in your notebook once, or want to execute a large segment of code at a time, you can choose to merge these cells. Click
Edit
->Merge Cell [Above | below]
.
Remember these operations, they can help you save a lot of time.
Markdown cell advanced usage
Let's take a look at the Markdown cell. Although its type is markdown, such cells also accept HTML code. In this way, you can implement richer styles, add images, and so on in the cell class. For example, if you want to add a Jupyter logo to the notebook, set the size to 100px x 100px and place it on the left side of the cell, you can write it as follows:
After the cell is calculated, the following result is displayed:
In addition, the markdown cell also supports the LaTex syntax. For example:
$$\int_0^{+\infty} x^2 dx$$
Calculate the above cells and obtain the following LaTex equation:
Export Function
Another powerful feature of notebook is its export function. You can export notebook in multiple formats:
- HTML
- Markdown
- ReST
- PDF (via LaTeX)
- Raw Python
Exporting PDF allows you to create beautiful PDF documents without having to write LaTex. You can also publish the notebook as a webpage on your website. You can even export it to the ReST format as a document in the software library.
Matplotlib Integration
If you have drawn a graph using Python, you must know matplotlib. Matplotlib is a Python library used to create beautiful images. It can be used in combination with Jupyter notebook for better experience.
To use matplotlib in Jupyter notebook, notify Jupyter to obtain all the images generated by matplotlib and embed them into the notebook. Therefore, you need to calculate:
%matplotlib inline
To succeed, you must firstpip install matplotlib
.
It may take several seconds to run this command, but you only need to execute it once in the notebook. Next, let's draw a graph to see the specific integration effect:
import matplotlib.pyplot as pltimport numpy as npx = np.arange(20)y = x**2plt.plot(x, y)
The above Code draws the equation y = x ^ 2. After the cell is calculated, the following result is displayed:
We can see that the drawn image is directly added to the notebook, just below the code. After that, we can modify the code and re-calculate it. Then, the graph will be dynamically updated. This is a feature that every data scientist wants: Put code and images in the same file to clearly see the effect of each piece of code.
Non-local Kernel
We can easily start Jupyter on a computer, and support multiple people to connect to the same Jupyter instance through the network. In the previous article, have you noticed the following message when you started Jupyter:
The IPython Notebook is running at: http://localhost:8888/
This means that your notebook runs locally. You can open http: // localhost: 8888/in a browser to access the notebook. You can also modify the configuration so that the notebook can be publicly accessed. In this way, anyone who knows the notebook address can connect to the notebook for remote modification.
Conclusion
From the two quick start introductions, we can see that Jupyter notebook is a very powerful tool that can create beautiful interactive documents, make teaching materials, and so on. We recommend that you start using Jupyter notebook immediately to explore more powerful notebook functions.
Link from: https://www.packtpub.com/books/content/getting-started-jupyter-notebook-part-1