Build jupyter lab on the remote server and access it locally

1. Install jupyter

pip install jupyter

Can be installed directly in the base environment

2. Configure jupyter

2.1 Key generation

Enter python interactive mode and enter the following code:

from jupyter_server.auth import passwd
passwd()

Then enter the password, get a string of keys, and save it

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-OFNaiPfR-1683531071648) (D:\Memo Notes\Environment Construction\Server Environment jupyter.assets\image-20230508145110788 .png)]

2.2 Configuration file generation

Enter the following command to generate the default configuration file:

jupyter lab --generate-config, generate the configuration file in the user root directory~/.jupyter/jupyter_lab_config.py

2.3 Modify configuration file

Use the following command to modify the configuration file:

vim ~/.jupyter/jupyter_lab_config.py, this configuration file is full of commented-out content. For convenience, we add the content at the bottom:

c.ServerApp.allow_remote_access = True
c.ServerApp.ip = '*'

# 启动时不自动打开浏览器 
c.ServerApp.open_browser = False
c.LabServerApp.open_browser = False
c.ExtensionApp.open_browser = False
c.LabApp.open_browser = False

c.ServerApp.password_required = True
# 添加刚刚生成的密钥
c.ServerApp.password = 'argon2:$XXXXXXXXXXXXXXXXXXXXXXXXXXquAf8BGiNKI+ad54lcotVPfUT+ROgm5ggMZIgwb10'

3. Local access

If you want to access jupyter on the server locally, you need to run jupyter on the server and forward the port to the local machine. Here is the method I use.

3.1 Server starts jupyter

Use screen -S jupyterthe command to create a new window and execute it after entering the window jupyter labto enable jupyer. In window mode, press CTRL+a+d to exit the window. The command to re-enter isscreen -r name
Please add image description

3.2 Use SSH service to forward ports

Use cmd to execute the following command and enter the password to log in.

ssh -L 127.0.0.1:9000:localhost:9000 -X Name@Host, and then access http://127.0.0.1:9000/ with the browser to access jupyter lab

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-gQou09ra-1683531071650) (D:\Memo Notes\Environment Construction\Server Environment jupyter.assets\image-20230508152702305 .png)]

4. Install ipython in a virtual environment

After activating the virtual environment, executeconda install ipykernel

5. Display the virtual environment to jupyter

After activating the virtual environment, execute it python -m ipykernel install --user --name=env, where env is the name you want to display in jupyter

Guess you like

Origin blog.csdn.net/u014295602/article/details/130560221