Turning an app with embedded Python into a Jupyter kernel

未关闭
#675 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
jupyter-notebook, python

调研方向

先阅读 kernel.json 和 launch.py 示例,然后检查所引用的 qgspythonutilsimpl.cpp 启动代码以及 traitlets application.py 的参数处理。将观察到的重复启动行为与 ipykernel 的启动流程进行比较,并确定所请求的嵌入式进程或包装器进程方案是否可行。完成的标准是形成有文档记录的实现方向,并明确解决启动和执行行为。

由索引模型根据 Issue 内容生成。

描述

I am doing a few experiments on apps with Python integration via embedded Python, i.e. QGIS (and FreeCAD). The objective is to turn them into Jupyter kernels. Both apps come with their own Python console, but I'd like to run their GUI and Jupyter lab side-by-side while Jupyter's kernel is actually the embedded Python interpreter of the GUI app. I essentially want to use Jupyter for interacting with the apps instead of the apps' own consoles.

I started with QGIS (and on Windows, because I was curious ...). For "implementation details" see below.

QGIS launches, but from Jupyter's perspective, the kernel keeps starting. It never "finishes" starting. Interestingly, I can actually re-start the kernel, i.e. QGIS, from within Jupyter just fine. Either way, code can not be executed.

  • Completely ignoring my "implementation": Is what I described even possible?
  • I am trying to make sense of ipykernel (and ipython for that matter), but it is not trivial to get started. Does my "implementation" make any sense or do I have to approach things differently altogether?
  • Alternatively, could I turn an already running process (pure Python or with embedded Python) into a "kernel" by attaching to it from some kind of a wrapper process (via some form of IPC) which is the actual kernel from Jupyter's perspective?

This is what I have so far:

kernel.json, which injects code at startup via PYQGIS_STARTUP:

{
 "argv": [
  "C:/Users/demo/mambaforge/envs/cluster/Library/bin\\qgis.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "QGIS",
 "language": "python",
 "env": {
  "PYQGIS_STARTUP": "C:/Users/demo/mambaforge/envs/cluster/share/jupyter/kernels/qgis/launch.py"
 }
}

launch.py which is supposed to launch the kernelapp. argv is a bit of an issue because QGIS does not expose it via sys, hence the ugly hack. I think it should forward the args to the right place in traitlets:

from threading import Thread
import os
from time import time
import sys

from qgis.core import QgsApplication

from ipykernel import kernelapp as app

LOG_FN = 'C:/Users/demo/mambaforge/envs/cluster/share/jupyter/kernels/qgis/log.out'

def log_out(msg):
    with open(LOG_FN, mode = 'a') as f:
        f.write('%d | %s\n' % (round(time()), msg))

def launch_ipython():
    log_out('Argv...')
    argv = QgsApplication.arguments().copy() # HACK: sys.argv not available
    log_out(str(argv))
    log_out('App...')
    app.launch_new_instance(argv = argv) # Blocks ... ?
    log_out('Done?')

sys._ipython = Thread(target = launch_ipython) # HACK for later access
sys._ipython.start()

log.out from a single kernel start. Looks like two instances, threads or processes are getting started:

1621087681 | Argv...
1621087681 | ['C:/Users/demo/mambaforge/envs/cluster/Library/bin\\qgis.exe', '-m', 'ipykernel_launcher', '-f', 'C:\\Users\\demo\\AppData\\Roaming\\jupyter\\runtime\\kernel-b5e49e78-f742-49c1-b75d-6425c4fbee6a.json']
1621087681 | App...
1621087681 | Argv...
1621087681 | ['C:/Users/demo/mambaforge/envs/cluster/Library/bin\\qgis.exe', '-m', 'ipykernel_launcher', '-f', 'C:\\Users\\demo\\AppData\\Roaming\\jupyter\\runtime\\kernel-b5e49e78-f742-49c1-b75d-6425c4fbee6a.json']
1621087681 | App...
主要语言
Python
星标
734
派生
411
平均合并
1 天 2 小时
30 天内合并 PR
9

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ipython/ipykernel 的其他 Issue

查看 ipython/ipykernel 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。