【问题标题】:How to upload notebook contents to jupyter server using jupyter server rest api calls using java?如何使用 java 的 jupyter 服务器 rest api 调用将笔记本内容上传到 jupyter 服务器?
【发布时间】:2021-10-06 03:34:48
【问题描述】:

我有一个支持 python 语言的 jupyter 服务器。我已经创建了带有打印语句的 jupyter 笔记本,并将该笔记本下载到我的本地计算机上。 现在我必须读取本地存储的笔记本的内容,并使用 jupyter 服务器 rest api 调用上传到正在运行的 jupyter 服务器。 我更喜欢 java 编程语言来与 jupyter 服务器 rest api 调用交互以上传笔记本内容。

问题:我可以通过准备请求中预期的请求正文来读取内容并调用 jupyter 服务器 api。 我正在调用jupyter服务器的这个api:PUT:{base_url}/api/contents/a.ipynb

请求正文:

                log.info("jsonNode string : "+jsonNode);
                JSONObject modelRequest = new JSONObject();
                modelRequest.put("name", "a.ipynb");
                modelRequest.put("path", "");
                modelRequest.put("type","notebook");
                modelRequest.put("format","json");
                modelRequest.put("mimetype","None");
                modelRequest.put("content",new JSONObject(jsonNode.toPrettyString()));
                modelRequest.put("writable","True");
                log.info("json model object : "+modelRequest);

我尝试上传不同格式的内容键值,如 String、JSONObject、json String 等,但我无法成功上传 jupyter 服务器笔记本中的内容。 我看到笔记本正在创建,但内容为空。实际上,我希望内容密钥中传递的数据应该被上传,我应该在 jupyter 服务器的笔记本中看到。

任何人在java中有任何想法或线索或实现,我该如何实现? 我正在关注 jupyter server rest api 文档, https://jupyter-server.readthedocs.io/en/latest/developers/rest-api.html https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/jupyter_server/master/jupyter_server/services/api/api.yaml

【问题讨论】:

    标签: java jupyter-notebook jupyter


    【解决方案1】:

    我已经用 OpenAPI 将 https://github.com/jupyter/notebook/raw/v6.4.0/notebook/services/api/api.yaml 编译到 https://github.com/allen-ball/ganymede 的 jupyter-client 模块中。我用一个单元创建了一个简单的笔记本。

    print(1 + 2);
    

    $$.nscNotebookServicesClient 的一个实例,它是 OpenAPI 生成的 ganymede.jupyter.notebook.ApiClient 的子类。通过一些实验,我能够将内容上传到服务器(然后在 Jupyter Notebook 中打开“copy.ipynb”),如下所示。

    import ganymede.jupyter.notebook.*;
    import ganymede.jupyter.notebook.api.*;
    import ganymede.jupyter.notebook.model.*;
    
    var contents = new ContentsApi($$.nsc).apiContentsPathGet("trivial.ipynb", "file", "text", 1);
    
    print(contents);
    
    class Contents {
        name: trivial.ipynb
        path: trivial.ipynb
        type: file
        writable: true
        created: 2021-08-02T00:03:43.820449Z
        lastModified: 2021-08-02T00:03:43.820449Z
        size: 648
        mimetype: text/plain
        content: {
         "cells": [
          {
           "cell_type": "code",
           "execution_count": 1,
           "id": "f74b3993",
           "metadata": {},
           "outputs": [
            {
             "data": {
              "text/plain": [
               "3"
              ]
             },
             "execution_count": 1,
             "metadata": {},
             "output_type": "execute_result"
            }
           ],
           "source": [
            "print(1 + 2);"
           ]
          }
         ],
         "metadata": {
          "kernelspec": {
           "display_name": "Ganymede 2.0.0 (Java 11)",
           "language": "java",
           "name": "ganymede-2.0.0-java-11"
          },
          "language_info": {
           "file_extension": ".java",
           "mimetype": "text/x-java",
           "name": "java",
           "version": "11"
          }
         },
         "nbformat": 4,
         "nbformat_minor": 5
        }
        
        format: text
    }
    
    var model = new InlineObject();
    
    model.setName("copy.ipynb");
    model.setType("file");
    model.setFormat("text");
    model.setContent(contents.getContent());
    
    var newContents = new ContentsApi($$.nsc).apiContentsPathPut(model.getName(), model);
    
    print(newContents);
    
    class Contents {
        name: copy.ipynb
        path: copy.ipynb
        type: notebook
        writable: true
        created: 2021-08-02T01:01:07.904684Z
        lastModified: 2021-08-02T01:01:07.904684Z
        size: 648
        mimetype: null
        content: null
        format: null
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多