注意:以下操作均在服务器上操作
1)确认是否已经安装jupyter notebook
如未安装,打开终端输入
sudo pip install jupyter
2)生成配置文件
终端中输入
jupyter notebook --generate-config
生成的配置文件(一般配置文件的位置 ~/.jupyter/jupyter_notebook_config.py ),后面需要用到。注意:如果之前安装过,可能会提示你是否要重置配置文件,推荐不要,例如本机上重置可能会导致jupyter notebook无法正常在浏览器打开。
3)生成密码(后续写配置文件、登录Jupyter notebook需要,需要输入一大堆东西)
打开python终端,即
# 1、在服务器终端输入 python 或 ipython
In [1]: from IPython.lib import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:xxxxxxxxxxxxxxxxxxxxxx(一段密文)'
演示如下
执行完之后退出python终端,具体命令为:
exit()
接下来生成秘钥:
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
# 按照提示填写一大堆东西
填写完成后,会产生一个mycert.pem文件(一般在工作目录下,地址后面会需要)
创建一个服务器配置:
ipython profile create nbserver
4)修改默认配置文件
vim ~/.jupyter/jupyter_notebook_config.py
进行如下修改(这里可以自行配置):
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha1:xxxxxxxxxxxxxxxxxxxxxx(一段密文)刚才复制的那个密文'
c.NotebookApp.port = 9999 #随便指定一个端口
c.NotebookApp.certfile = u'/root/.jupyter/mycert.pem'
# 注意这里要用绝对路径,我在这里踩坑了
5)设置Jupyter Notebook文件存放位置
⑴ 创建文件夹/目录
在想要存放Jupyter Notebook文件的位置创建目录并为目录命名:
mkdir [workfile]
cd [workfile]
pwd #得到文件夹路径
⑵ 配置文件路径
配置文件所在路径和配置文件名如下所述:
/Users/<user_name>/.jupyter/ 或 ~/.jupyter/
配置文件名:jupyter_notebook_config.py
⑶ 修改配置文件
① 打开配置文件
vim ~/.jupyter/jupyter_notebook_config.py
进入配置文件后查找关键词“c.NotebookApp.notebook_dir”。查找方法如下:
/c.NotebookApp.notebook_dir
③ 编辑配置文件
c.NotebookApp.notebook_dir = u'/home/username/ipython'
https://github.com/ipython-contrib/jupyter_contrib_nbextensions/blob/master/README.md
1)安装python package
pip install jupyter_contrib_nbextensions
2)再执行第二个命令,用于安装 javascript and css files
jupyter contrib nbextension install --user
3)最后执行,用于安装configurator
pip install jupyter_nbextensions_configurator
4)生效/取消扩展
#enable
jupyter nbextension enable <nbextension require path>
#disable
jupyter nbextension disable <nbextension require path>
例如:
jupyter nbextension enable codefolding/main
5)选择生效插件
Collapsible Headings
允许笔记本有可折叠的部分,用标题隔开.允许笔记本有可折叠的部分,用标题隔开.任何标记的标题单元格(也就是以1-6字符开头的单元格),一旦呈现,就会变成可折叠的.标题的折叠/扩展状态存储在单元元数据中,并在笔记本加载上重新加载.
Highlighter
通过向网页文本中添加标记颜色的css标记,从而改变输出颜色的方法.也就表示,这对于以代码(code)表示的可执行文件无效,对未运行的markdown文件无效,对于已经运行的markdown文本有效.
Table of Contents
目录,可以将所有的heade标题栏收集起来,只对于已经运行的markdown类型的标题有效.通过目录可以进入指定链接.
Hinterland
代码自动补全,选中
[x] Whether to request hints while typing code comments.
则在进行注释时也会有代码补全的提醒.并且此插件不需要用空格的形式或者是Tab键的形式.
ExecuteTime
执行时间,用于显示程序代码执行时间
如果隐藏时间可以双击显示时间的条目,或者Cell -> Toggle timings -> Selected menu item
如果再点击一下表示显示.同理对于所有cell的执行时间,可以通过
Cell -> Toggle timings -> All
Notify 通知机制,跑一些耗时较久的任务,完成后通知
Freeze
将代码cell进行freeze冰冻操作或者是read-only只读操作.
对于code-cells:
Read-only:它可以被执行,但是它的代码不能被改变。
Freeze:它不能被改变或被执行。对于markdown:
Read-only:它的markdown代码可以通过双击它来查看,但是不能改变。
Freeze:不能通过双击来查看markdown代码。
https://github.com/dunovank/jupyter-themes
1)安装主题包
# install jupyterthemes
pip install jupyterthemes
# upgrade to latest version
pip install --upgrade jupyterthemes
2)配置主题
jt [-h] [-l] [-t THEME] [-f MONOFONT] [-fs MONOSIZE] [-nf NBFONT]
[-nfs NBFONTSIZE] [-tf TCFONT] [-tfs TCFONTSIZE] [-dfs DFFONTSIZE]
[-m MARGINS] [-cursw CURSORWIDTH] [-cursc CURSORCOLOR] [-vim]
[-cellw CELLWIDTH] [-lineh LINEHEIGHT] [-altp] [-altmd] [-altout]
[-P] [-T] [-N] [-r] [-dfonts]
# list available themes
# onedork | grade3 | oceans16 | chesterish | monokai | solarizedl | solarizedd
jt -l
# select theme...
jt -t chesterish
# restore default theme
# NOTE: Need to delete browser cache after running jt -r
jt -r
例如:
jt -t grade3 -f fira -fs 11 -cellw 90% -ofs 11 -dfs 11 -T -N -kl
1)将此代码放在Jupyter单元格中:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
2)以下步骤使更改成为永久更改。可能需要更改路径。
\.ipython\profile_default
使用以下代码在profile_defaults中创建ipython_config.py文件:
c = get_config()
c.InteractiveShell.ast_node_interactivity = "all"
Step-1: 在远程服务器上,启动jupyter notebooks服务:
jupyter notebook --no-browser --port=9999
Step-2: 在本地机器的Terminal中启动SSH:
如果从本地浏览器直接访问失败,则有可能是服务器防火墙设置的问题,此时最简单的方法是在本地建立一个ssh通道
ssh -N -f -L localhost:8888:localhost:9999 remote_user@remote_host
其中: -N 告诉SSH没有命令要被远程执行; -f 告诉SSH在后台执行; -L 是指定port forwarding的配置,远端端口是8889,本地的端口号的8888。remote_user@remote_host 用实际的远程帐户和远程地址替换
Step-3: 打开浏览器,输入地址:
http://localhost:8888/
#这里如果连接不上服务器,在服务器终端会报错,后面会有解析
问题类型一、服务器端 运行 jupyter notebook 报错
[C 10:20:53.426 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.
原因: 一般是因为 jupyter 安装在非 root用户的目录下,故默认不支持Cent OS 用户直接运行。此时实际上 jupyter notebook 服务并未启动,故使用不了。
解决方法: 按提示加入命令参数即可(或切换到 非 root 用户,也可以)
jupyter notebook --allow-root
[C 10:17:49.786 NotebookApp] Bad config encountered during initialization:
[C 10:17:49.786 NotebookApp] No such notebook dir: u'/home/username/ipython'
最后是这两行,上面还有一大串
原因: 检查配置文件,发现 c.NotebookApp.notebook_dir = u’/home/me/ipython’,该目录在 Cent OS 中不存在。
解决方法: 手动创建目录即可
问题类型二、无法访问(假设地址为:192.168.20.2)
原因: 防火墙拦截 。
解决方法: 关闭防火墙,或设置开放指定端口(推荐)。
①关闭防火墙
[root@centos .jupyter]# systemctl stop firewalld.service
②设置开放指定端口,重启防火墙
[root@server240 .jupyter]# firewall-cmd --zone=public --add-port=8888/tcp --permanent
success
[root@server240 .jupyter]# systemctl restart firewalld.service
[W 10:59:32.921 NotebookApp] SSL Error on 20 ('172.31.159.149', 60102): [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:579)
原因: jupyter配置了 ssl 加密,客户端浏览器访问时默认是 http://xxxxx,故拒绝访问。
解决方法: 浏览器地址 加上 https://
即可
原因: 登录成功说明 jupyter notebook 服务正常启动了,404 找不到页面是因为 工作目录有问题。核查后,发现配置文件 jupyter_notebook_conifg.py 中缺少对工作目录的设置
解决方法: 在 jupyter_notebook_config.py 中配置 工作目录 ,即加入如下一行:
c.NotebookApp.notebook_dir = u'/home/username/ipython'
此时配置文件的完整内容如下:
c.NotebookApp.ip='*'
c.NotebookApp.password = u'sha1:ce1b91965355:c5acf3d59b844c67d0e9a6b038eda7e1ad830ee1'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
c.NotebookApp.certfile = u'/home/me/.jupyter/mycert.pem'
c.NotebookApp.notebook_dir = u'/home/me/ipython'
此时启动服务器python环境过程:
#Server
jupyter notebook
#client
#cmd(根据服务器防火墙配置情况可选)
ssh -N -f -L localhost:8888:localhost:9999 remote_user@remote_host
#browser
http://ServerIP:9999
#browser
https://localhost:8888