一、Jupyterlab簡介
JupyterLab是一個交互式的開發(fā)環(huán)境,是Jupyter Notebook的下一代產(chǎn)品,某種意義上,它的出現(xiàn)是為了取代Jupyter Notebook。不過不用擔(dān)心Jupyter Notebook會消失,JupyterLab包含了Jupyter Notebook所有功能。JupyterLab作為一種基于web的集成開發(fā)環(huán)境,可以使用它編寫Notebook、操作終端、編輯MarkDown文本、打開交互模式、查看csv文件及圖片等功能。
JupyterLab是開發(fā)者們下一階段更主流的開發(fā)環(huán)境。JupyterLab支持更加靈活和更加強(qiáng)大的項目操作方式,具有和Jupyter Notebooks一樣的組件。
Jupyterlab的github網(wǎng)站:https://github.com/jupyterlab/jupyterlab
Jupyterlab說明文檔:https://jupyterlab.readthedocs.io/en/stable/
Jupyter lab和Jupyter notebook的區(qū)別
- Jupyter Notebook 是一個款以網(wǎng)頁為基礎(chǔ)的交互計算環(huán)境,可以創(chuàng)建Jupyter的文檔,支持多種語言,包括Python, Julia, R等等。廣泛用于數(shù)據(jù)分析,數(shù)據(jù)可視化和其他的交互和探索性計算中。
- JupyterLab 是包括了Notebook的下一代用戶界面。有模塊化的界面,可以在同一個窗口同時打開好幾個notebook或文件(HTML, TXT, Markdown等等),都以標(biāo)簽的形式展示,于是就更像是一個IDE。
JupyterLab有以下特點(diǎn):
- 交互模式:Python交互式模式可以直接輸入代碼,然后執(zhí)行,并立刻得到結(jié)果,因此Python交互模式主要是為了調(diào)試Python代碼用的
- 內(nèi)核支持的文檔:使你可以在可以在Jupyter內(nèi)核中運(yùn)行的任何文本文件(Markdown,Python,R等)中啟用代碼
- 模塊化界面:可以在同一個窗口同時打開好幾個notebook或文件(HTML, TXT, Markdown等等),都以標(biāo)簽的形式展示,更像是一個IDE
- 鏡像notebook輸出:讓你可以輕易地創(chuàng)建儀表板
- 同一文檔多視圖:使你能夠?qū)崟r同步編輯文檔并查看結(jié)果
- 支持多種數(shù)據(jù)格式:你可以查看并處理多種數(shù)據(jù)格式,也能進(jìn)行豐富的可視化輸出或者M(jìn)arkdown形式輸出
- 云服務(wù):使用Jupyter Lab連接Google Drive等服務(wù),極大得提升生產(chǎn)力
優(yōu)先支持的瀏覽器
- Firefox
- Chrome
- Safari
安裝的目的是在服務(wù)器運(yùn)行JupyterLab,然后在本地瀏覽器訪問,操作
1.1 Jupyter lab安裝
JupyterLab可以通過conda, mamba, pip, pipenv or docke安裝。
安裝anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
bash Anaconda3-2021.05-Linux-x86_64.sh
anaconda安裝在/home/yourname/anaconda3。安裝過程一路yes即可。
安裝mamba
Mamba是用C++重新實現(xiàn)的conda包管理器,支持多線程與并行下載,并且依賴解析速度大幅提升。在已有conda的情況下只需要一行命令安裝
conda install -n base -c conda-forge mamba
然后就可以將原來命令里的conda直接替換成mamba。mamba的設(shè)計目標(biāo)就是與conda兼容。
使用anaconda創(chuàng)建一個新的虛擬環(huán)境,并在其中安裝nodejs和jupyterlab,強(qiáng)烈推薦通過mamba安裝,速度快很多:
conda create -p ~/env/Jupyter
conda activate ~/env/Jupyter/
mamba install -c conda-forge nodejs
mamba install -c conda-forge jupyterlab
生成Jupyterlab的配置文件:
jupyter-lab --generate-config
Writing default config to: /home/yourName/.jupyter/jupyter_lab_config.py
進(jìn)入Jupyter虛擬環(huán)境中python,生成Jupyterlab密碼哈希值(以密碼123456為例):
from jupyter_server.auth import passwd
passwd()
Enter password:
Verify password:
'argon2:$argon2id$v=19$m=10240,t=10,p=8$3GmksmJCRj/N/whpQE24lQ$8FmVHwfJYAUmtK5BHcQ4Cg'
生成的字符串便是經(jīng)過哈希加密后的密碼,將其復(fù)制并黏貼到Jupyterlab配置文件中,修改JupyterLab配置文件:
vim /home/yourName/.jupyter/jupyter_lab_config.py
# password
c.ServerApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$bKDhVGXZbLQDpvzXV7NESQ$gBSXW9cxTMYMSLiL8jVOhA'
c.ServerApp.password_required = True
c.ServerApp.allow_root = True
# 若反向代理開啟并且沒有掛在根目錄下,則需要修改這一項
c.ServerApp.allow_origin = '*'
# 反向代理路徑,兩者保持一致
c.ServerApp.base_url = '/jupyter/'
c.ServerApp.custom_display_url = '/jupyter/'
# ip與端口,需與nginx代理配置一致 ,'XXXX'需要替換成服務(wù)器ip
c.ServerApp.ip = 'XXXX'
c.ServerApp.port = 8788
# 默認(rèn)路徑
c.ServerApp.notebook_dir = u'/home/yourName/Jupyter'
# 遠(yuǎn)程訪問
c.ServerApp.allow_remote_access = True
c.ServerApp.open_browser = False #不打開瀏覽器
安裝擴(kuò)展:
jupyter serverextension enable --py jupyterlab --user
# 安裝一個生成目錄的插件
jupyter labextension install @jupyterlab/toc
# 可以查看一下安裝的插件
jupyter labextension list
安裝好后在xshell輸入命令jupyter lab打開測試一下。
jupyter lab
打開本地瀏覽器查看yourServer_IP:8788/jupter

