跳转到主要内容

一个用于从服务器执行代码单元格的Jupyter Server扩展。

项目描述

jupyter_server_nbmodel

Github Actions Status Binder

一个用于从服务器执行代码单元格的Jupyter Server扩展。

此扩展由名为 jupyter_server_nbmodel 的服务器扩展Python包和名为 jupyter-server-nbmodel 的前端扩展NPM包组成。

需求

  • Jupyter Server
  • [推荐] JupyterLab/Notebook的实时协作:这将把内核的结果从服务器推送到笔记本。

安装

要在JupyterLab或Notebook 7中使用此扩展,执行

pip install jupyter_server_nbmodel[lab]

仅用于API

pip install jupyter_server_nbmodel

或使用推荐

pip install jupyter_server_nbmodel[rtc]

卸载

要删除扩展,执行

pip uninstall jupyter_server_nbmodel

故障排除

如果您看到前端扩展但无法使用,请检查服务器扩展是否已启用

jupyter server extension list

如果服务器扩展已安装并启用,但您未看到前端扩展,请检查前端扩展是否已安装

jupyter labextension list

它如何工作

通用情况

执行Python代码片段:print("hello")

sequenceDiagram
    Frontend->>+Server: POST /api/kernels/<id>/execute
    Server->>+ExecutionStack: Queue request
    ExecutionStack->>Kernel: Execute request msg
    activate Kernel
    ExecutionStack-->>Server: Task uid
    Server-->>-Frontend: Returns task uid
    loop Running
        Kernel->>Shared Document: Add output
        Shared Document->>Frontend: Document update
    end
    loop While status is 202
        Frontend->>+Server: GET /api/kernels/<id>/requests/<uid>
        Server->>ExecutionStack: Get task result
        ExecutionStack-->>Server: null
        Server-->>-Frontend: Request status 202
    end
    Kernel-->>ExecutionStack: Execution reply
    deactivate Kernel
    Frontend->>+Server: GET /api/kernels/<id>/requests/<uid>
    Server->>ExecutionStack: Get task result
    ExecutionStack-->>Server: Result
    Server-->>-Frontend: Status 200 & result

有输入的情况

执行Python代码片段:input("Age:")

sequenceDiagram
    Frontend->>+Server: POST /api/kernels/<id>/execute
    Server->>+ExecutionStack: Queue request
    ExecutionStack->>Kernel: Execute request msg
    activate Kernel
    ExecutionStack-->>Server: Task uid
    Server-->>-Frontend: Returns task uid
    loop Running
        Kernel->>Shared Document: Add output
        Shared Document->>Frontend: Document update
    end
    loop While status is 202
        Frontend->>+Server: GET /api/kernels/<id>/requests/<uid>
        Server->>ExecutionStack: Get task result
        ExecutionStack-->>Server: null
        Server-->>-Frontend: Request status 202
    end
    Kernel->>ExecutionStack: Set pending input
    Frontend->>+Server: GET /api/kernels/<id>/requests/<uid>
    Server->>ExecutionStack: Get task result
    ExecutionStack-->>Server: Pending input
    Server-->>-Frontend: Status 300 & Pending input
    Frontend->>+Server: POST /api/kernels/<id>/input
    Server->>Kernel: Send input msg
    Server-->>-Frontend: Returns
    loop While status is 202
        Frontend->>+Server: GET /api/kernels/<id>/requests/<uid>
        Server->>ExecutionStack: Get task result
        ExecutionStack-->>Server: null
        Server-->>-Frontend: Request status 202
    end
    Kernel-->>ExecutionStack: Execution reply
    deactivate Kernel
    Frontend->>+Server: GET /api/kernels/<id>/requests/<uid>
    Server->>ExecutionStack: Get task result
    ExecutionStack-->>Server: Result
    Server-->>-Frontend: Status 200 & result

[!注意] 代码片段始终发送到POST /api/kernels/<id>/execute 请求的正文,以避免文档模型不一致;后端的文档最终与前端相同(文档更新不是瞬时的)。

