JupyterLab 扩展示例教程
项目介绍
JupyterLab 是一个基于 web 的交互式开发环境,用于 Jupyter 笔记本、代码和数据。JupyterLab 扩展可以增强 JupyterLab 的功能,提供更多的自定义选项和工具。jupyterlab/extension-examples
项目是一个官方提供的示例集合,旨在帮助开发者理解和创建 JupyterLab 扩展。
项目快速启动
环境准备
在开始之前,请确保你已经安装了以下软件:
- Node.js
- Python
- JupyterLab
克隆项目
首先,克隆 jupyterlab/extension-examples
仓库到本地:
git clone https://github.com/jupyterlab/extension-examples.git
cd extension-examples
安装依赖
进入项目目录后,安装必要的依赖:
pip install -r requirements.txt
jlpm install
构建和运行
使用以下命令构建和运行扩展:
jupyter labextension install .
jupyter lab
应用案例和最佳实践
创建一个简单的扩展
以下是一个简单的 JupyterLab 扩展示例,它会在 JupyterLab 的侧边栏中添加一个新的按钮:
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICommandPalette } from '@jupyterlab/apputils';
/**
* Initialization data for the myextension extension.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: 'myextension',
autoStart: true,
requires: [ICommandPalette],
activate: (app: JupyterFrontEnd, palette: ICommandPalette) => {
console.log('JupyterLab extension myextension is activated!');
const command = 'myextension:open';
app.commands.addCommand(command, {
label: 'Open My Extension',
execute: () => {
window.alert('Hello from My Extension!');
}
});
palette.addItem({ command, category: 'My Extension' });
}
};
export default extension;
最佳实践
- 模块化设计:将功能拆分为多个小模块,便于维护和测试。
- 遵循 JupyterLab 风格指南:确保扩展的外观和行为与 JupyterLab 一致。
- 文档和注释:为代码添加详细的文档和注释,方便其他开发者理解和使用。
典型生态项目
Jupyter Notebook
Jupyter Notebook 是一个基于 web 的交互式计算环境,用于创建和共享文档,包含实时代码、方程、可视化和叙述文本。
JupyterHub
JupyterHub 是一个多用户版本的 Jupyter Notebook,允许管理员创建、管理、并为多个用户提供 Jupyter Notebook 服务器。
Voilà
Voilà 允许你将 Jupyter Notebook 转换为独立的 web 应用程序,非常适合创建交互式报告和仪表板。
通过这些生态项目,JupyterLab 扩展可以更好地融入整个 Jupyter 生态系统,提供更丰富的功能和更好的用户体验。