【问题标题】:jupyter lab launcher: change soft order of launcher iconsjupyter lab 启动器:更改启动器图标的软顺序
【发布时间】:2021-01-24 18:23:44
【问题描述】:

在 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 不太熟悉)。

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    据我所知,控制内核顺序的唯一方法是为内核指定display_names,以便它们根据String.localeCompare() 的顺序与您想要的匹配(结合排除“默认” /"native" 内核,如果它不是您想首先看到的内核)。

    解释...

    图标的顺序首先由等级控制,然后是标签上的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)

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 2013-01-01
      • 1970-01-01
      • 2020-06-17
      相关资源
      最近更新 更多