Python - 搭建Jupyter notebook环境

1- Jupyter简介

Jupyter提供一个开源的基于浏览器的Notebook, 可以开发、协作、分享甚至发布数据科学结果: 
  • 可用于创建和共享代码与文档网络应用;
  • 可在其中编写并运行代码、查看输出、可视化数据并查看结果;
  • 不仅支持 Python, 还包括用于Julia、 R和其他编程语言;
 
与IPython的联系与区别:
  • Jupyter是从IPython中迁移出来的独立应用;
  • 从IPython4.0(包含)之后分离成IPython和Jupyter,IPython只具有终端接口(安装的依赖包少很多);
  • 分离之后的Jupyter可以被更加广泛的使用,支持多种语言,并不断加入新特性;

2- 安装Jupyter

$ pip install jupyter -i https://mirrors.ustc.edu.cn/pypi/web/simple/

安装Jupyter的同时也安装包括Ipython等大量依赖库;

3- 帮助信息

jupyter --help
jupyter console --help
jupyter notebook --help
Jupyter中包括console和notebook:
console类似ipython;
notebook是基于浏览器Web技术的开发环境,可以进行写作文档和执行代码,包括web application和notebook documents;
  • web application等同于一个编辑器,除了支持普通代码,还包括富媒体信息如HTML,LaTex,Markdown,MathJax和各种图像格式;
  • notebook documents以JSON格式保存为.ipynb文件,可以通过bfconvert转成HTML、PDF等格式;

4- 启动Jupyter console

$ jupyter console
Jupyter console 5.2.0

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.



In [1]:

In [1]: exit
Shutting down kernel

$

5- 启动Jupyter notebook

Jupyter notebook是web based IPython封装,可以展现富文本,使得整个工作能够以笔记的形式展现和存储;
  • 交互上使用C/S结构,使用浏览器作为客户端访问页面,向后台的Jupyter Notebook服务器发送请求,并显示结果;
  • 页面都被保存为 .ipynb 的类 JSON 文件格式;
  • 默认启动的地址为“http://localhost:8888/tree”,可以启动多个,端口号会自动累加;
 
常用启动命令及参数:
$ jupyter notebook  # 以默认参数启动
$ jupyter notebook test.ipynb  # 启动时打开test.ipynb
$ jupyter notebook --port 12345  # 启动时指定端口
$ jupyter notebook --no-brower  # 启动时不打开浏览器
$ jupyter notebook --ip="*"  # 允许任意IP连接

以默认参数启动:

$ jupyter notebook
[I 23:16:00.714 NotebookApp] Writing notebook server cookie secret to C:\Users\anliven\AppData\Roaming\jupyter\runtime\notebook_cookie_secret
[I 23:16:01.245 NotebookApp] Serving notebooks from local directory: D:\Anliven
[I 23:16:01.246 NotebookApp] 0 active kernels
[I 23:16:01.247 NotebookApp] The Jupyter Notebook is running at:
[I 23:16:01.247 NotebookApp] http://localhost:8888/?token=34be99926b1d3972f14ccc93bff0be9fa1815cc81674199f
[I 23:16:01.247 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 23:16:01.249 NotebookApp]

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=34be99926b1d3972f14ccc93bff0be9fa1815cc81674199f
[I 23:16:01.570 NotebookApp] Accepting one-time-token-authenticated connection from ::1
[W 23:16:01.915 NotebookApp] 404 GET /static/components/moment/locale/zh-cn.js?v=20171207231600 (::1) 7.04ms referer=http://localhost:8888/tree
[W 23:16:01.920 NotebookApp] 404 GET /i18n/zh-CN/LC_MESSAGES/nbjs.json?v=20171207231600 (::1) 2.00ms referer=http://localhost:8888/tree

默认浏览器将打开Jupyter的web界面:

在Files标签中-->New-->选择对应的NoteBook,例如Python3,将出现交互式界面:

每个Code输入区域都有一个对应的输出区域,输入代码后按  Shift-Enter 将运行此代码,显示代码中最后一个表达式的值;
如果希望屏蔽输出,可以在最后一条语句后添加一个分号”;”;
使用单元(Cell)保存信息,种类包括表示格式化文本的Markdown单元和表示代码的Code单元等;
  • Code cells:用于写代码;
  • Markdown cells:写markdown文档,用来写说明,在Markdown单元中可以直接使用Html和Javascript;
  • Raw Cells: 代码或文档的输出信息;

  • 在函数、模块、类后面输入‘?’,按住Ctrl-Entry显示帮助文档,如果是两个‘??’则显示源代码;
  • 以%开始的魔术命令只对单行有效;
  • 以%%开头的魔术命令放在单元的第一行,对整个单元有效;
  • %prun用于代码的执行性能分析,可以作为行命令和单元命令使用;

6-设置Jupyter Notebook远程访问

6.1-生成配置文件

将在~/.jupyter目录下生成一个jupyter_notebook_config.py文件
$ jupyter notebook --generate-config
Writing default config to: C:\Users\anliven\.jupyter\jupyter_notebook_config.py

6.2-生成一个密文密码

$ jupyter console
Jupyter console 5.2.0

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.



In [1]: from IPython.lib import passwd

In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:e16f8ee37c2d:32b310c2505bd268a408a703ada17b7c0181c5f7'

In [3]: exit
Shutting down kernel

6.3-修改默认配置文件

jupyter_notebook_config.py的修改内容如下:
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha1:e16f8ee37c2d:32b310c2505bd268a408a703ada17b7c0181c5f7'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 12345

6.4-启动Jupyter Notebook

$ jupyter notebook                                                                                                   
[W 23:34:18.473 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. 
This is not recommended.                                                                                             
[I 23:34:18.643 NotebookApp] Serving notebooks from local directory: D:\Anliven                                      
[I 23:34:18.644 NotebookApp] 0 active kernels                                                                        
[I 23:34:18.644 NotebookApp] The Jupyter Notebook is running at:                                                     
[I 23:34:18.644 NotebookApp] http://[all ip addresses on your system]:12345/                                         
[I 23:34:18.645 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation)
.     

6.5-远程访问 

浏览器直接访问 http://<jupyter-server>:<port>可以看到jupyter的登陆界面。

6.6-官网参考信息

7- 更改jupyter notebook的工作空间

  1. 执行“jupyter notebook --generate-config”命令获取配置文件的位置;
  2. 修改jupyter_notebook_config.py中的c.NotebookApp.notebook_dir参数,将值更改为实际的工作空间目录即可;

8- 实现同时支持Python2和Python3

# 假定已安装Python3(通过Anaconda3安装)
# $ conda env list
# # conda environments:
# #
# base                  *  D:\DownLoadFiles\anaconda3

conda create -n testpy2  python=2  # 建立一个Python2的虚拟环境
source activate testpy2  # 激活环境
conda install ipykernel  # 安装ipykernel
python -m ipykernel install --user
source activate  # 激活base环境
jupyter notebook  # 启动notebook,此时可以看到Python2和Python3内核

9- 参考信息

10- 问题处理

Jupyter notebook的启动日志显示大量404

[W 23:35:49.743 NotebookApp] 404 GET /static/components/moment/locale/zh-cn.js?v=20171207233418 (127.0.0.1) 6.02ms referer=http://127.0.0.1:12345/tree?
[W 23:35:49.756 NotebookApp] 404 GET /i18n/zh-CN/LC_MESSAGES/nbjs.json?v=20171207233418 (127.0.0.1) 1.98ms referer=http://127.0.0.1:12345/tree?
[I 23:50:19.421 NotebookApp] 302 POST /login?next=%2F (10.172.192.178) 1.00ms
[I 23:50:19.429 NotebookApp] 302 GET / (10.172.192.178) 0.00ms
[W 23:50:19.724 NotebookApp] 404 GET /static/components/moment/locale/zh-cn.js?v=20171207233418 (10.172.192.178) 2.01ms referer=http://10.172.192.178:12345/tree?
[W 23:50:19.772 NotebookApp] 404 GET /i18n/zh-CN/LC_MESSAGES/nbjs.json?v=20171207233418 (10.172.192.178) 1.98ms referer=http://10.172.192.178:12345/tree?
问题分析:
302错误疑似notebook不应该放在d盘目录。
404错误疑似语言支持格式问题导致,对中文语言不适配。
问题处理:
对于学习python知识而言,无实质影响,暂不关注。
 

猜你喜欢

转载自www.cnblogs.com/anliven/p/10005041.html