ExecutionStack 为每个内核维护一个执行队列,以确保执行顺序。

贡献

开发安装

注意:您将需要NodeJS来构建扩展包。

jlpm 命令是JupyterLab的固定版本 yarn,它是与JupyterLab一起安装的。您可以使用 yarnnpm 替代以下 jlpm

# Clone the repo to your local environment
# Change directory to the jupyter_server_nbmodel directory
# Install package in development mode
pip install -e ".[test]"
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Server extension must be manually installed in develop mode
jupyter server extension enable jupyter_server_nbmodel
# Rebuild extension Typescript source after making changes
jlpm build

您可以在不同的终端中同时监视源目录并运行JupyterLab,以监视扩展的源代码并在自动重新构建扩展时进行更改。

# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm watch
# Run JupyterLab in another terminal
jupyter lab --autoreload

在监视命令运行时,每次保存的更改都会立即在本地上构建并可在运行的JupyterLab中访问。刷新JupyterLab以在浏览器中加载更改(您可能需要等待几秒钟才能重新构建扩展)。

默认情况下,jlpm build 命令为该扩展生成源映射,以便更容易使用浏览器开发工具进行调试。要同时为JupyterLab核心扩展生成源映射,可以运行以下命令

jupyter lab build --minimize=False

开发卸载

# Server extension must be manually disabled in develop mode
jupyter server extension disable jupyter_server_nbmodel
pip uninstall jupyter_server_nbmodel

在开发模式下,您还需要删除由jupyter labextension develop命令创建的符号链接。要找到其位置,可以运行jupyter labextension list以确定labextensions文件夹的位置。然后您可以在该文件夹中删除名为jupyter-server-nbmodel的符号链接。

测试扩展

服务器测试

此扩展使用Pytest进行Python代码测试。

安装测试依赖关系(只需安装一次)

pip install -e ".[test]"
# Each time you install the Python package, you need to restore the front-end extension link
jupyter labextension develop . --overwrite

要执行它们,运行

pytest -vv -r ap --cov jupyter_server_nbmodel

前端测试

此扩展使用Jest进行JavaScript代码测试。

要执行它们,执行

jlpm
jlpm test

集成测试

此扩展使用Playwright进行集成测试(也称为用户级测试)。更确切地说,JupyterLab辅助Galata用于处理在JupyterLab中对扩展的测试。

更多详细信息请参阅ui-tests的README。

手动测试

# Terminal 1.
jupyter server --port 8888 --autoreload --ServerApp.disable_check_xsrf=True --IdentityProvider.token= --ServerApp.port_retries=0

# Terminal 2.
KERNEL=$(curl -X POST http://localhost:8888/api/kernels)
echo $KERNEL
KERNEL_ID=$(echo $KERNEL | jq --raw-output '.id')
echo $KERNEL_ID
REQUEST=$(curl --include http://localhost:8888/api/kernels/$KERNEL_ID/execute -d "{ \"code\": \"print('1+1')\" }")
RESULT=$(echo $REQUEST | grep -i ^Location: | cut -d' ' -f2 | tr -d '\r')
echo $RESULT

curl http://localhost:8888$RESULT
{"status": "ok", "execution_count": 1, "outputs": "[{\"output_type\": \"stream\", \"name\": \"stdout\", \"text\": \"1+1\\n\"}]"}

运行测试

安装依赖项

pip install -e ".[test]"

要运行Python测试,使用

pytest

# To test a specific file
pytest jupyter_server_nbmodel/tests/test_handlers.py

# To run a specific test
pytest jupyter_server_nbmodel/tests/test_handlers.py -k "test_post_execute"

开发卸载

pip uninstall jupyter_server_nbmodel

打包扩展

RELEASE

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

jupyter_server_nbmodel-0.1.1a1.tar.gz (153.4 kB 查看散列)

上传于

构建发行版

jupyter_server_nbmodel-0.1.1a1-py3-none-any.whl (28.9 kB 查看哈希值)

上传于 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面