如何从JupyterLab中的JavaScript评价文档上下文中的Python代码? - javascript

有了Jupyter笔记本,我可以有一个牢房

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

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

我正在尝试使用JupyterLab产生类似的结果。我知道我应该写一个插件而不是使用临时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;

参考方案

这是一种“有效”的黑客尝试。如果有人知道在哪里有公开的内核承诺,如何避免中间类或任何其他常规改进,仍然可以使用建议:

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;

如果我得到url(''),我该如何使用另一个URL - javascript

我是新手,正在写这篇文章,但是如果源上没有图像,那么我只有空白。有人可以告诉我,如果我正在获取背景图像,如何获取/images/no-image.jpg:url();这是我的代码:<div class="uk-clearfix uk-position-relative"> <div class="recipeb…

对ID为'abc%'的dom执行操作 - javascript

我想对ID为'abc%'的DOM进行一些操作<a id='abc1'></a> <a id='abc2'></a> <a id='abc3'></a> <a id='abc4'></a>…

粗糙的Unicode->没有CLDR的语言代码? - javascript

我在写字典应用。如果用户键入Unicode字符,我想检查该字符是哪种语言。例如字 - returns ['zh', 'ja', 'ko'] العربية - returns ['ar'] a - returns ['en', 'fr', …

Jsonp没有出现``访问控制允许来源''错误 - javascript

在我的PHP中,我喜欢这样来回显jsonp类型的“ json数据”echo $_GET['callback'] . '('.json_encode($arr).')'; 在我的js(angularjs)中,$http.get('http://example.com/app/?callbac…

Javascript vs python:具有两个递归分支的函数的不同输出 - javascript

我一直在将python代码毫无问题地翻译成javascript,但对于以下示例,我却没有这样做,也不知道是什么原因。尽管有相似的代码,但是javascript代码似乎产生了与python完全不同的输出。我已经研究了这个问题,并且似乎javascript无法执行第二个递归分支?谁知道我该如何使javascript代码输出与python相似的代码?谢谢1- Py…