IT 人的笔记本——全面了解 Jupyter
序言
Jupyter 作为 IT 人的笔记本,在数据科学中得到了广泛的应用,而今在云原生的环境下,通过与容器和 git 的结合,基于这款笔记本,我们可以重塑许多以前晦涩的技术,将之前的隐性知识让更多人理解并运用。
发展迅速的 Jupyter
Jupyter 是数据科学家非常青睐的产品,而时至今日,这款产品支持了更多的语言,提供了更多的客户端,使用体验不断优化,很显然,这款产品有了更广泛的使用场景。官方推荐了 JupyterLab、Jupyter Notebook、JupyterHub、Voilà 四种产品。
JupyterHub ia a multi-user version of the notebook designed for companies,classrooms and research labs.
Voilà helps communicate insights by transforming notebooks into secure, stand-alone web applications that you can customize and share.
JupyterLab is the latest web-based interactive development environment for notebooks, code, and data. Its flexible interface allows users to configure and arrange workflows in data science, scientific computing, computational journalism, and machine learning. A modular design invites extensions to expand and enrich functionality.
The Jupyter Notebook is the original web application for creating and sharing computational documents. It offers a simple, streamlined, document-centric experience.
Jupyter 在过去十年取得了极好的发展,在 GitHub 上分开了多个组织:
Jupyter 采用了 BSD 3-Clause 协议,只要保留代码原信息,允许商用,对开发者友好。
市面上已经有很多成型的商品,提供友好的交互界面。
https://github.com/jupyter-resources/joss-papers/blob/master/LICENSE
学习 Jupyter 的重要资源
官网给出了 Jupyter Learning Resources。
https://github.com/jupyter-resources
搭建 Jupyter 学习环境
搭建 Jupyter 原生环境
通过参考这篇官网资料 https://github.com/jupyterlab/jupyterlab,可以顺利搭建起 Jupyter 的原生环境。这里的要点是:有 Anaconda 就使用 conda 完成 Jupyterlab 的搭建,没有 Anaconda 就用 Python 的 pip 完成 Jupyterlab 。
支持 Go 和 Java 运行
之所以从 Jupyterlab 使用,因为 jupyter notebook 只支持 Python。原生环境的使用在许多数据科学的书籍里已经有了浩如烟海的说明。而对 Go 语言和 Java 语言的使用,才是本文的重点。
使用 GopherNotes 支持 Go 在 Jupyter 中运行
https://github.com/gopherdata/gophernotes
在 MacOS 系统中 ,可直接参考链接中的安装方式:
Mac
Important Note - gomacro relies on the
plugin
package when importing third party libraries. This package works reliably on Mac OS X with Go 1.10.2+ as long as you never execute the commandstrip gophernotes
.Method 1: quick installation as module
$ env GO111MODULE=on go get github.com/gopherdata/gophernotes $ mkdir -p ~/Library/Jupyter/kernels/gophernotes $ cd ~/Library/Jupyter/kernels/gophernotes $ cp "$(go env GOPATH)"/pkg/mod/github.com/gopherdata/gophernotes@v0.7.4/kernel/* "." $ chmod +w ./kernel.json # in case copied kernel.json has no write permission $ sed "s|gophernotes|$(go env GOPATH)/bin/gophernotes|" < kernel.json.in > kernel.json
Method 2: manual installation from GOPATH
$ env GO111MODULE=off go get -d -u github.com/gopherdata/gophernotes $ cd "$(go env GOPATH)"/src/github.com/gopherdata/gophernotes $ env GO111MODULE=on go install $ mkdir -p ~/Library/Jupyter/kernels/gophernotes $ cp kernel/* ~/Library/Jupyter/kernels/gophernotes $ cd ~/Library/Jupyter/kernels/gophernotes $ chmod +w ./kernel.json # in case copied kernel.json has no write permission $ sed "s|gophernotes|$(go env GOPATH)/bin/gophernotes|" < kernel.json.in > kernel.json
To confirm that the
gophernotes
binary is installed in GOPATH, execute it directly:$ "$(go env GOPATH)"/bin/gophernotes
and you shoud see the following:
2017/09/20 10:33:12 Need a command line argument specifying the connection file.
Note - if you have the
JUPYTER_PATH
environmental variable set or if you are using an older version of Jupyter, you may need to copy this kernel config to another directory. You can check which directories will be searched by executing:$ jupyter --data-dir
在 Linux 系统中,需要注意:
在 Go 1.11 后的版本中,使用 GO111MODULE=on。
安装 Go 时,修改 GOPROXY,
go env -w GOPROXY=https://goproxy.cn,direct
Linux or FreeBSD
The instructions below should work both on Linux and on FreeBSD.
Method 1: quick installation as module
$ env GO111MODULE=on go get github.com/gopherdata/gophernotes $ mkdir -p ~/.local/share/jupyter/kernels/gophernotes $ cd ~/.local/share/jupyter/kernels/gophernotes $ cp "$(go env GOPATH)"/pkg/mod/github.com/gopherdata/gophernotes@v0.7.4/kernel/* "." $ chmod +w ./kernel.json # in case copied kernel.json has no write permission $ sed "s|gophernotes|$(go env GOPATH)/bin/gophernotes|" < kernel.json.in > kernel.json
Method 2: manual installation from GOPATH
$ env GO111MODULE=off go get -d -u github.com/gopherdata/gophernotes $ cd "$(go env GOPATH)"/src/github.com/gopherdata/gophernotes $ env GO111MODULE=on go install $ mkdir -p ~/.local/share/jupyter/kernels/gophernotes $ cp kernel/* ~/.local/share/jupyter/kernels/gophernotes $ cd ~/.local/share/jupyter/kernels/gophernotes $ chmod +w ./kernel.json # in case copied kernel.json has no write permission $ sed "s|gophernotes|$(go env GOPATH)/bin/gophernotes|" < kernel.json.in > kernel.json
To confirm that the
gophernotes
binary is installed in GOPATH, execute it directly:$ "$(go env GOPATH)"/bin/gophernotes
and you shoud see the following:
2017/09/20 10:33:12 Need a command line argument specifying the connection file.
Note - if you have the
JUPYTER_PATH
environmental variable set or if you are using an older version of Jupyter, you may need to copy this kernel config to another directory. You can check which directories will be searched by executing:$ jupyter --data-dir
在 Windows 系统中,建议安装虚拟机,在虚拟机中部署 Linux 系统,在系统中 通过修改生成和修改配置文件,实现在本地 Windows 的访问。如果 Windows 上安装了容器运行环境,参考官方文档运行。
生产配置文件
jupyter-notebook --generate-config
编辑 ~/.jupyter/jupyter_notebook_config.py 配置文件,增加如下内容,重新启动 Jupyter-lab
c.NotebookApp.allow_remote_access = True
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
使用 IJava 支持 Java 在 Jupyter 中运行
https://github.com/SpencerPark/IJava
部署前提条件:
Java JDK >= 9. Not the JRE.
Some jupyter-like environment to use the kernel in.
Download the release from the releases tab. A prepackaged distribution will be in an artifact named
ijava-$version.zip
.Unzip it into a temporary location. It should have at least the
install.py
andjava
folder extracted in there.Run the installer with the same python command used to install jupyter. The installer is a python script and has the same options as
jupyter kernelspec install
but additionally supports configuring some of the kernel properties mentioned further below in the README.# Pass the -h option to see the help page > python3 install.py -h # Otherwise a common install command is > python3 install.py --sys-prefix
Check that it installed with
jupyter kernelspec list
which should containjava
.
完成上述安装,即可在笔记本中享受语言自由的笔记本。
下面笔记内容通过点击选择右侧的 Python3 处选择 Java、Go、Python 三种语言得到不同 Cell 的运行结果。
版权声明: 本文为 InfoQ 作者【dongge】的原创文章。
原文链接:【http://xie.infoq.cn/article/ed0f72ba3ac2979b75e8f1f2a】。文章转载请联系作者。
评论