An Engineering Notebook post. Feel free to ignore.
Perhaps I should put this directly in Imports, code reuse and initialization
<https://github.com/leo-editor/leo-editor/issues/567> or in Design of
Jupyter project <https://github.com/leo-editor/leo-editor/issues/566> but
these are getting pretty long.
*tl;dr:* It's surprisingly easy to use the jupyter_client module.
Terry and I agree that simple prototypes would be good. Here is the first
one. It's surprisingly informative:
import os
import time
from jupyter_client import connect
try:
path = connect.find_connection_file()
print(path)
print('created: ', time.ctime(os.path.getctime(path)))
print('modified:', time.ctime(os.path.getmtime(path)))
with open(path, 'r') as f:
print(f.read())
except IOError as err: # No kernel file.
print(err)
print('Done')
And here is sample output:
C:\Users\edreamleo\AppData\Roaming\jupyter\runtime\kernel-b9a6f7c7-6b8c-4fc6-b607-fb27a5f9f34d.json
created: Fri Nov 3 07:49:27 2017
modified: Fri Nov 3 07:49:50 2017
{
"iopub_port": 50177,
"transport": "tcp",
"hb_port": 50180,
"stdin_port": 50178,
"kernel_name": "",
"signature_scheme": "hmac-sha256",
"ip": "127.0.0.1",
"key": "5c17ee3a-a8bf-445c-9cd8-9eb4b48518af",
"control_port": 50179,
"shell_port": 50176
}
Done
*A window into Jupyter operation*
This code uncovers details about jupyter's operation that would not be
obvious from reading docs:
- connect.find_connection_file returns *at most one* connection file, the
most recent one.
- Initialization is not a problem. Using defaults for all keyword args in
connect.find_connection_file works
- On windows, jupyter keeps connection data in the AppData/jupyter/runtime
folder.
connect.find_connection_file finds these data by default.
- This folder contains kernel-<kernel-id>.json, nbserver<server-id>.json
and notebook_cookie_secret files.
To better understand what jupyter does, I deleted the entire contents of
the AppData/jupyter/runtime folder and reran the script. I got:
Could not find 'kernel-*.json' in
['.', 'C:\\Users\\edreamleo\\AppData\\Roaming\\jupyter\\runtime']
I restarted (by hand) a jupyter server in a new console, but rerunning the
script still gave the Could not find 'kernel-*.json' message.
It was only after choosing "Restart Kernel" from the Jupyter web page that
a kernel-*.json file was created.
Going back to the console window, I then saw
[I 07:49:50.448 NotebookApp] Kernel restarted:
b9a6f7c7-6b8c-4fc6-b607-fb27a5f9f34d
The last line contains the kernel-id, which is good to know.
*Summary*
This short script shows what the Jupyter server (and etc.) are doing.
The jupyter/runtime folder contains a cookie and .json files describing the
notebook server and *zero* or more kernels. Apparently, the server does
*not* create a python kernel on startup.
Running the script revealed no problems with imports or initing the code.
The script contains no gui code, nor does it init any, so it can be run as
a Leo script.
I recommend this approach to Terry, or anyone else interested in doing
quick prototypes.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.