【jupter notebook】设置 jupyter notebook 可远程访问

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

配置总共分为三步

生成配置文件
设置密码
修改配置文件


一、生成配置文件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

二、设置登录密码

  • 自动设置(推荐)
    在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
  • 对于手动方法,除了上述修改之外,还需要配置哈希秘钥:

#配置刚刚生成的秘钥,一长串哈希码
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,需要修改刚刚生成的配置文件~/.jupyter/jupyter_notebook_config.py

  • 对于自动模式
    打开配置文件后修改三个地方:
#把前面的#去掉
c.NotebookApp.ip = '*'    #允许所有ip访问  补充:报错 No address associated with hostname可设置为:'0.0.0.0'
c.NotebookApp.open_browser = False    #不打开浏览器
c.NotebookApp.port = 8888             #端口为8888

c.NotebookApp.allow_remote_access=True

 

第四步:在终端运行jupyter notebook

运行命令:  jupyter notebook --allow-root 

后台不挂起运行:nohup jupyter notebook --allow-root &

输出如下内容

第五步:在浏览器中访问

在浏览器中输入ip和端口号即可打开jupyter,进行下一步的代码调试

注意:此时启动就可以在局域网内的其它电脑上访问jupyter了,在浏览器输入192.168.1.111::8888(安装并启动了jupyter server的电脑ip)就有如下登录界面:
输入刚才设置的密码即可。
如果需要在外网访问,需要设置端口转发:利用路由器的端口转发功能或者使用花生壳等内网穿透来实现,将这台电脑的端口绑定到对应外网的ip的某个端口上。

icon from easyicon

ref:
doc:https://jupyter-notebook.readthedocs.io/en/latest/public_server.html#
简书:https://www.jianshu.com/p/803d09a0a92e
端口转发:https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/


参考:https://blog.csdn.net/u014636245/article/details/83652126 

树莓派 3b jupyter notebook 设置为后台服务

 

情景说明:
树莓派上已经搭建好了jupyter,命令jupyter notebook可以启动,在局域网/外网都可以通过浏览器访问.在终端内运行jupyter notebook会一直占据命令行.
后台实现:jupyter notebook > output.file 2&>1 & 这样可以实现把输出到命令行的信息输出到output.file ps:output.file 是自己新建的一个文件.
这里也有一个问题:当关闭终端或者远程连接的session,任务就会终止
关闭会话不关闭任务实现:
 

screen -s jupyter #创建一个jupyter窗口 	
jupyter-notebook  #运行jupyter-notebook 或者是jupyter notebook > output.file 2&>1 &

这里的问题是:断电重启之后原来的jupyter进程被关闭,需要重新启动

后台服务实现

  1. /etc/systemd/system 下创建jupyter.service输入如下内容
[Unit]
Description=Jupyter Notebook
After=network.target

[Service]
Type=simple
PIDFile=/run/jupyter.pid #这里在/run/目录下没有jupyter.pid,搜索相关信息发现,这个是进程产生之后出现的,虽然在启动前没有,但是可以使用.
ExecStart=/home/pi/.local/bin/jupyter-notebook --config=/home/pi/.jupyter/jupyter_notebook_config.py 
#ExecStart 是执行文件 --config是配置文件,之前我已经配置过jupyter_notebook_config.py,有的教程是配置.json文件(没试过)
User=pi
Group=pi #查看用户组 groups pi,发现属于pi组
WorkingDirectory=/home/pi/Desktop/jupyter_project #自己设置的工作目录,需要同时在jupyter_notebook_config.py中设置c.NotebookApp.notebook_dir = '/home/pi/Desktop/jupyter_project' 配置自己的工作目录
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
  1. 使服务自启动:systemctl enable jupyter.service
  2. 启动服务 service jupyter start

引用自链接搭建 Jupyter Notebook 服务器

猜你喜欢

转载自blog.csdn.net/bandaoyu/article/details/90706081