You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JupyterLab中打开.ipynb文件时自动开启目录(TOC)?

在JupyterLab中自动打开目录(TOC)的解决方案

方法一:通过内置设置配置

  1. 打开JupyterLab设置面板(快捷键Ctrl+,或右上角设置图标)
  2. 进入Advanced Settings Editor,左侧找到Table of Contents扩展
  3. 在User Preferences中添加或修改以下配置:
    {
      "autoActivate": true,
      "autoExpand": true,
      "sideBarPosition": "left"
    }
    
    • autoActivate:设为true后,打开支持TOC的文件(如Markdown、Notebook)时自动激活TOC面板
    • autoExpand:自动展开TOC层级
    • sideBarPosition:固定TOC在左侧侧边栏

方法二:自定义JavaScript脚本(解决模拟点击无效问题)

直接模拟点击失效多是因为未监听JupyterLab的文档加载事件,可通过以下脚本实现自动激活:

  1. 安装@jupyterlab/scriptmanager-extension扩展(未安装的话,终端执行pip install jupyterlab_scriptmanager后重启JupyterLab)
  2. 打开设置→Advanced Settings Editor→Script Manager
  3. 在User Scripts中添加以下代码:
    document.addEventListener('DOMContentLoaded', () => {
      // 监听文档创建/切换事件
      document.body.addEventListener('jupyterlab-document-manager:document-created', (e) => {
        const tocPanel = document.querySelector('#jp-toc-panel');
        const sideBar = document.querySelector('#jp-sidebar');
        if (tocPanel && sideBar) {
          // 激活TOC面板并确保侧边栏展开
          tocPanel.classList.add('jp-mod-active');
          sideBar.classList.remove('jp-mod-collapsed');
        }
      });
    });
    
  4. 保存设置后重启JupyterLab,后续打开文件时会自动激活TOC

方法三:设置快捷键快速唤起TOC

如果自动方案仍有问题,可配置快捷键快速打开:

  1. 打开设置→Advanced Settings Editor→Keyboard Shortcuts
  2. 在User Preferences中添加:
    {
      "command": "toc:activate",
      "keys": ["Ctrl+Shift+T"],
      "selector": "body"
    }
    
    保存后即可用快捷键快速切换到TOC面板

内容的提问来源于stack exchange,提问作者Dylano Chan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 09:05:27