Jupyter Lab 高级用法
Jupyter Lab 是 Jupyter Notebook 的升级版,提供了类似 IDE 的多标签页界面,支持代码、Markdown、终端、文件浏览等功能并排显示,是数据分析和生信研究的日常工作台。
核心知识点
| 知识点 | 说明 |
|---|
| 工具定位 | 交互式开发环境(Notebook + IDE 混合体) |
| 最新版本 | JupyterLab 4.x |
| 核心优势 | 多标签页、拖拽布局、扩展系统、多语言内核 |
| 与 Notebook 关系 | JupyterLab 是 Notebook 的下一代界面 |
| 适用场景 | 数据分析、可视化、教学、论文图表制作 |
| 内核支持 | Python、R、Julia、Bash 等 40+ 种语言 |
安装配置
# 安装 JupyterLab
pip install jupyterlab # pip 安装
# 或
conda install -c conda-forge jupyterlab # conda 安装
# 或
uv tool install jupyter # uv 安装
# 启动
jupyter lab # 启动 JupyterLab
jupyter lab --port 8888 --no-browser # 指定端口,不自动打开浏览器
# 远程访问配置
jupyter lab --ip 0.0.0.0 --port 8888 --no-browser --allow-root
# 然后在浏览器访问 http://服务器IP:8888
安装 R 内核
# 在 R 中安装 IRkernel
conda install -c conda-forge r-irkernel # conda 方式
# 或在 R 中运行:
# install.packages('IRkernel')
# IRkernel::installspec()
基本使用
1. 常用快捷键
=== 命令模式(按 Esc 进入)===
A 在上方插入新单元格
B 在下方插入新单元格
DD 删除当前单元格
M 转为 Markdown 单元格
Y 转为代码单元格
Z 撤销删除
Shift+M 合并选中单元格
C / V 复制 / 粘贴单元格
=== 编辑模式(按 Enter 进入)===
Shift+Enter 运行当前单元格并跳到下一个
Ctrl+Enter 运行当前单元格(不跳转)
Alt+Enter 运行当前单元格并在下方插入新的
Tab 自动补全
Shift+Tab 查看函数文档
Ctrl+/ 注释/取消注释
2. 魔法命令(Magic Commands)
# 行魔法(单行生效)
%time result = heavy_computation() # 计时
%timeit x = [i**2 for i in range(1000)] # 多次计时取平均
# 单元格魔法(整个单元格生效)
%%time # 计时整个单元格
import pandas as pd
df = pd.read_csv("big_file.csv")
%%bash # 在单元格中运行 bash 命令
ls -la
wc -l data.csv
# 其他常用魔法命令
%who # 列出所有变量
%whos # 列出变量详细信息
%reset # 清除所有变量
%load script.py # 加载外部脚本到单元格
%store variable # 持久化变量(跨 notebook)
%matplotlib inline # 内联显示图表
3. Shell 命令
# 在代码单元格中运行 shell 命令
!ls -la # 列出文件
!pip install pandas # 安装包
!head -5 data.csv # 查看文件前 5 行
# 捕获 shell 输出
files = !ls *.csv # 捕获文件列表到变量
print(files)
高级用法
1. 安装扩展
# JupyterLab 4 使用 pip 安装扩展
pip install jupyterlab-git # Git 集成
pip install jupyterlab-lsp # 语言服务器(代码补全增强)
pip install jupyterlab-code-formatter # 代码格式化
# 查看已安装扩展
jupyter labextension list
2. 自定义配置
# 生成配置文件
jupyter lab --generate-config # 生成 jupyter_lab_config.py
# 常用配置
# 编辑 ~/.jupyter/jupyter_lab_config.py
c.ServerApp.ip = '0.0.0.0' # 允许远程访问
c.ServerApp.port = 8888 # 端口
c.ServerApp.open_browser = False # 不自动打开浏览器
c.ServerApp.notebook_dir = '/home/user/notebooks' # 默认工作目录
3. 多内核并行
# 在同一个 JupyterLab 界面中:
# - 用 Python 内核做数据处理
# - 用 R 内核做统计分析
# - 用 Bash 终端运行生信流程
# - 同时查看 Markdown 文档
# 拖拽标签页到右侧可以并排显示
4. 导出为其他格式
# 导出为 HTML
jupyter nbconvert --to html notebook.ipynb
# 导出为 PDF(需要 LaTeX)
jupyter nbconvert --to pdf notebook.ipynb
# 导出为 Python 脚本
jupyter nbconvert --to script notebook.ipynb
# 导出为 Markdown
jupyter nbconvert --to markdown notebook.ipynb
5. 性能优化技巧
# 大数据处理时减少输出
pd.set_option('display.max_rows', 20) # 限制显示行数
# 禁用自动显示(大数据集时)
; # 行末加分号可以抑制输出
# 使用 tqdm 显示进度条
from tqdm.notebook import tqdm
for i in tqdm(range(1000000)):
pass # 显示漂亮的进度条
常见报错与解决
| 报错信息 | 原因 | 解决方法 |
|---|
Kernel died | 内存不足 | 减少数据量或增加内存 |
ModuleNotFoundError | 包没安装到当前内核 | !pip install xxx 或检查内核环境 |
| 远程访问被拒绝 | 配置问题 | 设置 --ip 0.0.0.0 和防火墙 |
| 扩展不显示 | 版本不兼容 | 升级 JupyterLab 或扩展 |
速查表
# ===== JupyterLab 速查表 =====
# 安装启动
pip install jupyterlab
jupyter lab # 本地启动
jupyter lab --ip 0.0.0.0 --port 8888 # 远程启动
# 快捷键
# Shift+Enter 运行并下移
# Ctrl+Enter 运行不移动
# A/B 上/下方插入单元格
# DD 删除单元格
# M/Y Markdown/代码模式
# Tab 自动补全
# Shift+Tab 查看文档
# 魔法命令
# %time 计时
# %who 列出变量
# %%bash 运行 bash
# %matplotlib inline 内联图表
# 导出
jupyter nbconvert --to html notebook.ipynb
jupyter nbconvert --to pdf notebook.ipynb
jupyter nbconvert --to script notebook.ipynb
# 扩展
pip install jupyterlab-git
pip install jupyterlab-lsp