0

在 Jupyter Lab 启动器中,我想控制启动器图标的排序顺序。基本上,我希望最新版本的 Python 首先出现在左侧。

现在,我看不出是什么决定了排序顺序。查看 /usr/local/share/jupyter/kernels/ 和 ~/.local/share/jupyter/ 中的 kernel.json 规范,看起来排序顺序不是基于 display_name、语言或内核显示的.json 创建时间戳。排序顺序看起来有点随意,但也许我没有看到它使用的模式。

看起来 Jupyter Lab 启动器是使用 React 应用程序(https://github.com/jupyterlab/jupyterlab/blob/master/packages/launcher/src/index.tsx#L224)生成的,但我不确定在哪里它从(并且对 React 不太熟悉)获取 Launchers 列表。

有人知道如何更改启动器上图标的排序顺序吗?

4

1 回答 1

2

我认为您是在专门询问启动器中笔记本内核的顺序。

排序顺序看起来有点随意,但也许我没有看到它使用的模式。

它首先是“默认”(或“本机”)内核,然后是根据它们的display_name.

有人知道如何更改启动器上图标的排序顺序吗?

据我所见,控制内核顺序的唯一方法是display_name为内核指定 s ,以便它们的顺序String.localeCompare()与您想要的匹配(如果不是,则结合排除“默认”/“本机”内核)你想先看到的)。

解释...

图标的顺序首先由等级控制,然后localeCompare()由标签控制。这是排序发生的地方,在您已经找到的启动器扩展的 index.tsx 文件中:

// Within each category sort by rank
for (const cat in categories) {
  categories[cat] = categories[cat].sort(
    (a: ILauncher.IItemOptions, b: ILauncher.IItemOptions) => {
      return Private.sortCmp(a, b, this._cwd, this._commands);
    }
  );
}

https://github.com/jupyterlab/jupyterlab/blob/0bec95d5364986eaf19dab4721a98c75f13db40f/packages/launcher/src/index.tsx#L172-L179

排序功能:

  /**
   * A sort comparison function for a launcher item.
   */
  export function sortCmp(
    a: ILauncher.IItemOptions,
    b: ILauncher.IItemOptions,
    cwd: string,
    commands: CommandRegistry
  ): number {
    // First, compare by rank.
    const r1 = a.rank;
    const r2 = b.rank;
    if (r1 !== r2 && r1 !== undefined && r2 !== undefined) {
      return r1 < r2 ? -1 : 1; // Infinity safe
    }

    // Finally, compare by display name.
    const aLabel = commands.label(a.command, { ...a.args, cwd });
    const bLabel = commands.label(b.command, { ...b.args, cwd });
    return aLabel.localeCompare(bLabel);
  }
}

https://github.com/jupyterlab/jupyterlab/blob/0bec95d5364986eaf19dab4721a98c75f13db40f/packages/launcher/src/index.tsx#L499-L520

不幸的是,内核的等级是固定的:0对于默认内核,Infinity对于其他内核:

for (const name in specs.kernelspecs) {
  const rank = name === specs.default ? 0 : Infinity;

https://github.com/jupyterlab/jupyterlab/blob/0bec95d5364986eaf19dab4721a98c75f13db40f/packages/notebook-extension/src/index.ts#L770

如果您尝试如上所述控制顺序,您可能需要考虑排除默认内核(例如Python 3或类似),因为它总是首先出现:

class KernelSpecManager(LoggingConfigurable):

    [...]

    ensure_native_kernel = Bool(True, config=True,
        help="""If there is no Python kernelspec registered and the IPython
        kernel is available, ensure it is added to the spec list.
        """
    )

https://github.com/jupyter/jupyter_client/blob/012cb1948d92c6f329cf1a749fece6c99a2485bf/jupyter_client/kernelspec.py#L122-L125

于 2020-10-13T10:50:08.957 回答