第二次のJupyter notebook

上一篇教程的完结篇,我搬~

https://www.packtpub.com/books/content/getting-started-jupyter-notebook-part-2


As seen in the first part of this introduction, you can do a lot of things with the basic capabilities of the Jupyter notebook. But it offers even more possibilities and options, allowing users to create beautiful, interactive documents.

Cells manipulation

When writing your notebook, you will want to use more advanced cell manipulation. Thankfully, the notebook allows you to manipulate a variety of operations on your cells. You can delete a cell by selecting the desired cell, then going to Edit -> Delete cell, you can movecells by going to Edit -> Move cell [up | down], or you can cut the cell and paste it by going Edit -> Cut Cell then Edit -> Paste Cell ..., selecting the pasting style you need. You can alsomerge cells, by going to Edit -> Merge cell [above|below], if you find that you have so many cells that you execute only once, or if you want a big chunk of code to be executed in a single sweep. Keep these commands in mind when writing your notebook -- they will save you a lot of time.

Markdown cells advanced usage

Let's start by exploring the markdown cell type a little more. Even though it says markdown, this type of cell also accepts HTML code. Using this, you can create more advanced styling within your cell, add images and so on. For example, if you want to add the Jupyter logo to your notebook, with a size of 100px by 100px, on the left of the cell:

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

This provides the following after the cell evaluation:

Let’s end with the capabilities of the markdown cells: they also support LaTeX syntax. Write your equations in a markdown cell, evaluate the cell, and look at the result. By evaluating this equation:

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

You get the LaTeX equation:

Export capabilities

Another powerful feature of the notebook is the export capability. Indeed, you can write your notebook (an illustrated coding course, for example) and export it in multiple formats such as:

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

By exporting to PDF, you can then create a beautiful document using LaTeX without even using LaTeX! Or you can publish your notebook as a page on your personnal website. You can even write documentation for libraries by exporting to ReST.

Matplotlib integration

If you have ever done plotting using Python, then you probably know about matplotlib. Matplotlib is a Python library used to create beautiful plots. And it really shines when used with the Jupyter notebook. Let's start exploring what can be done with it.

To get started using matplotlib in the Jupyter notebook, you need to tell Jupyter to get all images generated by matplotlib and include them in the notebook. To do that, you just evaluate:

%matplotlib inline

It might take a few seconds to run, but you only need to do this once when you start your notebook. Let's make a plot and see how this integration works:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = x**2

plt.plot(x, y)

This simple code will just plot the equation y=x^2. When you evaluate the cell, you get:

As you can see here, the plot was added directly into the notebook, just after the code. We can then change our code, reevaluate, and the image will be updated on the fly. This is a nice feature for every data scientist wanting to have their code and images in the same file, letting them know which code does what exactly. Being able to add some more text to the document is also a great help.

Non local kernel

The Jupyter notebook is built in such a way that it is very easy to start Jupyter from a computer, and allow multiple people to connect to the same Jupyter instance through network. Did you notice, in the previous part of this introduction, the following sentence during Jupyter startup:

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

This means that your notebook is running locally, on your computer, and that you can access it through a browser at the address http://localhost:8888/. It is possible to make the notebook publicly available by changing the configuration. This will allow anyone with the address to connect to this notebook and make modifications to the notebooks remotely, through their Internet browser.

The end word

As we have seen in these two parts, the Jupyter notebook is a very powerful tool, allowing users to create beautiful documents for data exploration, education, documentation, and, actually, everything you can think of. Don't hesitate to explore more of its possibilities, and give feedback to the developers if you ever run into trouble, or even if you just want to thank them.

About the author

Marin Gilles is a PhD student in Physics in Dijon, France. A large part of his work is dedicated to physical simulations for which he developed his own simulation framework using Python, and contributed to open-source libraries such as Matplotlib or IPython.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值