从index.ts触发导出可执行脚本命令

2024-05-23 22:46:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个JupyterLab扩展,它最终将允许用户测试代码的功耗,第一步是创建一个按钮,将笔记本的内容作为可执行脚本下载

这可以通过文件>;将笔记本导出为…>;可执行脚本或通过带有Export Notebook:Executable Script的命令面板,因此我认为它应该相当简单。但是,从index.ts文件运行此命令时遇到问题;任何帮助都将不胜感激。我认为我的主要问题是不知道如何将正确的论点传递给笔记本:导出为格式,但我可能也做了其他错误的事情

我使用扩展教程(扩展教程-JupyterLab 3.0.16文档)中的此命令和cookiecutter设置了环境:

conda create-n greencode-ext3--覆盖通道--严格通道优先级-c conda forge-c anaconda jupyterlab=3 cookiecutter nodejs jupyter打包git

cookiecutter GitHub-jupyterlab/扩展cookiecutter ts:Typescript中jupyterlab扩展的cookiecutter配方

这是我的index.ts文件:

import {
  JupyterLab,
  JupyterFrontEnd,
  JupyterFrontEndPlugin,
} from '@jupyterlab/application';

import { ITranslator } from '@jupyterlab/translation';

import { ToolbarButton } from "@jupyterlab/apputils";
import { DocumentRegistry } from "@jupyterlab/docregistry";
import { INotebookModel, NotebookPanel, INotebookTracker } from "@jupyterlab/notebook";

import { IDisposable } from "@lumino/disposable";

export class ButtonExtension implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel> {
    
  constructor(app: JupyterLab) {
      this.app = app;
  }
    
  readonly app: JupyterLab;

  createNew(panel: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>): IDisposable {
     const { commands } = this.app;
     const command = 'notebook:export-to-format'

     // Create the toolbar button
     let mybutton = new ToolbarButton({
         label: 'Measure Power Usage',
         onClick: () => { 
             alert('You did it');
             commands.execute(command, {formatLabel: 'script'});
          }
       });

    // Add the toolbar button to the notebook toolbar
    panel.toolbar.insertItem(10, 'MeasurePowerUsage', mybutton);
    console.log("MeasPowerUsage activated");

    // The ToolbarButton class implements `IDisposable`, so the
    // button *is* the extension for the purposes of this method.
    return mybutton;
  }
}

/**
 * Initialization data for the greencode-ext3 extension.
 */
const yourPlugin: JupyterFrontEndPlugin<void> = {
  id: '@your-org/plugin-name',
  autoStart: true,
  requires: [ITranslator, INotebookTracker],
  activate: (app: JupyterFrontEnd) => {
    const your_button = new ButtonExtension(new JupyterLab);
    app.docRegistry.addWidgetExtension('Notebook', your_button);
  }
}

export default yourPlugin;

Tags: 文件thefromimport命令app笔记本button