Python-搭建Jupyternotebook环境
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Python-搭建Jupyternotebook环境
1- Jupyter简介
HomePage:
安装指南:
官⽅⽂档:
Jupyter提供⼀个开源的基于浏览器的Notebook,可以开发、协作、分享甚⾄发布数据科学结果:
可⽤于创建和共享代码与⽂档⽹络应⽤;
可在其中编写并运⾏代码、查看输出、可视化数据并查看结果;
不仅⽀持 Python,还包括⽤于Julia、 R和其他编程语⾔;
与IPython的联系与区别:
Jupyter是从IPython中迁移出来的独⽴应⽤;
从IPython4.0(包含)之后分离成IPython和Jupyter,IPython只具有终端接⼝(安装的依赖包少很多);
分离之后的Jupyter可以被更加⼴泛的使⽤,⽀持多种语⾔,并不断加⼊新特性;
2- 安装Jupyter
⽅式⼀:安装Anaconda(包含Jupyter notebook)--- 推荐
⽅式⼆:通过pip安装(同时也会安装包括Ipython等⼤量依赖库)
$ pip install jupyter -i https:///pypi/web/simple/
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 将运⾏此代码,显⽰代码中最后⼀个表达式的值;
如果希望屏蔽输出,可以在最后⼀条语句后添加⼀个分号”;”;
Code Cell的状态
In[数字]:表⽰已经执⾏的代码段,其中数字代表顺序编号
In[*]:表⽰正在执⾏的代码段,括号⾥显⽰星号*
In[]:表⽰未执⾏的代码段,括号⾥为空
如果Code Cell状态长时间显⽰In[*],⽆法执⾏代码,可能是上⾯的执⾏框中出现错误,导致下⾯框中的代码也执⾏不了。
点击Kernel中的“Restart & Run all”来执⾏全部代码。
从上向下检查每⼀个框,找到错误代码并修改。
特别注意:如果下⾯已经运⾏出了执⾏结果,但是上⾯⼜修改了,并且出错了,那么重新执⾏的时候下⾯的执⾏结果还会保留,因为没有执⾏到这⾥来,程序在上⾯就已经出错退出了。
Cell的类型
使⽤单元(Cell)保存信息,种类包括表⽰格式化⽂本的Markdown单元和表⽰代码的Code单元等;
Code cells:⽤于写代码;
Markdown cells:写markdown⽂档,⽤来写说明,在Markdown单元中可以直接使⽤Html和Javascript;
Raw NBConvert:代码或⽂档的输出信息,可将笔记本转换成另⼀种格式(⽐如 HTML)的命令⾏⼯具;
Heading:添加标题,遵循Markdown格式;
在函数、模块、类后⾯输⼊‘?’,按住Ctrl-Entry显⽰帮助⽂档,如果是两个‘??’则显⽰源代码;
以%开始的魔术命令只对单⾏有效;
以%%开头的魔术命令放在单元的第⼀⾏,对整个单元有效;
%prun⽤于代码的执⾏性能分析,可以作为⾏命令和单元命令使⽤;
如果想关闭已经打开的终端和“ipynb”格式的笔记本,仅仅关闭其页⾯是⽆法彻底退出程序的,必须点击Running标签中对应的关闭按钮。
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
⽇志信息包括笔记⽂件的存放⽬录、启动的IP地址和端⼝、关闭⽅法等;
$ 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的登陆界⾯。
7- ⼀些设置
7.1- 更改Jupyter notebook的⼯作空间
默认显⽰⽤户⽬录下的⾮隐藏⽂件和⽬录,例如“C:\Users\guowli\”;
1. 执⾏“jupyter notebook --generate-config”命令获取配置⽂件的位置;
2. 修改jupyter_notebook_config.py中的c.NotebookApp.notebook_dir参数,将值更改为实际的⼯作空间⽬录即可;
例如:
注意:如果不想在配置⽂件中指定默认⽬录,可以尝试在Windows系统CMD窗⼝中进⼊到指定⽬录,执⾏“jupyter notebook”即可。
7.2- 实现同时⽀持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内核
7.3- 修改默认浏览器
在配置⽂件jupyter_notebook_config.py的c.NotebookApp.browser参数下,添加如下内容并保存。
import webbrowser
webbrowser.register("chrome", None, webbrowser.GenericBrowser(u"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"))
c.NotebookApp.browser = 'chrome'
注意:与chrome.exe实际所在路径⼀致,并且使⽤“\\”符号来分割路径地址。
7.4- 管理插件
使⽤Jupyter NbExtensions Configurator可以简易地在Jupyter notebook中添加、设置、启⽤、禁⽤插件。
NbExtensions:
安装NbExtensions之后重启Jupyter Notebook,导航栏将出现“Nbextensions”的类⽬,点击“Nbextensions”,将看到所有的插件列表。
# 通过conda安装Nbextensions:
conda install -c conda-forge jupyter_nbextensions_configurator
# 通过pip安装Nbextensions:
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
根据需要勾选(启⽤)相应的插件,例如:
Codefolding:折叠代码
Code prettify:它能重新调整代码块内容的格式并进⾏美化
Collapsible Headings:使得各级标题可缩进
Notify:For long running task, the notify extension sends a notification when the notebook becomes idle.
Table of Contents (2):⽣成Markdown内容的⽬录结构
7.5- 安装nb_conda
nb_conda可以关联Jupyter Notebook与conda的环境和包
通过conda安装nb_conda:conda install nb_conda
完成安装后,conda创建的环境将与Jupyter Notebook相关联,可以在Conda类⽬下对conda环境和包进⾏⼀系列操作,也可以在笔记本内的“Kernel”类⽬⾥的“Change kernel”切换内核。
8- 通过nbviewer查看“.ipynb”⽂件
⽰例:
如果GitHub上的.ipynb⽂件链接为:https:///anliven/Hello-Data/blob/master/test.ipynb
使⽤nbviewer打开此⽂件的地址为:https:///github/anliven/Hello-Data/tree/master/test.ipynb
9- 参考信息
必须会的Jupyter Notebook:
始于Jupyter Notebooks:⼀份全⾯的初学者实⽤指南
Jupyter Notebook介绍、安装及使⽤教程:
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知识⽽⾔,⽆实质影响,暂不关注。