Jupyter Notebook是Ipython的升级版,而Ipython可以说是一个加强版的交互式 Shell
jupyter notebook的安装和打开
安装非常简单,在终端输入:
pip install jupyter
打开jupyter notebook 也只需要在终端输入:
jupyter notebook
远程访问配置
1. 生成登录密码
打开ipython,创建一个密文的密码:
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274'
2. 查看本地ip
ifconfig
# 一般为192.168.100.100 # 例如
3 生成配置文件
$jupyter notebook --generate-config
Writing default config to: /home/toucan/.jupyter/jupyter_notebook_config.py
4. 修改配置文件
复制原来的行,并修改,取消前面开头的#号
$vim ~/.jupyter/jupyter_notebook_config.py # 注意目录根据自己实际确定
进行如下修改:
c.NotebookApp.ip='*' # ifconfig的ip
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888 #随便指定一个端口
5. 登录
命令行输入
jupyter notebook
会显示一个登录的ip
http://192.168.100.100:8888/
在谷歌浏览器,输入这个网址,并输入ipython输入的用户登录密码
6. 配置完成
可以使用里面的markdown,ipython、shell的 窗口工具,这个工具最大的用处是用于画图的调试。和写演示教程。
快捷键
常用的快捷键是:
Ctrl + Enter: 执行单元格代码
Shift + Enter: 执行单元格代码并且移动到下一个单元格
Alt + Enter: 执行单元格代码,新建并移动到下一个单元格
这几个快捷键都是非常常用的。
历史输入和输出变量
与标准 Shell 类似,IPython 中也可以通过 _ 和 _ _ 访问上一次和上上一次的输出。
当你写的单元格多了,肯定会注意到,IPython 中每一次的输入输出都有序号。你可以通过一下方法访问这些输入和输出:
_:访问上一次输出
__:访问上上一次输出
_X:访问历史 X 行输出
_iX:访问历史 X 行输入
其中小写字母 “i”,代表 “in”。
外部功能
上面讲的是jupyter 的一些使用方法和技巧,其实它还有很多强大的外部功能的,下面给大家介绍一下。
A.魔术命令
在 IPython 的会话环境中,所有文件都可以通过 %run 命令来当做脚本执行,并且文件中的变量也会随即导入当前命名空间。
即,对于一个模块文件,你对他使用 %run 命令的效果和 from module import * 相同
这种以 % 开头的命令在 IPython 中被称为魔术命令,用于加强 shell 的功能。
常用的魔术命令有:
%quickref 显示 IPython 快速参考
%magic 显示所有魔术命令的详细文档
%debug 从最新的异常跟踪的底部进入交互式调试器
%pdb 在异常发生后自动进入调试器
%reset 删除 interactive 命名空间中的全部变量
%run script.py 执行 script.py
%prun statement 通过 cProfile 执行对 statement 的逐行性能分析
%time statement 测试 statement 的执行时间
%timeit statement 多次测试 statement 的执行时间并计算平均值
%who、%who_ls、%whos 显示 interactive 命名空间中定义的变量,信息级别/冗余度可变
%xdel variable 删除 variable,并尝试清除其在 IPython 中的对象上的一切引用
!cmd 在系统 shell 执行 cmd
output=!cmd args 执行cmd 并赋值
%bookmark 使用 IPython 的目录书签系统
%cd direcrory 切换工作目录
%pwd 返回当前工作目录(字符串形式)
%env 返回当前系统变量(以字典形式)
防火墙的开关
root用户权限
开启防火墙
ufw enable
关闭防火墙
ufw disable
后台永久运行jupyter book
每次用SSH远程登入打开服务器 关闭ssh 服务器也会关闭 所以我们要设置jupyter notebook在后台不间断运行,且配置成错误信息输出到屏幕(可选)
有时候,端口会变,需要指定端口,更好。
nohup jupyter notebook --ip=0.0.0.0 --port=8080 >/dev/null 2>&1 &
未试过
方法2
$cat jupyter_run.sh
jupyter notebook --ip=0.0.0.0 --port=8080
sh jupyter_run.sh
ctrl + z 暂停
后台运行
$bg 1
[1]+ sh jupyter_run.sh &
查看运行中
$jobs
[1]+ Running
安装R
要想在jupyter notebook中运行R语言其实非常简单,按顺序安装下面扩展包即可:
install.package('repr','IRdisplay','evaluate','crayon','pbdZMQ','devtools','uuid','digest')
library(devtools)
install_github("IRkernel/IRkernel")
IRkernel::installspec()
查看目录
$jupyter --config-dir
/home/toucan/.jupyter
$jupyter --data-dir
/home/toucan/.local/share/jupyter
$jupyter --runtime-dir
/run/user/1000/jupyter
$jupyter --paths
config:
/home/toucan/.jupyter
/opt/conda/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/home/toucan/.local/share/jupyter
/opt/conda/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/run/user/1000/jupyter
后期维护
- 设置IP地址
由于虚拟机经常变,设置为localhost比较好。如果有固定IP,则设置为固定IP
$vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip = 'localhost'
#c.NotebookApp.ip = '192.168.161.150'
输出显示:
http://localhost:8888/
- 设置启动固定目录
## The directory to use for notebooks and kernels.
#c.NotebookApp.notebook_dir = ''
c.NotebookApp.notebook_dir = '/'
启动方式
使用jupyter notebook,报错
OSError: [Errno 99] Cannot assign requested address
解决方法
jupyter notebook --ip=0.0.0.0 --port=8080
或者根据上面,设置IP为主机后,自动
jupyter notebook