jupyter-js-services - 如何保存笔记本

时间:2016-02-13 00:59:53

标签: jupyter jupyter-notebook

我试图使用jupyter作为我系统的后端,现在我使用jupyter-js-api文档中的示例。 使用IKernel和INotebookSession我设法执行简单的代码并从内核获取响应。

但我可以弄清楚如何提取笔记本电脑本身。没有像" saveNotebook()"在API中。我尝试执行session.renameNotebook(),它成功完成,但没有文件出现在文件系统中(尝试了不同的路径,如" /tmp/trynote.ipynb"" trynote.ipnb"等等上...)。

以下是代码,它是http://jupyter.org/jupyter-js-services/页面

中略有编辑的示例
#!/usr/bin/env node

var jpt = require("jupyter-js-services");
var xr = require("xmlhttprequest");
var ws = require("ws");

global.XMLHttpRequest = xr.XMLHttpRequest;
global.WebSocket = ws;

// start a new session
var options = {
    baseUrl: 'http://localhost:8889',
    wsUrl: 'ws://localhost:8889',
    kernelName: 'python',
    notebookPath: 'trynote.ipynb'
};
jpt.startNewSession(options).then((session) => {
    // execute and handle replies on the kernel
    var future = session.kernel.execute({ code: 'print(5 * 5);' });
    future.onDone = (msg) => {
        console.log('Future is fulfilled: ');
        console.log(msg);
    };
    future.onIOPub = (msg) => {
        console.log("Message in IOPub: ");
        console.log(msg);
    };

    // rename the notebook
    session.renameNotebook('trynote2.ipynb').then(() => {
        console.log('Notebook renamed to', session.notebookPath);
    });

    // register a callback for when the session dies
    session.sessionDied.connect(() => {
        console.log('session died');
    });

    // kill the session
    session.shutdown().then(() => {
        console.log('session closed');
    });

});

Look和ContentManager API似乎可以处理现有文件或创建新文件,但不清楚它是如何绑定到会话的。 更多,甚至最简单的尝试使用" newUntitled"函数给出了404响应......

var contents = new jpt.ContentsManager('http://localhost:8889');
// create a new python file
contents.newUntitled("foo", { type: "file", ext: "py" }).then(
    (model) => {
        console.log(model.path);
    }
);

我对这一切感到有些迷茫,并希望得到任何解释。 感谢..

0 个答案:

没有答案