yourServer_IP:8788/jupyter
輸入上面設(shè)置的密碼:123456

上面的方式,如果xshell斷連,Jupyterlab啟動不了,改成Jupyterlab開機(jī)自啟模式。
進(jìn)程守護(hù)開啟Jupyterlab
使用傻狗先生的教程配置文件的時候在服務(wù)器上出現(xiàn)一些小問題,應(yīng)該是我的配置文件設(shè)置不對,

嘗試的是以下方法:
最終采用的方法是,下面的配置是重點(diǎn)
vim /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook server
After=network.target
#StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=yourName
Group=yourName
ExecStart=/home/yourName/env/Jupyter/bin/jupyter lab
Environment="PATH=/home/yourName/anaconda3/bin:/home/yourName/anaconda3"
[Install]
WantedBy=multi-user.target
#----------------------------------
chmod a+x /etc/systemd/system/jupyter.service
更新配置
systemctl daemon-reload
# 服務(wù)立即啟動 下次不啟動
systemctl start jupyter.service
# 停止服務(wù)
systemctl stop jupyter.service
# 重啟服務(wù)
systemctl restart jupyter.service
# 查看狀態(tài)
systemctl status jupyter.service
# 將服務(wù)設(shè)置為每次開機(jī)啟動
systemctl enable jupyter.service


跟之前的界面一樣
參考:
1.https://medium.com/@desjoerdhaan/manage-your-jupyter-server-with-systemd-9914ff39049d
3.https://www.ishelo.com/archives/331
2.https://blog.mrdoge.cool/index.php/2021/06/08/tech/nginx_for_rstudio_jupyterlab
二、nginx代理
2.1 nginx簡介
nginx網(wǎng)站:https://www.nginx.com/
Nginx 是一個高性能的網(wǎng)絡(luò)服務(wù)器軟件。 它是一個比 Apache HTTP Server 更加靈活和輕量級的程序。
nginx與apache,tomcat的區(qū)別

2.2 nginx安裝
在CentOS 使用yum安裝軟件的時候,出現(xiàn)報錯no more mirrors to try,導(dǎo)致軟件無法繼續(xù)安裝,可能是網(wǎng)絡(luò)問,放棄此方法。
failure: repodata/a3546ceff5c3fded9f99f918376a5e5922d554433032e539ee187de4219aef0d-primary.sqlite.bz2 from updates: [Errno 256] No more mirrors to try.
改用手動安裝
# 下載 Nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
# 進(jìn)入安裝包目錄
cd nginx-1.8.0
# 編譯安裝
./configure --prefix=/home/yourName/software/nginx
make
make install
# 查看nginx版本
/home/yourName/software/nginx/sbin/nginx -v
# nginx version: nginx/1.8.0
打開nginx配置文件/etc/nginx/nginx.conf,修改http部分:
vim /home/yourName/software/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 8220;
server_name yourServer_IP;
charset koi8-r;
location / {
rewrite / yourServer_IP permanent;
}
# the jupyter path, note to keep port and path agree with your jupyterlab config
location /jupyter/ {
proxy_pass http://yourServer_IP:8790/jupyter/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
測試配置文件是否正確:
/home/yourName/software/nginx/sbin/nginx -s reopen
/home/yourName/software/nginx/sbin/nginx -s reload
/home/yourName/software/nginx/sbin/nginx -t
nginx: the configuration file /home/yourName/software/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/yourName/software/nginx/conf/nginx.conf test is successful

http://yourServer_IP:8220/jupyter

如有報錯,查看nginx日志
位置/home/yourName/software/nginx/logs/error.log
安裝過程會遇到一些小問題,跟自身服務(wù)器的相關(guān)配置有關(guān)系,多查詢問題和查看日志報錯。