Turning an app with embedded Python into a Jupyter kernel

オープン
#675 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
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時間
マージ済み PR(30日)
9

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ipython/ipykernel のほかの issue

ipython/ipykernel の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。