【问题标题】:How can I evaluate Python code in the document context from JavaScript in JupyterLab?如何在 JupyterLab 的 JavaScript 中评估文档上下文中的 Python 代码?
【发布时间】:2018-09-20 15:36:04
【问题描述】:

使用 Jupyter Notebooks,我可以拥有一个单元格

%%javascript IPython.notebook.kernel.execute('x = 42')

然后,在文档的其他地方,带有x 的 Python 代码单元将显示它按预期绑定到 42。

我正在尝试用 JupyterLab 制作类似的东西。我知道我应该编写一个插件而不是使用 ad-hoc JS,这很好,但我没有从笔记本中找到类似于全局 IPython 的内核接口:

import { JupyerLab, JupyterLabPlugin } from '@jupyterlab/application';
const extension: JupyterLabPlugin<void> = {
    // ...
    requires: [],
    activate: (app: JupyterLab) => {
        // can I get to Python evaluation through app?
        // by adding another class to `requires` above?
    }
}
export default extension;

【问题讨论】:

    标签: javascript python typescript jupyter-lab


    【解决方案1】:

    这是一个“有效”的 hacky 尝试。如果有人知道内核准备就绪的公开承诺、如何避免中间类或任何其他一般性改进,仍然可以使用建议:

    import { JupyterLab, JupyterLabPlugin } from '@jupyterlab/application';
    import { DocumentRegistry } from '@jupyterlab/docregistry';
    import { INotebookModel, NotebookPanel } from '@jupyterlab/notebook';
    import { IDisposable, DisposableDelegate } from '@phosphor/disposable';
    
    declare global {
        interface Window {
            'execPython': {
                'readyState': string,
                'exec': (code: string) => any,
                'ready': Promise<void>
            } | null
        }
    }
    
    class ExecWidgetExtension implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel> {
        createNew(nb: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>): IDisposable {
            if (window.execPython) {
                return;
            }
            window.execPython = {
                'readyState': 'waiting',
                'exec': null,
                'ready': new Promise((resolve) => {
                    const wait = setInterval(() => {
                        if (!context.session.kernel || window.execPython.readyState === 'ready') {
                            return;
                        }
                        clearInterval(wait);
                        window.execPython.readyState = 'ready';
                        window.execPython.exec = (code: string) =>
                            context.session.kernel.requestExecute({ code }, true);
                        resolve();
                    }, 50);
                })
            };
            // Usage elsewhere: execPython.ready.then(() => execPython.exec('x = 42').done.then(console.log, console.error))
    
            return new DisposableDelegate(() => {});
        }
    }
    
    const extension: JupyterLabPlugin<void> = {
        'id': 'jupyterlab_foo',
        'autoStart': true,
        'activate': (app: JupyterLab) => {
            app.docRegistry.addWidgetExtension('Notebook', new ExecWidgetExtension())
        }
    };
    export default extension;
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2010-11-24
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 2021-01-12
      相关资源
      最近更新 更多