jupyter notebook远程连接服务器及安装扩展时遇到的坑

    xiaoxiao2022-07-07  228

    文章目录

    1.jupyter notebook远程连接服务器1.1 生成一个 notebook 配置文件1.2 生成密码自动生成手动生成(本人采用的方法) 1.3 修改配置文件 2. 添加jupyter-notebook的扩展

    1.jupyter notebook远程连接服务器

    原链接: jupyter notebook远程连接服务器

    设置 jupyter notebook 可远程访问的官方指南在这里,在远端服务器上执行以下操作:

    1.1 生成一个 notebook 配置文件

    默认情况下,配置文件 ~/.jupyter/jupyter_notebook_config.py 并不存在,需要自行创建。使用下列命令生成配置文件:

    jupyter notebook --generate-config

    如果要在特定的虚拟环境中使用jupyter-notebook的话,就先activate这个环境,然后输入jupyter notebook --generate-config

    无论是不是在特定虚拟环境中生成配置文件,配置文件都会默认的目录中。比如,如果是linux系统,就会存放在/home/user/目录下

    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

    如果是 root 用户执行上面的命令,会发生一个问题:

    Running as root it not recommended. Use --allow-root to bypass.

    提示信息很明显,root 用户执行时需要加上 --allow-root 选项。

    jupyter notebook --generate-config --allow-root

    执行成功后,会出现下面的信息:

    Writing default config to: /root/.jupyter/jupyter_notebook_config.py

    1.2 生成密码

    自动生成

    从 jupyter notebook 5.0 版本开始,提供了一个命令来设置密码:jupyter notebook password,生成的密码存储在 jupyter_notebook_config.json。

    $ jupyter notebook password Enter password: **** Verify password: **** [NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json

    手动生成(本人采用的方法)

    除了使用提供的命令,也可以通过手动安装。我使用的是这种。 这里你输入并确认的密码(如 123456),就是最终打开浏览器时要输入的密码。 在终端中输入 python,进入解释器

    $ python Python 3.6.6 | packaged by conda-forge | (default, Oct 12 2018, 14:08:43) [GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from notebook.auth import passwd >>> passwd() Enter password: # 输入你的密码,如 123456 Verify password: # 再次确认密码 # 得到一串长长的密码 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

    sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed 这一串就是要在 jupyter_notebook_config.py 添加的密码。

    c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11a'

    1.3 修改配置文件

    在 jupyter_notebook_config.py 中找到下面的行,取消注释并修改。

    c.NotebookApp.ip='localhost' c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11a' # 刚才复制的那个密文 c.NotebookApp.open_browser = False c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口

    以上设置完以后就可以在服务器上启动 jupyter notebook,jupyter notebook, root 用户使用 jupyter notebook --allow-root。打开 IP:指定的端口, 输入密码(刚才设置的123456)就可以访问了。

    jupyter notebook --ip=192.168.xxx.xxx(你的服务器的ip地址) --no-browser [I 20:12:38.096 NotebookApp] [jupyter_nbextensions_configurator] enabled 0.4.1 [I 20:12:38.096 NotebookApp] 启动notebooks 在本地路径: /sdd/user/demo [I 20:12:38.096 NotebookApp] 本程序运行在: http://192.168.123.24:8888/ [I 20:12:38.096 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).

    第二条,启动notebooks 在本地路径: /sdd/user/demo 这里的路径就是输入命令时的当前路径

    需要注意的是不能在隐藏目录 (以 . 开头的目录)下启动 jupyter notebook, 否则无法正常访问文件。

    2. 添加jupyter-notebook的扩展

    原文链接:我知道你会用Jupyter Notebook,但这些插件你都会了吗?

    在命令提示符中运行以下命令:

    pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install

    但是会报错:

    Errno 13] Permission denied: '/usr/local/share/jupyter'

    解决方法: 参考:https://github.com/Calysto/matlab_kernel/issues/68

    在命令的最后加上 --user

    pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install --user
    最新回复(0)