本文最后更新于 2024-11-20 10:49

一 概述

Jupyter 是一个在浏览器中使用的交互式的笔记本,可以非常方便的用于调试python代码

二 安装

建议读者先安装conda,然后在conda环境中安装jupyter

注:只要某个环境安装了 JupyterLab,它就可以通过注册(ipykernel)访问其他环境的 Python 解释器。所以只需在一个环境安装jupyter。

  1. 创建一个conda环境来安装jupyter,建议python使用3.10以上的版本

    conda create -n jupyter_env python=3.10
  2. 激活环境

    conda activate jupyter_env
  3. 安装jupyterlab,通过中科大镜像下载

    pip install jupyterlab --default-time=99999 -i https://pypi.mirrors.ustc.edu.cn/simple
  4. 生成配置文件

    jupyter server --generate-config # 执行完此命令后会提示已写入配置到jupyter_server_config.py文件

    image-20241107215217921

  5. 设置密码

    jupyter server password

    image-20241107215538412

    显示将密码加密为哈希字符,并存储在名为jupyter_server_config.json文件中

  6. 复制jupyter_server_config.json文件中的密码(不要复制双引号)
    image-20241107215826957

  7. 打开第四步中生成的配置文件,一般是 $HOME/.jupyter/jupyter_server_config.py 文件。然后找到c.ServerApp.password字段,取消前面的注释,并填写内容为刚刚复制的哈希值,如下:

    image-20241107220218974

    找到c.ServerApp.allow_remote_access字段,将其修改为True,使jupyter可以远程登录

    image-20241107220300867

    找到c.ServerApp.notebook_dir字段,并将其修改为c.ServerApp.root_dir='', 引号里面可以填写任意路径,如果不写的话就默认是自己的 $HOME 目录,注意必须是绝对路径
    image-20241107224914665

  8. 启动 jupyterlab:

    jupyter-lab --port=1003 --no-browser --ip=0.0.0.0
    # --port 用于指定jupyterlab的端口
    # --no-browser 表示静默启动,不在浏览器中打开
    # --ip=0.0.0.0 表示不限制访问该服务的ip地址来源
    启动完成后,使用ip:端口的形式,就可以访问jupyterlab的服务了

三 远程访问

一般来说,在进行上述操作后,可能还是不能访问jupyterlab,因为运行jupyterlab服务都是远端的Linux节点,通常都会遇到端口的问题。我们需要使用端口转发,将远端的jupyterlab服务,转发到我们本地的Windows电脑上,下面介绍常用的端口转发和frpc配置

3.1 端口转发

打开cmd命令行,输入以下命令:

ssh -L local_port:localhost:remote_port username@server_ip -p ssh_port
● local_port: 你本地机器上希望用于访问 JupyterLab 的端口号。
● localhost: 这里指的是服务器端,因为 SSH 命令登录到服务器后,localhost 会被解析为服务器本身。
● remote_port: 运行 JupyterLab 的服务器上的端口号,这里是 8888。
● username: 你在服务器上的用户名。
● server_ip: 服务器的 IP 地址。
● ssh_port: 服务器的ssh端口,一般都是22

举例:

假设你的服务器 IP 是 173.0.36.105,用户名是 qiql,ssh端口是1001,并且 JupyterLab 正在服务器上的 8888 端口运行。你希望通过你的本地机器的 9999 端口访问它:

ssh -L 9999:localhost:8888 qiql@173.0.36.105 -p 1001

在cmd中输入密码后即可打开浏览器,输入localhost:9999即可打开jupyterlab服务(密码填写之前所输入的密码,不是哈希加密后的密码)

注意:这个cmd窗口需要一直开着,如果关闭窗口转发也会失效

3.2 公网访问

如果希望这个jupyterlab服务可以直接公网访问,比如直接使用域名进行访问,需要读者自行准备一台公网Linux服务器,老手可以直接看以下的配置,新手建议先阅读:https://docs.aheadai.cn/58.html

在frpc.ini 中,进行以下配置

[jupyter]
type=http
local_ip = 127.0.0.1 
local_port = 1003 # jupyter的端口号
custom_domains = xxxxxx.com # 配置的域名

在nginx.conf中,进行以下配置:

    server {
        listen       443 ssl;
        server_name  jupyter.xxxxxx.com;
        access_log  logs/host-https.jupyter.xxxxxx.com.access.log  main;
        ssl_certificate /etc/letsencrypt/live/xxxxxx.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/xxxxxx.com/privkey.pem;
        ssl_trusted_certificate  /etc/letsencrypt/live/xxxxxx.com/chain.pem;

        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-Scheme $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://127.0.0.1:7001;
            client_max_body_size 40G;
            # WebSocket support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            proxy_read_timeout 120s;
            proxy_next_upstream error;
            proxy_buffering off;

        }
        error_page  500 502 503 504  /50x.html;

        location /50x.html {
            root   /usr/local/nginx/1.23.0/html;
        }
    }

四 配置jupyterlab

  1. 安装中文插件

    pip install jupyterlab-language-pack-zh-CN
  2. 将conda环境添加到jupyter中

    python -m ipykernel install --user --name=myenv
  3. 配置开启脚本

    echo "jupyter-lab --port=1003 --no-browser --ip=0.0.0.0 >> ~/run-jupyter.log 2>&1 & "  >> ~/start-jupyter.sh
    chmod +x ~/start-jupyter.sh
    # 写完脚本后,以后就可以直接使用 ~/start-jupyter.sh 命令直接启动jupyterlab了
  4. 关闭 jupyterlab

    # 查询jupyter的进程号
    ps -ef | grep jupyter
    # 用户名后面那一列就是进程号
    kill 进程号
    
    # 也可以直接杀死名为jupyter的进程(root账号慎用)
    pkill  jupyter
本文系作者 @ admin 原创发布在 文档中心 | AheadAI ,未经许可,禁止转载。