常见扩展点#
JupyterLab 的大部分组件都设计为可扩展的,它们提供可以通过令牌在扩展中请求的服务。扩展作者可以请求的常见核心令牌列表在核心插件中给出。
在核心令牌列表之后,是使用 JupyterLab 最常用的一些扩展点的指南。然而,它并不是关于如何扩展应用程序组件的详尽说明,更详细的公共 API 描述可以在JupyterLab和Lumino API 文档中找到。
核心插件#
JupyterLab 的核心包提供以下插件。它们可以使用命令jupyter labextension enable <plugin-id>
或jupyter labextension disable <plugin-id>
启用或禁用。
@jupyterlab/application-extension:commands
: 添加与 shell 相关的命令。@jupyterlab/application-extension:context-menu
: 填充上下文菜单。@jupyterlab/application-extension:dirty
: 在关闭带有未保存修改的浏览器标签时添加安全对话框。@jupyterlab/application-extension:faviconbusy
: 根据应用程序状态处理网站图标。@jupyterlab/application-extension:info
: 提供应用程序信息。@jupyterlab/application-extension:layout
: 提供 shell 布局恢复器。@jupyterlab/application-extension:logo
: 设置应用程序标志。@jupyterlab/application-extension:main
: 初始化应用程序并提供 URL 树路径处理程序。@jupyterlab/application-extension:mimedocument
: 提供 mime 文档部件跟踪器。@jupyterlab/application-extension:mode-switch
: 添加界面模式切换。@jupyterlab/application-extension:notfound
: 定义未找到 URL(即路由)的行为。@jupyterlab/application-extension:paths
: 提供应用程序路径。@jupyterlab/application-extension:property-inspector
: 提供属性检查器。@jupyterlab/application-extension:router
: 提供 URL 路由器。@jupyterlab/application-extension:shell
: 提供 JupyterLab shell。与app.shell
相比,它具有扩展的 API。@jupyterlab/application-extension:status
: 提供应用程序状态。@jupyterlab/application-extension:top-bar
: 在顶部区域(主菜单栏旁边)添加一个工具栏。@jupyterlab/application-extension:tree-resolver
: 提供树路由解析器。@jupyterlab/apputils-extension:announcements
: 添加公告功能。它将从互联网获取新闻并检查应用程序更新。@jupyterlab/apputils-extension:kernel-status
: 提供内核状态指示器模型。@jupyterlab/apputils-extension:kernels-settings
: 为内核设置保留名称。@jupyterlab/apputils-extension:licenses-client
: 用于获取许可证的许可证客户端插件。@jupyterlab/apputils-extension:licenses-plugin
: 添加许可证报告工具。@jupyterlab/apputils-extension:notification
: 添加通知中心及其状态指示器。@jupyterlab/apputils-extension:palette
: 提供命令面板。@jupyterlab/apputils-extension:palette-restorer
: 恢复命令面板。@jupyterlab/apputils-extension:print
: 添加打印功能。@jupyterlab/apputils-extension:resolver
: 提供窗口名称解析器。@jupyterlab/apputils-extension:running-sessions-status
: 添加正在运行的会话和终端状态栏项。@jupyterlab/apputils-extension:sanitizer
: 提供 HTML 清理器。@jupyterlab/apputils-extension:sessionDialogs
: 提供会话上下文对话框。@jupyterlab/apputils-extension:settings
: 提供设置注册表。@jupyterlab/apputils-extension:settings-connector
: 提供设置连接器。@jupyterlab/apputils-extension:splash
: 提供启动画面。@jupyterlab/apputils-extension:state
: 提供应用程序状态。它按工作区存储。@jupyterlab/apputils-extension:subshell-settings
: 内核子 shell 设置。@jupyterlab/apputils-extension:themes
: 提供主题管理器。@jupyterlab/apputils-extension:themes-palette-menu
: 将主题命令添加到菜单和命令面板。@jupyterlab/apputils-extension:toggle-header
: 添加一个命令来显示主区域部件内容标题。@jupyterlab/apputils-extension:toolbar-registry
: 提供工具栏项注册表。@jupyterlab/apputils-extension:utilityCommands
: 添加元命令以运行一组其他命令。@jupyterlab/apputils-extension:workspaces
: 添加工作区文件类型。@jupyterlab/audio-extension:plugin
: 添加音频文件查看器。@jupyterlab/cell-toolbar-extension:plugin
: 添加单元格工具栏。@jupyterlab/celltags-extension:plugin
: 添加单元格标签编辑器。@jupyterlab/codemirror-extension:binding
: 注册 CodeMirror 扩展工厂,绑定编辑器和共享模型。@jupyterlab/codemirror-extension:commands
: 注册对选定/活动 CodeMirror 编辑器进行操作的命令。@jupyterlab/codemirror-extension:extensions
: 提供 CodeMirror 扩展工厂注册表。@jupyterlab/codemirror-extension:languages
: 提供 CodeMirror 语言注册表。@jupyterlab/codemirror-extension:line-col-status
: 提供代码编辑器光标位置模型。@jupyterlab/codemirror-extension:services
: 提供实例化 CodeMirror 编辑器的服务。@jupyterlab/codemirror-extension:themes
: 提供 CodeMirror 主题注册表。@jupyterlab/completer-extension:base-service
: 添加上下文和内核补全提供者。@jupyterlab/completer-extension:inline-completer
: 注册内联补全工厂;添加内联补全命令、快捷方式和设置。@jupyterlab/completer-extension:inline-completer-factory
: 提供内联补全工厂。@jupyterlab/completer-extension:inline-history
: 添加内联补全提供者,从执行历史中建议代码。@jupyterlab/completer-extension:manager
: 提供补全提供者管理器。@jupyterlab/console-extension:cell-executor
: 提供控制台单元格执行器。@jupyterlab/console-extension:completer
: 为控制台添加补全功能。@jupyterlab/console-extension:cursor-position
: 将控制台添加到代码编辑器光标位置模型。@jupyterlab/console-extension:factory
: 提供控制台部件内容工厂。@jupyterlab/console-extension:foreign
: 为控制台添加 IOPub 消息的外部处理程序。@jupyterlab/console-extension:kernel-status
: 将控制台添加到内核状态指示器模型。@jupyterlab/console-extension:tracker
: 提供控制台部件跟踪器。@jupyterlab/csvviewer-extension:csv
: 添加 CSV 文件类型查看器。@jupyterlab/csvviewer-extension:tsv
: 添加 TSV 文件类型查看器。@jupyterlab/debugger-extension:config
: 提供调试器配置。@jupyterlab/debugger-extension:consoles
: 为控制台添加调试器功能。@jupyterlab/debugger-extension:files
: 为文件添加调试器功能。@jupyterlab/debugger-extension:main
: 初始化调试器用户界面。@jupyterlab/debugger-extension:notebooks
: 为笔记本添加调试器功能并提供调试器笔记本处理程序。@jupyterlab/debugger-extension:service
: 提供调试器服务。@jupyterlab/debugger-extension:sidebar
: 提供调试器侧边栏。@jupyterlab/debugger-extension:source-viewer
: 初始化调试器源查看器。@jupyterlab/debugger-extension:sources
: 为调试提供源功能。@jupyterlab/debugger-extension:variables
: 在调试器变量面板中添加变量渲染器和检查。@jupyterlab/docmanager-extension:contexts
: 添加已打开文档脏状态的处理。@jupyterlab/docmanager-extension:download
: 添加下载文件命令。@jupyterlab/docmanager-extension:manager
: 提供文档管理器。@jupyterlab/docmanager-extension:open-browser-tab
: 添加打开浏览器标签命令。@jupyterlab/docmanager-extension:opener
: 提供部件开启器。@jupyterlab/docmanager-extension:path-status
: 在状态栏中添加文件路径指示器。@jupyterlab/docmanager-extension:plugin
: 为文档管理器添加命令和设置。@jupyterlab/docmanager-extension:recents
: 提供最近打开和关闭文档的管理器。@jupyterlab/docmanager-extension:saving-status
: 添加保存状态指示器。@jupyterlab/documentsearch-extension:labShellWidgetListener
: 在有效文档上激活搜索。@jupyterlab/documentsearch-extension:plugin
: 提供文档搜索注册表。@jupyterlab/extensionmanager-extension:plugin
: 添加扩展管理器插件。@jupyterlab/filebrowser-extension:browser
: 设置默认文件浏览器命令和状态恢复。@jupyterlab/filebrowser-extension:default-file-browser
: 提供默认文件浏览器。@jupyterlab/filebrowser-extension:download
: 添加下载文件命令。如果用户输入适当的下载 URL,禁用此插件将不会禁用从服务器下载文件。@jupyterlab/filebrowser-extension:factory
: 提供文件浏览器工厂。@jupyterlab/filebrowser-extension:file-upload-status
: 添加文件上传状态部件。@jupyterlab/filebrowser-extension:notify-upload
: 添加上传后自动打开支持文件的功能。@jupyterlab/filebrowser-extension:open-browser-tab
: 添加在新浏览器标签中打开的功能。@jupyterlab/filebrowser-extension:open-url
: 添加“从远程 URL 打开文件”功能。@jupyterlab/filebrowser-extension:open-with
: 添加“打开方式”功能,允许用户选择非首选的文档查看器。@jupyterlab/filebrowser-extension:settings
: 设置默认文件浏览器设置。@jupyterlab/filebrowser-extension:share-file
: 添加“复制可共享链接”命令;例如,对于 JupyterHub 部署很有用。@jupyterlab/filebrowser-extension:widget
: 将文件浏览器添加到应用程序 shell。@jupyterlab/fileeditor-extension:completer
: 为文件编辑器添加补全功能。@jupyterlab/fileeditor-extension:cursor-position
: 添加文件编辑器光标位置状态部件。@jupyterlab/fileeditor-extension:editor-syntax-status
: 添加文件编辑器语法状态部件。@jupyterlab/fileeditor-extension:language-server
: 为文件编辑器添加语言服务器功能。@jupyterlab/fileeditor-extension:plugin
: 提供文件编辑器部件跟踪器。@jupyterlab/fileeditor-extension:search
: 为文件编辑器添加搜索功能。@jupyterlab/fileeditor-extension:tab-space-status
: 添加文件编辑器缩进状态部件。@jupyterlab/fileeditor-extension:widget-factory
: 提供用于创建文件编辑器的工厂。@jupyterlab/help-extension:about
: 添加“关于”对话框功能。@jupyterlab/help-extension:jupyter-forum
: 添加命令以打开 Jupyter 论坛网站。@jupyterlab/help-extension:licenses-commands
: 添加许可证命令以实现向后兼容性。@jupyterlab/help-extension:open
: 添加命令以面板或浏览器标签打开网站。@jupyterlab/help-extension:resources
: 添加到 Jupyter 参考文档网站的菜单项。@jupyterlab/htmlviewer-extension:plugin
: 添加 HTML 文件查看器并提供其跟踪器。@jupyterlab/hub-extension:connectionlost
: 提供服务以在与 hub 服务器的连接丢失时收到通知。@jupyterlab/hub-extension:menu
: 将 hub 相关命令添加到菜单。@jupyterlab/hub-extension:plugin
: 注册与 hub 服务器相关的命令。@jupyterlab/imageviewer-extension:plugin
: 添加图像查看器并提供其跟踪器。@jupyterlab/inspector-extension:consoles
: 为控制台添加代码自省支持。@jupyterlab/inspector-extension:inspector
: 提供代码自省部件。@jupyterlab/inspector-extension:notebooks
: 为笔记本添加代码自省。@jupyterlab/javascript-extension:factory
: 添加 JavaScript 内容渲染器。@jupyterlab/json-extension:factory
: 添加 JSON 内容渲染器。@jupyterlab/json-lines-extension:factory
: 添加 JSONLines 内容渲染器。@jupyterlab/launcher-extension:plugin
: 提供启动器标签服务。@jupyterlab/logconsole-extension:plugin
: 提供日志记录器注册表。@jupyterlab/lsp-extension:code-extractor-manager
: 提供代码提取器管理器。@jupyterlab/lsp-extension:feature
: 提供语言服务器功能管理器。@jupyterlab/lsp-extension:plugin
: 提供语言服务器连接管理器。@jupyterlab/lsp-extension:settings
: 提供语言服务器设置。@jupyterlab/lsp-extension:tracker
: 提供WidgetLSPAdapter
跟踪器。@jupyterlab/mainmenu-extension:plugin
: 添加并提供应用程序主菜单。@jupyterlab/mainmenu-extension:recents
: 将用于打开最近文档的子菜单添加到主菜单的“文件”部分。@jupyterlab/markdownviewer-extension:plugin
: 添加 Markdown 文件查看器并提供其跟踪器。@jupyterlab/markedparser-extension:plugin
: 提供 Markdown 解析器。@jupyterlab/mathjax-extension:plugin
: 提供 LaTeX 数学表达式解释器。@jupyterlab/mermaid-extension:context-commands
: 为美人鱼图提供上下文菜单命令。@jupyterlab/mermaid-extension:core
: 提供美人鱼管理器。@jupyterlab/mermaid-extension:factory
: 为美人鱼基于文本的图表提供渲染器。@jupyterlab/mermaid-extension:markdown
: 提供美人鱼 Markdown 渲染器。@jupyterlab/metadataform-extension:metadataforms
: 提供元数据表单注册表。@jupyterlab/notebook-extension:active-cell-tool
: 在元数据编辑器标签中添加活动单元格字段。@jupyterlab/notebook-extension:cell-executor
: 提供笔记本单元格执行器。@jupyterlab/notebook-extension:cloned-outputs
: 添加克隆输出功能。@jupyterlab/notebook-extension:code-console
: 添加笔记本代码控制台功能。@jupyterlab/notebook-extension:completer
: 为笔记本添加代码补全功能。@jupyterlab/notebook-extension:copy-output
: 添加复制单元格输出功能。@jupyterlab/notebook-extension:cursor-position
: 添加笔记本光标位置状态。@jupyterlab/notebook-extension:execution-indicator
: 添加笔记本执行状态部件。@jupyterlab/notebook-extension:export
: 添加导出笔记本命令。@jupyterlab/notebook-extension:factory
: 提供笔记本单元格工厂。@jupyterlab/notebook-extension:kernel-status
: 添加笔记本内核状态。@jupyterlab/notebook-extension:language-server
: 为笔记本添加语言服务器功能。@jupyterlab/notebook-extension:log-output
: 将单元格输出日志添加到应用程序日志记录器。@jupyterlab/notebook-extension:metadata-editor
: 添加用于完整元数据编辑器的元数据表单。@jupyterlab/notebook-extension:mode-status
: 添加笔记本模式状态部件。@jupyterlab/notebook-extension:search
: 为笔记本添加搜索功能。@jupyterlab/notebook-extension:toc
: 为笔记本添加目录功能。@jupyterlab/notebook-extension:tools
: 提供笔记本工具。@jupyterlab/notebook-extension:tracker
: 提供笔记本部件跟踪器。@jupyterlab/notebook-extension:trust-status
: 添加笔记本信任状态部件。@jupyterlab/notebook-extension:update-raw-mimetype
: 添加用于原始单元格 mimetype 的元数据表单编辑器。@jupyterlab/notebook-extension:widget-factory
: 提供笔记本部件工厂。@jupyterlab/pdf-extension:factory
: 添加 PDF 内容渲染器。@jupyterlab/pluginmanager-extension:plugin
: 启用或禁用单个插件。@jupyterlab/rendermime-extension:plugin
: 提供渲染 mime 注册表。@jupyterlab/running-extension:plugin
: 提供正在运行的会话管理器。@jupyterlab/running-extension:recently-closed
: 添加最近关闭的文档列表。@jupyterlab/running-extension:search-tabs
: 添加一个用于搜索已打开和已关闭标签的部件。@jupyterlab/running-extension:sidebar
: 提供正在运行的会话侧边栏。@jupyterlab/services-extension:config-section-manager
: 提供配置部分管理器。@jupyterlab/services-extension:connection-status
: 提供默认连接状态。@jupyterlab/services-extension:contents-manager
: 默认内容管理器插件。@jupyterlab/services-extension:default-drive
: 内容管理器的默认驱动器。@jupyterlab/services-extension:event-manager
: 事件管理器插件。@jupyterlab/services-extension:kernel-manager
: 内核管理器插件。@jupyterlab/services-extension:kernel-spec-manager
: 内核规范管理器插件。@jupyterlab/services-extension:nbconvert-manager
: nbconvert 管理器插件。@jupyterlab/services-extension:server-settings
: 默认服务器设置插件。@jupyterlab/services-extension:service-manager
: 默认服务管理器插件。@jupyterlab/services-extension:session-manager
: 会话管理器插件。@jupyterlab/services-extension:setting-manager
: 设置管理器插件。@jupyterlab/services-extension:terminal-manager
: 终端管理器插件。@jupyterlab/services-extension:user-manager
: 用户管理器插件。@jupyterlab/services-extension:workspace-manager
: 工作区管理器插件。@jupyterlab/settingeditor-extension:form-ui
: 添加交互式设置编辑器并提供其跟踪器。@jupyterlab/settingeditor-extension:plugin
: 添加 JSON 设置编辑器并提供其跟踪器。@jupyterlab/shortcuts-extension:shortcuts
: 添加键盘快捷键编辑器。@jupyterlab/statusbar-extension:plugin
: 提供应用程序状态栏。@jupyterlab/terminal-extension:plugin
: 添加终端并提供其跟踪器。@jupyterlab/theme-dark-extension:plugin
: 添加深色主题。@jupyterlab/theme-dark-high-contrast-extension:plugin
: 添加深色高对比度主题。@jupyterlab/theme-light-extension:plugin
: 添加浅色主题。@jupyterlab/toc-extension:registry
: 提供目录注册表。@jupyterlab/toc-extension:tracker
: 添加目录部件并提供其跟踪器。@jupyterlab/tooltip-extension:consoles
: 为控制台添加工具提示功能。@jupyterlab/tooltip-extension:files
: 为文件编辑器添加工具提示功能。@jupyterlab/tooltip-extension:manager
: 提供工具提示管理器。@jupyterlab/tooltip-extension:notebooks
: 为笔记本添加工具提示功能。@jupyterlab/translation-extension:plugin
: 添加翻译命令和设置。@jupyterlab/translation-extension:translator
: 提供应用程序翻译对象。@jupyterlab/translation-extension:translator-connector
: 提供应用程序翻译连接器。@jupyterlab/ui-components-extension:form-renderer-registry
: 提供设置表单渲染器注册表。@jupyterlab/ui-components-extension:labicon-manager
: 提供图标管理器。@jupyterlab/vega5-extension:factory
: 为 Vega 5 和 Vega-Lite 3 到 5 内容提供渲染器。@jupyterlab/video-extension:plugin
: 添加视频文件查看器。@jupyterlab/workspaces-extension:commands
: 添加工作区命令。@jupyterlab/workspaces-extension:indicator
: 在顶栏添加工作区指示器元素。@jupyterlab/workspaces-extension:menu
: 用工作区子菜单填充“文件”主菜单。@jupyterlab/workspaces-extension:model
: 提供可用工作区的模型。@jupyterlab/workspaces-extension:sidebar
: 用工作区填充运行侧边栏。
核心令牌#
JupyterLab 的核心包为插件提供许多服务。这些服务的令牌在此处列出,并附有简要说明,说明您可能希望在扩展中使用这些服务的情况。
@jupyterlab/application:IConnectionLost
: 一项用于调用当 JupyterLab 与服务器连接丢失时显示的对话框的服务。如果您出于某种原因想要在新的情况下调出“连接丢失”对话框,请使用此服务。@jupyterlab/application:IConnectionStatus
: 一项提供应用程序连接状态的服务。@jupyterlab/application:IInfo
: 一项提供当前应用程序元数据的服务,包括禁用的扩展和是否启用了开发模式。@jupyterlab/application:ILabShell
: 一项用于与 JupyterLab shell 交互的服务。顶层application
对象也引用 shell,但其接口受到限制,以便与应用程序上的不同 shell 实现无关。使用此服务可获取有关当前活动部件和布局状态的更详细信息。@jupyterlab/application:ILabStatus
: 一项用于与应用程序忙/脏状态交互的服务。如果您想设置应用程序的“忙碌”网站图标,或设置应用程序的“脏”状态(在离开应用程序页面之前要求用户确认),请使用此服务。@jupyterlab/application:ILayoutRestorer
: 一项提供应用程序布局恢复功能的服务。使用此服务可在页面加载后恢复您的活动。@jupyterlab/application:IMimeDocumentTracker
: 一个用于使用 mime 渲染器扩展渲染的文档的部件跟踪器。如果您想列出此类扩展渲染的文档并与之交互,请使用此服务。@jupyterlab/application:IPaths
: 一项提供有关当前应用程序各种 URL 和服务器路径信息的服务。如果您想组装 URL 以使用 JupyterLab REST API,请使用此服务。@jupyterlab/application:IRouter
: 应用程序使用的 URL 路由器。使用此服务可为您的扩展添加自定义 URL 路由(例如,如果用户导航到子路径,则调用命令)。@jupyterlab/application:ITreePathUpdater
: 一项用于更新树路径的服务。@jupyterlab/application:ITreeResolver
: 一项用于解析树路径的服务。@jupyterlab/apputils:ICommandPalette
: 左侧面板中应用程序命令面板的服务。使用此服务可将命令添加到面板。@jupyterlab/apputils:IKernelStatusModel
: 一项用于将内核会话提供者注册到内核状态指示器的服务。@jupyterlab/apputils:ILicensesClient
: 一项用于获取许可证的服务。@jupyterlab/apputils:ISanitizer
: 一项用于清理 HTML 字符串的服务。@jupyterlab/apputils:ISessionContextDialogs
: 一项用于处理会话对话框的服务。@jupyterlab/apputils:ISplashScreen
: 应用程序启动画面的服务。如果您想出于自己的目的显示启动画面,请使用此服务。@jupyterlab/apputils:IThemeManager
: 应用程序主题管理器的服务。这主要用于主题扩展以注册新主题。@jupyterlab/apputils:IToolbarWidgetRegistry
: 一个工具栏部件注册表。如果您想根据数据定义(例如存储在设置中)动态构建工具栏,则需要此服务。@jupyterlab/apputils:IWindowResolver
: 应用程序窗口解析器的服务。JupyterLab 工作区被赋予一个名称,该名称使用窗口解析器确定。如果您想使用当前工作区的名称,请使用此服务。@jupyterlab/codeeditor:IEditorServices
: 应用程序文本编辑器提供者的服务。使用此服务可创建新的文本编辑器并将其托管在您的 UI 元素中。@jupyterlab/codeeditor:IPositionModel
: 一项用于处理代码编辑器光标位置的服务。@jupyterlab/codemirror:IEditorExtensionRegistry
: CodeMirror 扩展工厂的注册表。@jupyterlab/codemirror:IEditorLanguageRegistry
: CodeMirror 语言的注册表。@jupyterlab/codemirror:IEditorThemeRegistry
: CodeMirror 主题的注册表。@jupyterlab/completer:ICompletionProviderManager
: 一项用于补全提供者管理的服务。@jupyterlab/completer:IInlineCompleterFactory
: 内联补全部件的工厂。@jupyterlab/console:IConsoleCellExecutor
: 控制台单元格执行器。@jupyterlab/console:IConsoleTracker
: 一个用于代码控制台的部件跟踪器。如果您想迭代并与应用程序创建的代码控制台交互,请使用此服务。@jupyterlab/console:IContentFactory
: 一个创建新代码控制台的工厂对象。如果您想创建代码控制台并将其托管在自己的 UI 元素中,请使用此服务。@jupyterlab/coreutils:ISettingConnector
: 一项用于连接到设置端点的服务。@jupyterlab/coreutils:ISettingRegistry
: JupyterLab 设置系统的服务。如果您想为您的应用程序存储设置,请使用此服务。有关更多信息,请参阅“schemaDir”。@jupyterlab/coreutils:IStateDB
: JupyterLab 状态数据库的服务。如果您想存储在页面加载后仍然存在的数据,请使用此服务。有关更多信息,请参阅“状态数据库”。@jupyterlab/debugger:IDebugger
: 调试器用户界面。@jupyterlab/debugger:IDebuggerConfig
: 一项用于处理调试器配置的服务。@jupyterlab/debugger:IDebuggerHandler
: 一项用于处理笔记本调试器的服务。@jupyterlab/debugger:IDebuggerSidebar
: 调试器侧边栏的服务。@jupyterlab/debugger:IDebuggerSourceViewer
: 调试器源查看器。@jupyterlab/debugger:IDebuggerSources
: 一项用于在调试模式下显示源的服务。@jupyterlab/docmanager:IDocumentManager
: 一项用于应用程序使用的所有文档的管理器服务。如果您想打开和关闭文档、创建和删除文件以及以其他方式与文件系统交互,请使用此服务。@jupyterlab/docmanager:IDocumentWidgetOpener
: 一项用于打开部件的服务。@jupyterlab/docmanager:IRecentsManager
: 一项提供有关最近打开和关闭文档信息的服务。@jupyterlab/documentsearch:ISearchProviderRegistry
: 应用程序搜索提供者注册表的服务。插件可以将它们的 UI 元素注册到此注册表以提供查找/替换支持。@jupyterlab/filebrowser:IDefaultFileBrowser
: 默认文件浏览器的服务。@jupyterlab/filebrowser:IFileBrowserCommands
: 一个令牌,用于确保文件浏览器命令已加载。@jupyterlab/filebrowser:IFileBrowserFactory
: 一个创建文件浏览器的工厂对象。如果您想创建自己的文件浏览器(例如,用于自定义存储后端),或与已由扩展创建的其他文件浏览器交互,请使用此服务。@jupyterlab/fileeditor:IEditorTracker
: 一个用于文件编辑器的部件跟踪器。如果您想迭代并与应用程序创建的文件编辑器交互,请使用此服务。@jupyterlab/fileeditor:IEditorWidgetFactory
: 用于创建文件编辑器的工厂。@jupyterlab/htmlviewer:IHTMLViewerTracker
: 一个用于渲染 HTML 文档的部件跟踪器。如果您想迭代并与应用程序查看的 HTML 文档交互,请使用此服务。@jupyterlab/imageviewer:IImageTracker
: 一个用于图像的部件跟踪器。如果您想迭代并与应用程序查看的图像交互,请使用此服务。@jupyterlab/inspector:IInspector
: 一项用于向部件添加上下文帮助的服务(通过“帮助”菜单中的“显示上下文帮助”可见)。使用此服务可将您的扩展挂接到上下文帮助系统。@jupyterlab/launcher:ILauncher
: 应用程序活动启动器的服务。使用此服务可将您的扩展活动添加到启动器面板。@jupyterlab/logconsole:ILoggerRegistry
: 一项提供日志记录基础设施的服务。@jupyterlab/lsp:ILSPCodeExtractorsManager
: 提供代码提取器管理器。在您的扩展中需要此令牌才能注册代码提取器,从而允许从已打开的文档创建多个虚拟文档。@jupyterlab/lsp:ILSPDocumentConnectionManager
: 提供虚拟文档和语言服务器连接服务。@jupyterlab/lsp:ILSPFeatureManager
: 提供语言服务器功能管理器。注册新客户端功能需要此令牌。@jupyterlab/lsp:IWidgetLSPAdapterTracker
: 提供 WidgetLSPAdapter 跟踪器。在您的扩展中需要此令牌才能跟踪 WidgetLSPAdapter。@jupyterlab/mainmenu:IMainMenu
: 应用程序主菜单栏的服务。如果您想添加自己的菜单项或为特定活动提供标准化菜单项的实现,请使用此服务。@jupyterlab/markdownviewer:IMarkdownViewerTracker
: 一个用于 Markdown 文档查看器的部件跟踪器。如果您想迭代并与渲染的 Markdown 文档交互,请使用此服务。@jupyterlab/mermaid:IMermaidManager
: 用于渲染美人鱼文本图表的管理器。@jupyterlab/mermaid:IMermaidMarkdown
: 用于在 Markdown 围栏代码块中渲染美人鱼文本图表的管理器。@jupyterlab/metadataform:IMetadataFormProvider
: 一项用于注册新元数据编辑器部件的服务。@jupyterlab/notebook:IContentFactory
: 一个创建新笔记本的工厂对象。如果您想创建笔记本并将其托管在自己的 UI 元素中,请使用此服务。@jupyterlab/notebook:INotebookCellExecutor
: 笔记本单元格执行器。@jupyterlab/notebook:INotebookTools
: 右侧边栏中“笔记本工具”面板的服务。使用此服务可将您自己的功能添加到面板。@jupyterlab/notebook:INotebookTracker
: 一个用于笔记本的部件跟踪器。如果您想迭代并与应用程序创建的笔记本交互,请使用此服务。@jupyterlab/notebook:INotebookWidgetFactory
: 一项用于创建笔记本查看器的服务。@jupyterlab/pluginmanager:IPluginManager
: 插件管理器存在的指示器,带有一个打开插件管理器部件的方法。@jupyterlab/property-inspector:IPropertyInspectorProvider
: 一项用于在属性检查器侧面板中注册新部件的服务。@jupyterlab/rendermime:ILatexTypesetter
: 应用程序 LaTeX 排版器的服务。如果您想在扩展中排版数学,请使用此服务。@jupyterlab/rendermime:IMarkdownParser
: 一项用于将 Markdown 语法渲染为 HTML 内容的服务。@jupyterlab/rendermime:IRenderMimeRegistry
: 应用程序 rendermime 注册表的服务。使用此服务可在您的扩展中为各种 mime 类型创建渲染器。很多时候,创建“mime 渲染器扩展”比直接使用此服务更容易。@jupyterlab/rendermime:IUrlResolverFactory
: 资产 URL 解析器的工厂。@jupyterlab/running:IRunningSessionManagers
: 一项用于添加正在运行的会话管理器的服务。@jupyterlab/running:IRunningSessionsSidebar
: 一个允许修改正在运行的会话侧边栏的令牌。@jupyterlab/services:IConfigSectionManager
: 一项提供配置部分管理器的服务。@jupyterlab/services:IContentsManager
: 内容管理器令牌。@jupyterlab/services:IDefaultDrive
: 内容管理器的默认驱动器。@jupyterlab/services:IEventManager
: 事件管理器令牌。@jupyterlab/services:IKernelManager
: 内核管理器令牌。@jupyterlab/services:IKernelSpecManager
: 内核规范管理器令牌。@jupyterlab/services:INbConvertManager
: nbconvert 管理器令牌。@jupyterlab/services:IServerSettings
: 应用程序的服务器设置。@jupyterlab/services:IServiceManager
: 应用程序的服务管理器。@jupyterlab/services:ISessionManager
: 会话管理器令牌。@jupyterlab/services:ISettingManager
: 设置管理器令牌。@jupyterlab/services:ITerminalManager
: 终端管理器令牌。@jupyterlab/services:IUserManager
: 用户管理器令牌。@jupyterlab/services:IWorkspaceManager
: 工作区管理器令牌。@jupyterlab/settingeditor:IJSONSettingEditorTracker
: JSON 设置编辑器的部件跟踪器。如果您想迭代并与应用程序创建的设置编辑器交互,请使用此服务。@jupyterlab/settingeditor:ISettingEditorTracker
: 交互式设置编辑器的部件跟踪器。如果您想迭代并与应用程序创建的设置编辑器交互,请使用此服务。@jupyterlab/statusbar:IStatusBar
: 应用程序状态栏的服务。如果您想添加新的状态栏项,请使用此服务。@jupyterlab/terminal:ITerminalTracker
: 终端的部件跟踪器。如果您想迭代并与应用程序创建的终端交互,请使用此服务。@jupyterlab/toc:ITableOfContentsRegistry
: 一项用于注册目录工厂的服务。@jupyterlab/toc:ITableOfContentsTracker
: 目录的部件跟踪器。@jupyterlab/tooltip:ITooltipManager
: 应用程序工具提示管理器的服务。使用此服务可允许您的扩展调用工具提示。@jupyterlab/translation:ITranslator
: 一项用于翻译字符串的服务。@jupyterlab/translation:ITranslatorConnector
: 一项用于连接到服务器翻译端点的服务。@jupyterlab/ui-components:IFormRendererRegistry
: 一项用于设置表单渲染器注册的服务。@jupyterlab/ui-components:ILabIconManager
: 一项用于注册和请求图标的服务。@jupyterlab/workspaces:IWorkspaceCommands
: 提供工作区命令的标识符。@jupyterlab/workspaces:IWorkspacesModel
: 提供可用工作区的模型。
命令#
向命令注册表添加命令#
也许向 JupyterLab 添加功能的最常见方式是通过命令。这些是轻量级对象,包括要执行的函数以及其他元数据,包括它们的标签和何时启用。应用程序有一个由字符串命令 ID 键控的单一命令注册表,您可以将自定义命令添加到其中。
添加到命令注册表的命令随后可以用于填充 JupyterLab 的几个用户界面元素,包括菜单和启动器。
这是一个向应用程序(由app
给出)添加命令的示例代码块。
const commandID = 'my-command';
let toggled = false;
app.commands.addCommand(commandID, {
label: 'My Cool Command',
isEnabled: () => true,
isVisible: () => true,
isToggled: () => toggled,
iconClass: 'some-css-icon-class',
describedBy: {
args: {
type: 'object',
properties: {
text: {
type: 'string',
description: 'Optional text to log',
default: ''
},
count: {
type: 'number',
description: 'Optional number of times to log the text',
default: 1
}
}
}
},
execute: (args) => {
const text = args?.text || '';
const count = args?.count || 1;
for (let i = 0; i < count; i++) {
console.log(`Executed ${commandID} with text: ${text}`);
}
}
});
此示例添加了一个新命令,当触发时,它会调用execute
函数。isEnabled
指示命令是否启用,并决定其呈现是否变灰。isToggled
指示是否在命令旁边渲染一个复选标记。isVisible
指示是否完全渲染该命令。iconClass
指定了一个 CSS 类,可用于在命令呈现旁边显示图标。describedBy
是一个可选但建议的属性,它提供了一个 JSON 模式,描述了命令的参数,这对于文档、工具和确保命令调用的_一致性_很有用。
isEnabled
、isToggled
和isVisible
都可以是布尔值或返回布尔值的函数,以防您想进行一些逻辑来确定这些条件。
同样,label
和iconClass
都可以是字符串值或返回字符串值的函数。
添加新命令时,可以向命令注册表传递更多选项。这些选项在此处有详细文档。
命令添加到应用程序命令注册表后,您可以将其添加到应用程序用户界面的各个位置,它们将使用您提供的元数据进行渲染。
例如,您可以使用CommandToolbarButtonComponent
向笔记本工具栏添加一个按钮以运行该命令。
将命令添加到命令面板#
为了将现有注册命令添加到命令面板,您需要在扩展中请求ICommandPalette
令牌。以下是显示如何将命令添加到命令面板(由palette
给出)的示例。
palette.addItem({
command: commandID,
category: 'my-category',
args: {}
});
命令 ID 是您注册命令时使用的相同 ID。您还必须提供一个category
,它决定了命令面板中渲染命令的子标题。它可以是一个预先存在的类别(例如'notebook'
),也可以是您自己选择的新类别。
args
是一个 JSON 对象,将在渲染/执行时传递给命令的函数。您可以使用这些来自定义命令的行为,具体取决于其调用方式。例如,您可以传入args: { isPalette: true }
。您的命令label
函数可以检查提供的args
中的isPalette
,并在这种情况下返回不同的标签。这对于使单个命令足够灵活以在多个上下文中使用非常有用。
图标#
键盘快捷键#
在 JupyterLab 中添加键盘快捷键有两种方式。如果您不希望快捷键可由用户配置,您可以将它们直接添加到应用程序命令注册表。
app.commands.addKeyBinding({
command: commandID,
args: {},
keys: ['Accel T'],
selector: '.jp-Notebook'
});
在此示例中,my-command
命令映射到Accel T
,其中Accel
在 Mac 上对应Cmd
,在 Windows 和 Linux 计算机上对应Ctrl
。
键盘快捷键的行为与上下文菜单非常相似:快捷键处理程序从焦点元素向上遍历 DOM 树,并针对注册的选择器测试每个元素。如果找到匹配项,则使用提供的args
执行该命令。addKeyBinding
选项的完整文档可在此处找到。
JupyterLab 还提供与其设置系统集成以实现键盘快捷键。您的扩展可以提供一个带有jupyter.lab.shortcuts
键的设置模式,声明命令的默认键盘快捷键。
{
"jupyter.lab.shortcuts": [
{
"command": "my-command",
"keys": ["Accel T"],
"selector": ".jp-mod-searchable"
}
]
}
添加到设置系统的快捷键将可由用户编辑。
从 Jupyterlab 3.1 版开始,可以用一个快捷键执行多个命令。这要求您为apputils:run-all-enabled
命令定义一个键盘快捷键。
{
"command": "apputils:run-all-enabled",
"keys": ["Accel T"],
"args": {
"commands": [
"my-command-1",
"my-command-2"
],
"args": [
{},
{}
]
},
"selector": "body"
}
在此示例中,my-command-1
和my-command-2
作为commands
列表传递到apputils:run-all-enabled
命令的args
中。您可以选择将my-command-1
和my-command-2
的命令参数作为args
列表传递到apputils:run-all-enabled
命令的args
中。
启动器#
与菜单、键盘快捷键和命令面板一样,可以通过命令将新项目添加到应用程序启动器。您可以通过在扩展中请求ILauncher
令牌来完成此操作。
launcher.add({
command: commandID,
category: 'Other',
rank: 0
});
除了提供命令 ID 之外,您还需要提供一个类别来放置您的项目(例如“Notebook”或“Other”),以及一个排名来确定其在其他项目中的位置。
Jupyter 前端 Shell#
Jupyter 前端shell用于在应用程序中添加和与内容交互。IShell
接口提供一个add()
方法用于向应用程序添加部件。在 JupyterLab 中,应用程序 shell 由以下部分组成:
一个
top
区域,用于放置顶层工具栏和信息等内容。一个
menu
区域,用于放置顶层菜单。在多文档模式下,该区域会折叠到top
区域中;在单文档模式下,则位于其下方。left
和right
侧边栏区域,用于放置可折叠内容。一个
main
工作区域,用于用户活动。一个
down
区域,用于放置信息内容,如日志控制台、上下文帮助。一个
bottom
区域,用于放置状态栏等内容。一个
header
区域,用于自定义元素。
顶部区域#
顶部区域旨在承载用户整个会话期间的大部分持久用户界面元素。主菜单栏右侧有一个名为 **TopBar** 的工具栏。例如,JupyterLab 在 协作
模式下启动时,会将用户下拉菜单添加到该工具栏。
请参阅 通用工具栏,了解如何将工具栏或自定义小部件添加到工具栏。
您可以使用数字排序来控制顶部栏项的顺序;请参阅 工具栏定义。
JupyterLab 默认在排序 50
处为顶部栏添加一个间隔小部件。您可以按照以下指导放置您的项
排序 <= 50
:将项放置在顶部栏的左侧排序 > 50
:将项放置在顶部栏的右侧
左/右区域#
JupyterLab 的左右侧边栏区域旨在承载比主区域更持久的用户界面元素。话虽如此,扩展作者可以自由地将任何他们喜欢的组件添加到这些区域。您添加的对象的最外层预计是一个 Lumino Widget
,但它可以承载任何您喜欢的内容(例如 React 组件)。
举个例子,以下代码将应用程序命令执行到终端小部件,然后将终端添加到右侧区域
app.commands
.execute('terminal:create-new')
.then((terminal: WidgetModuleType.Terminal) => {
app.shell.add(terminal, 'right');
});
您可以使用数字排序来控制左右标签的顺序
app.shell.add(terminal, 'left', {rank: 600});
建议的排序范围是
0-500:保留给第一方 JupyterLab 扩展。
501-899:保留给第三方扩展。
900:如果未指定,则为默认排序。
1000:JupyterLab 扩展管理器。
状态栏#
JupyterLab 的状态栏旨在显示少量上下文信息。与左右区域一样,它只期望一个 Lumino Widget
,其中可能包含任何类型的内容。由于状态栏空间有限,您应该努力只向其添加小型小部件。
以下示例展示了如何放置一个状态项,该状态项显示应用程序的当前“忙碌”状态。此信息可从 ILabStatus
令牌获取,我们通过名为 labStatus
的变量引用它。我们将 statusWidget
放置在状态栏的中间。当 labStatus
忙碌状态发生变化时,我们会更新 statusWidget
的文本内容以反映这一点。
const statusWidget = new Widget();
labStatus.busySignal.connect(() => {
statusWidget.node.textContent = labStatus.isBusy ? 'Busy' : 'Idle';
});
statusBar.registerStatusItem('lab-status', {
align: 'middle',
item: statusWidget
});
工具栏注册表#
JupyterLab 提供了一个基础设施,用于从设置中定义和自定义工具栏小部件,这类似于定义上下文菜单和主菜单栏。
文档小部件#
一个典型的例子是笔记本工具栏,如下面的代码片段所示
function activatePlugin(
app: JupyterFrontEnd,
// ...
toolbarRegistry: IToolbarWidgetRegistry | null,
settingRegistry: ISettingRegistry | null
): NotebookWidgetFactory.IFactory {
const { commands } = app;
let toolbarFactory:
| ((widget: NotebookPanel) => DocumentRegistry.IToolbarItem[])
| undefined;
// Register notebook toolbar specific widgets
if (toolbarRegistry) {
toolbarRegistry.addFactory<NotebookPanel>(FACTORY, 'cellType', panel =>
ToolbarItems.createCellTypeItem(panel, translator)
);
toolbarRegistry.addFactory<NotebookPanel>(
FACTORY,
'kernelStatus',
panel => Toolbar.createKernelStatusItem(panel.sessionContext, translator)
);
// etc...
if (settingRegistry) {
// Create the factory
toolbarFactory = createToolbarFactory(
toolbarRegistry,
settingRegistry,
// Factory name
FACTORY,
// Setting id in which the toolbar items are defined
'@jupyterlab/notebook-extension:panel',
translator
);
}
}
const factory = new NotebookWidgetFactory({
name: FACTORY,
fileTypes: ['notebook'],
modelName: 'notebook',
defaultFor: ['notebook'],
// ...
toolbarFactory,
translator: translator
});
app.docRegistry.addWidgetFactory(factory);
注册表 addFactory
方法允许扩展为唯一的(工厂名称、工具栏项名称)对提供特殊小部件。然后可以使用辅助函数 createToolbarFactory
从设置中提取工具栏定义,并构建要传递给小部件工厂的工厂。
默认工具栏项可以通过在 "jupyter.lab.toolbars"
映射中提供一个条目来定义,并跨多个扩展。例如,对于笔记本面板
"jupyter.lab.toolbars": {
"Notebook": [ // Factory name
// Item with non-default widget - it must be registered within an extension
{
"name": "save", // Unique toolbar item name
"rank": 10 // Item rank
},
// Item with default button widget triggering a command
{ "name": "insert", "command": "notebook:insert-cell-below", "rank": 20 },
{ "name": "cut", "command": "notebook:cut-cell", "rank": 21 },
{ "name": "copy", "command": "notebook:copy-cell", "rank": 22 },
{ "name": "paste", "command": "notebook:paste-cell-below", "rank": 23 },
{ "name": "run", "command": "runmenu:run", "rank": 30 },
{ "name": "interrupt", "command": "kernelmenu:interrupt", "rank": 31 },
{ "name": "restart", "command": "kernelmenu:restart", "rank": 32 },
{
"name": "restart-and-run",
"command": "notebook:restart-run-all",
"rank": 33 // The default rank is 50
},
{ "name": "cellType", "rank": 40 },
// Horizontal spacer widget
{ "name": "spacer", "type": "spacer", "rank": 100 },
{ "name": "kernelName", "rank": 1000 },
{ "name": "kernelStatus", "rank": 1001 }
]
},
"jupyter.lab.transform": true,
"properties": {
"toolbar": {
"title": "Notebook panel toolbar items",
"items": {
"$ref": "#/definitions/toolbarItem"
},
"type": "array",
"default": []
}
}
设置注册表将透明地合并这些设置 schema 中的定义和任何用户提供的覆盖(自定义),并将它们保存在最终设置对象的 toolbar
属性下。toolbar
列表将用于创建工具栏。源设置 schema 和最终设置对象都由传递给 createToolbarFactory
的插件 ID 标识。用户可以通过添加新项或覆盖现有项(例如提供不同的排序或添加 "disabled": true
以移除项)来定制工具栏。
注意
您需要在收集所有项的插件 ID 中将 jupyter.lab.transform
设置为 true
。
什么是转换?jupyter.lab.transform
标志告诉 JupyterLab 在加载插件之前等待转换函数。这允许动态修改设置 schema,通常用于合并来自多个扩展的工具栏/菜单定义。
加载顺序陷阱:提供转换的扩展必须在激活的早期注册它们,在依赖插件加载之前,否则那些插件将因等待转换而超时。
当前支持工具栏自定义的小部件工厂有
Notebook
:笔记本面板工具栏Cell
:单元格工具栏Editor
:文本编辑器工具栏HTML Viewer
:HTML 查看器工具栏CSVTable
:CSV(逗号分隔值)查看器工具栏TSVTable
:TSV(制表符分隔值)查看器工具栏
工具栏项必须遵循此定义
{
"toolbarItem": {
"properties": {
"name": {
"title": "Unique name",
"type": "string"
},
"args": {
"title": "Command arguments",
"type": "object"
},
"command": {
"title": "Command id",
"type": "string",
"default": ""
},
"disabled": {
"title": "Whether the item is ignored or not",
"type": "boolean",
"default": false
},
"icon": {
"title": "Item icon id",
"description": "If defined, it will override the command icon",
"type": "string"
},
"label": {
"title": "Item label",
"description": "If defined, it will override the command label",
"type": "string"
},
"caption": {
"title": "Item caption",
"description": "If defined, it will override the command caption",
"type": "string"
},
"type": {
"title": "Item type",
"type": "string",
"enum": [
"command",
"spacer"
]
},
"rank": {
"title": "Item rank",
"type": "number",
"minimum": 0,
"default": 50
}
},
"required": [
"name"
],
"additionalProperties": false,
"type": "object"
}
}
带工具栏的通用小部件#
上一节中详细介绍的逻辑可用于自定义带有工具栏的任何小部件。
jupyter.lab.toolbars
设置属性中使用的其他键是
Cell
:单元格工具栏FileBrowser
:默认文件浏览器面板工具栏项TopBar
:顶部区域工具栏(主菜单栏右侧)
以下是启用小部件工具栏的示例
function activatePlugin(
app: JupyterFrontEnd,
// ...
toolbarRegistry: IToolbarWidgetRegistry,
settingRegistry: ISettingRegistry
): void {
const browser = new FileBrowser();
// Toolbar
// - Define a custom toolbar item
toolbarRegistry.addFactory(
'FileBrowser', // Factory name
'uploader',
(browser: FileBrowser) =>
new Uploader({ model: browser.model, translator })
);
// - Link the widget toolbar and its definition from the settings
setToolbar(
browser, // This widget is the one passed to the toolbar item factory
createToolbarFactory(
toolbarRegistry,
settings,
'FileBrowser', // Factory name
plugin.id,
translator
),
// You can explicitly pass the toolbar widget if it is not accessible as `toolbar` attribute
// toolbar,
);
请参阅 工具栏定义 示例,了解如何在设置中定义工具栏项。
小部件跟踪器#
扩展通常会希望与其他扩展创建的文档和活动进行交互。例如,扩展可能希望将一些文本注入到笔记本单元格中,或者设置自定义键映射,或者关闭特定类型的所有文档。此类操作通常由小部件跟踪器完成。扩展在 WidgetTrackers
中跟踪其活动的实例,然后将这些实例作为令牌提供,以便其他扩展可以请求它们。
例如,如果您想与笔记本进行交互,您应该请求 INotebookTracker
令牌。然后,您可以使用此跟踪器遍历、过滤和搜索所有打开的笔记本。您还可以使用它在笔记本被添加到或从跟踪器中移除时通过信号接收通知。
JupyterLab 中的许多活动都提供了小部件跟踪器令牌,包括笔记本、控制台、文本文件、MIME 文档和终端。如果您要将自己的活动添加到 JupyterLab,您可能会考虑提供自己的 WidgetTracker
令牌,以便其他扩展可以使用它。
补全提供者#
代码补全器和内联补全器都可以通过在由 ICompletionProviderManager
令牌提供的补全管理器上注册(内联)补全提供者来扩展。
代码补全器#
最小的代码补全提供者需要实现 fetch 和 isApplicable 方法,并定义唯一的 identifier 属性,但 ICompletionProvider
接口允许更广泛地自定义补全器。
import {
CompletionHandler,
ICompletionProviderManager,
ICompletionContext,
ICompletionProvider
} from '@jupyterlab/completer';
class MyProvider implements ICompletionProvider {
readonly identifier = 'my-provider';
async isApplicable(context: ICompletionContext) {
return true;
}
async fetch(
request: CompletionHandler.IRequest,
context: ICompletionContext
) {
return {
start: request.offset,
end: request.offset,
items: [
{ label: 'option 1' },
{ label: 'option 2' }
]
};
}
}
const plugin: JupyterFrontEndPlugin<void> = {
id: 'my-completer-extension:provider',
autoStart: true,
requires: [ICompletionProviderManager],
activate: (app: JupyterFrontEnd, manager: ICompletionProviderManager): void => {
const provider = new MyProvider();
manager.registerProvider(provider);
}
};
在 extension-examples 存储库中提供了更详细的示例。
有关广泛自定义的补全提供者的示例,请参阅 jupyterlab-lsp 扩展。
内联补全器#
最小的内联补全提供者扩展将只实现必需的方法 fetch 并定义 identifier 和 name 属性,但可以使用许多附加字段来增强功能,例如流式传输,请参阅 IInlineCompletionProvider
文档。
import {
CompletionHandler,
ICompletionProviderManager,
IInlineCompletionContext,
IInlineCompletionProvider
} from '@jupyterlab/completer';
class MyInlineProvider implements IInlineCompletionProvider {
readonly identifier = 'my-provider';
readonly name = 'My provider';
async fetch(
request: CompletionHandler.IRequest,
context: IInlineCompletionContext
) {
return {
items: [
{ insertText: 'suggestion 1' },
{ insertText: 'suggestion 2' }
]
};
}
}
const plugin: JupyterFrontEndPlugin<void> = {
id: 'my-completer-extension:inline-provider',
autoStart: true,
requires: [ICompletionProviderManager],
activate: (app: JupyterFrontEnd, manager: ICompletionProviderManager): void => {
const provider = new MyInlineProvider();
manager.registerInlineProvider(provider);
}
};
有关支持流式传输的内联补全提供者的示例,请参阅 jupyterlab-transformers-completer。
状态数据库#
可以通过从 @jupyterlab/statedb
导入 IStateDB
并将其添加到插件的 requires
列表中来访问状态数据库
const id = 'foo-extension:IFoo';
const IFoo = new Token<IFoo>(id);
interface IFoo {}
class Foo implements IFoo {}
const plugin: JupyterFrontEndPlugin<IFoo> = {
id,
autoStart: true,
requires: [IStateDB],
provides: IFoo,
activate: (app: JupyterFrontEnd, state: IStateDB): IFoo => {
const foo = new Foo();
const key = `${id}:some-attribute`;
// Load the saved plugin state and apply it once the app
// has finished restoring its former layout.
Promise.all([state.fetch(key), app.restored])
.then(([saved]) => { /* Update `foo` with `saved`. */ });
// Fulfill the plugin contract by returning an `IFoo`.
return foo;
}
};
内核子 shell#
内核子 shell 允许在支持它们的内核中并发执行代码。子 shell 是独立的执行线程,允许在内核忙于执行长时间运行的代码时与内核交互,从而实现非阻塞通信和并行执行。
内核支持
子 shell 由以下支持
ipykernel 7.0.0+ (Python 内核) - 内核通过内核信息回复中的
supported_features: ['kernel subshells']
来宣传支持其他实现 JEP 91 的内核
用户界面
有关用户界面详细信息,请参阅 子 shell 控制台。
扩展开发
扩展开发人员可以通过内核服务 API 使用子 shell 功能
import { INotebookTracker } from '@jupyterlab/notebook';
// Get the current kernel from a notebook
const current = tracker.currentWidget;
if (!current) return;
const kernel = current.sessionContext.session?.kernel;
if (!kernel) return;
// Check if kernel supports subshells
if (kernel.supportsSubshells) {
// Create a new subshell
const reply = await kernel.requestCreateSubshell({}).done;
const subshellId = reply.content.subshell_id;
console.log(`Created subshell: ${subshellId}`);
// List existing subshells
const listReply = await kernel.requestListSubshell({}).done;
console.log(`Active subshells: ${listReply.content.subshell_id}`);
// Execute code in a specific subshell
const future = kernel.requestExecute(
{ code: 'print("Hello from subshell!")' },
false, // disposeOnDone
{ subshell_id: subshellId } // metadata
);
await future.done;
// Delete a subshell when done
await kernel.requestDeleteSubshell({ subshell_id: subshellId }).done;
console.log(`Deleted subshell: ${subshellId}`);
}
有关详细规范,请参阅 JEP 91。
LSP 功能#
JupyterLab 提供了一个与语言服务器通信的基础设施。如果 LSP 服务被激活并且用户安装了语言服务器,JupyterLab 将为打开的笔记本或文件的语言启动语言服务器。扩展作者可以通过从 @jupyterlab/lsp
请求 ILSPDocumentConnectionManager
令牌来访问虚拟文档和打开文档的关联 LSP 连接。
以下是向语言服务器发出请求的示例。
const plugin: JupyterFrontEndPlugin<void> = {
id,
autoStart: true,
requires: [ILSPDocumentConnectionManager],
activate: async (app: JupyterFrontEnd, manager: ILSPDocumentConnectionManager): Promise<void> => {
// Get the path to the opened notebook of file
const path = ...
// Get the widget adapter of opened document
const adapter = manager.adapters.get(path);
if (!adapter) {
return
}
// Get the associated virtual document of the opened document
const virtualDocument = adapter.virtualDocument;
// Get the LSP connection of the virtual document.
const connection = manager.connections.get(virtualDocument.uri);
...
// Send completion request to the language server
const response = await connection.clientRequests['textDocument/completion'].request(params);
...
}
};
有时,LSP 扩展包含一个 CodeMirror 扩展来修改代码编辑器。在这些情况下,您可以遵循此示例
const renamePlugin: JupyterFrontEndPlugin<void> = {
id,
autoStart: true,
requires: [ILSPDocumentConnectionManager, ILSPFeatureManager, IWidgetLSPAdapterTracker],
activate: (app: JupyterFrontEnd, connectionManager: ILSPDocumentConnectionManager, featureManager: ILSPFeatureManager, tracker: IWidgetLSPAdapterTracker) => {
const FEATURE_ID = "rename_symbol";
const extensionFactory: EditorAdapter.ILSPEditorExtensionFactory = {
name: FEATURE_ID,
factory: (options) => {
const { editor, widgetAdapter } = options;
// Get the editor
const ceEditor: CodeEditor.IEditor | null = editor.getEditor();
if (!ceEditor) {
return null;
}
// Get the associated virtual document of the opened document
if (!widgetAdapter.virtualDocument) {
return null;
}
// Get the LSP connection of the virtual document.
const connection = connectionManager.connections.get(widgetAdapter.virtualDocument.uri);
if (!connection || !connection.provides('renameProvider')) {
return null;
}
// Create a CodeMirror extension that listens for double click, gets the
// selected code and makes a LSP request to rename it and prints the results.
const ext = EditorView.domEventHandlers({ dblclick: (e, view) => {
const range = ceEditor.getSelection();
const res = connection.clientRequests['textDocument/rename'].request({
newName: "test",
position: { line: range.start.line, character: range.start.column },
textDocument: { uri: widgetAdapter.virtualDocument!.uri }
});
res.then(value => {
console.debug(value);
}).catch(e => console.error);
}});
// Wrap the CodeMirror extension in the extension registry object.
return EditorExtensionRegistry.createImmutableExtension(ext);
}
}
// Register the extension with the LSP feature
featureManager.register({
id: FEATURE_ID,
extensionFactory
});
}
};
内容供应#
文件系统交互可以通过添加以下内容进行自定义
内容提供者,选择性地替换内容的获取和同步方式
驱动器,添加新的内容源,类似于物理硬盘驱动器
尽管内容提供者和驱动器都旨在提供 Contents API 方法(如 get()
和 save()
)的自定义实现,并且可以选择自定义 sharedModelFactory
,但预期的用例以及它们在用户界面中的公开方式是不同的
驱动器
用例:提供默认驱动器上不可用的额外内容。
用户界面:来自驱动器的文件和目录路径以驱动器名称和冒号为前缀。
内容提供者
用例:通过扩展 Contents API 方法来修改用于数据检索的协议(例如,内容流式传输、实时协作),适用于已存在于某个驱动器上的文件。
用户界面:用户将通过“打开方式”下拉菜单选择如何打开文件时,选择一个与内容提供者关联的小部件工厂。
要注册自定义驱动器,请使用内容管理器的 addDrive
方法。驱动器需要遵循 IDrive
接口。对于使用符合 jupyter-server REST API 的驱动器,您可能希望扩展或重用内置的 Drive
类,如下所示
import { Drive, ServerConnection } from '@jupyterlab/services';
const customDrivePlugin: JupyterFrontEndPlugin<void> = {
id: 'my-extension:custom-drive',
autoStart: true,
activate: (app: JupyterFrontEnd) => {
const myDrive = new Drive({
apiEndpoint: 'api/contents',
name: 'MyNetworkDrive',
serverSettings: {
baseUrl: 'https://your-jupyter-server.com',
// ...
} as ServerConnection.ISettings,
});
app.serviceManager.contents.addDrive(myDrive);
}
};
要使用内容提供者,首先将其注册到一个驱动器(或多个驱动器)上
import { Contents, ContentsManager, RestContentProvider } from '@jupyterlab/services';
interface IMyContentChunk {
/** URL allowing to fetch the content chunk */
url: string;
}
interface CustomContentsModel extends Contents.IModel {
/**
* Specializes the content (which in `Contents.IModel` is just `any`).
*/
content: IMyContentChunk[];
}
class CustomContentProvider extends RestContentProvider {
async get(
localPath: string,
options?: Contents.IFetchOptions,
): Promise<CustomContentsModel> {
// Customize the behaviour of the `get` action to fetch a list of
// content chunks from a custom API endpoint instead of the `get`
try {
return getChunks(); // this method needs to be implemented
}
catch {
// fall back to the REST API on errors:
const model = await super.get(localPath, options);
return {
...model,
content: []
};
}
}
// ...
}
const customContentProviderPlugin: JupyterFrontEndPlugin<void> = {
id: 'my-extension:custom-content-provider',
autoStart: true,
activate: (app: JupyterFrontEnd) => {
const drive = (app.serviceManager.contents as ContentsManager).defaultDrive;
const registry = drive?.contentProviderRegistry;
if (!registry) {
// If content provider is a non-essential feature and support for JupyterLab <4.4 is desired:
console.error('Cannot initialize content provider: no content provider registry.');
return;
}
const customContentProvider = new CustomContentProvider({
// These options are only required if extending the `RestContentProvider`.
apiEndpoint: '/api/contents',
serverSettings: app.serviceManager.serverSettings,
});
registry.register('my-custom-provider', customContentProvider);
}
};
然后创建并注册一个小部件工厂,该工厂将了解如何使用您的自定义内容提供者
class ExampleWidgetFactory extends ABCWidgetFactory<ExampleDocWidget, ExampleDocModel> {
protected createNewWidget(
context: DocumentRegistry.IContext<ExampleDocModel>
): ExampleDocWidget {
return new ExampleDocWidget({
context,
content: new ExamplePanel(context)
});
}
}
const widgetFactoryPlugin: JupyterFrontEndPlugin<void> = {
id: 'my-extension:custom-widget-factory',
autoStart: true,
activate: (app: JupyterFrontEnd) => {
const widgetFactory = new ExampleWidgetFactory({
name: FACTORY,
modelName: 'example-model',
fileTypes: ['example'],
defaultFor: ['example'],
// Instructs the document registry to use the custom provider
// for context of widgets created with `ExampleWidgetFactory`.
contentProviderId: 'my-custom-provider'
});
app.docRegistry.addWidgetFactory(widgetFactory);
}
};
其中 ExampleDocModel
现在可以预期 CustomContentsModel
而不是 Contents.IModel
class ExampleDocModel implements DocumentRegistry.IModel {
// ...
fromJSON(chunks: IMyContentChunk[]): void {
this.sharedModel.transact(() => {
let i = 0;
for (const chunk of chunks) {
const chunk = fetch(chunk.url);
this.sharedModel.set(`chunk-${i}`, chunk);
i += 1;
}
});
}
fromString(data: string): void {
const chunks = JSON.parse(data) as IMyContentChunk[];
return this.fromJSON(chunks);
}
}
有关小部件工厂的完整示例(尽管未使用内容提供者),请参阅 documents extension example。