jupyter实验室启动器:更改启动器图标的软顺序

时间:2020-10-09 16:32:07

标签: python jupyter jupyter-lab jupyterhub jupyter-kernel

在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并不十分了解)。

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

1 个答案:

答案 0 :(得分:1)

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

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

首先是“默认”(或“本机”)内核,然后是其他根据其display_name排序的内核。

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

据我所知,控制内核顺序的唯一方法是为内核指定display_name,以使它们根据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