1_Jupyter notebook 的使用(20181120)

1 Jupyter notebook的安装

3 Jupyter notebook远程访问

jupyter notebook 默认只开启了本地端口,需要使用jupyter_notebook_config.py配置jupyter服务器来实现远程浏览器登录

在安装完成jupyter后,很多时候需要从远端登录notebook来进行调试使用,这时候就需要将它设置为一个notebook server,从而实现远端访问.

总共分为三步

  1. 生成配置文件
  2. 设置密码
  3. 修改配置文件

3.1生成配置文件jupyter_notebook_config.py

为了生成配置文件,需要使用下面的jupyter命令

$ jupyter notebook --generate-config

此时就会得到一个配置文件,其默认路径一般如下所示:

Windows: C:\Users\USERNAME\.jupyter\jupyter_notebook_config.py
OS X: /Users/USERNAME/.jupyter/jupyter_notebook_config.py
Linux: /home/USERNAME/.jupyter/jupyter_notebook_config.py

Ubuntu 下一般会保存在~/.jupyter/jupyter_notebook_config.py

3.2 设置登录密码

  1. 自动设置(推荐)
    在jupyter5.0以后的版本,可以使用jupyter notebook password来设置密码:
$ jupyter notebook password
Enter password:  yourcode  #输入密码
Verify password: yourcodeagain   #再次输入密码确认

#运行后结果
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json #密码被保存的位置 ~/.jupyter/jupyter_notebook_config.json
在这里插入图片描述

  1. 手动设置
    #利用Ipython工具来设置密码
    #进入python环境也是可以的
In [1]: from notebook.auth import passwd    #导入授权模块设置密码
In [2]: passwd()
Enter password:    yourcode          #输入密码
Verify password:    yourcodeagain    #再次输入密码
Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'    #这是一串密码的哈希值

#将在~/.jupyter/jupyter_notebook_config.py配置文件中设置,需要保存好

#----------------advance-----------------#
#可选

$ jupyter notebook --certfile=mycert.pem --keyfile mykey.key

#用来设置安全通信协议

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem

#授权签名

3.3 修改配置文件

为了能在远程访问jupyter,需要修改刚刚生成的配置文件~/.jupyter/jupyter_notebook_config.py

对于自动模式
打开配置文件后修改三个地方,把前面的注释符号#去掉

c.NotebookApp.ip = '*'    #允许所有ip访问
c.NotebookApp.open_browser = False    #不打开浏览器
c.NotebookApp.port = 8888             #端口为8888

对于手动方法,除了上述修改之外,还需要配置哈希秘钥:
#配置刚刚生成的秘钥,一长串哈希码

c.NotebookApp.password = u'sha1:bcd259ccf...<your hashed password here>'

----------------------advanced--------------------------------#
选配认证和授权

c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
c.NotebookApp.keyfile = u'/absolute/path/to/your/certificate/mykey.key'

注意:此时启动就可以在局域网内的其它电脑上访问jupyter了,在浏览器输入192.168.1.111::8888(安装并启动了jupyter server的电脑ip)就有如下登录界面:

  • 在服务器terminal输入:jupyter notebook
  • 在本机PC上输入:服务器IP:Port ,然后再输入设置的访问密码即可登录服务器的Jupyter notebook服务,如下图所示
    在这里插入图片描述
    在这里插入图片描述

如果需要在外网访问,需要设置端口转发:利用路由器的端口转发功能或者使用花生壳等内网穿透来实现,将这台电脑的端口绑定到对应外网的ip的某个端口上。

3.4 错误解决方式

一切准备就绪的时候,输入Jupyter notebook报如下错误:

错误:ValueError: ‘’ does not appear to be an IPv4 or IPv6 address
在这里插入图片描述

解决方式

参见
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41010198/article/details/84306394