Running notebooks on another machine, over a tunnel or otherwise.
Tunnelled servers
A server reached through an SSH tunnel answers at localhost:
ssh -L 9999:localhost:9999 box
so its address does not name the machine the files are on. emjupy finds it, in order, from:
emjupy-remote-root, if set: a complete path, used as given.- The address, if it names a host other than
localhost. emjupy-ssh-host, if set.- The command line of the
sshprocess that forwards the port, read withpson this machine. emjupy keeps the answer, including none, for a minute per port, because a reference search maps every result.
Cases 2 to 4 give only the host. The directory comes from the kernel, which reports the notebook's own. So notebooks in different directories of one machine resolve to different roots without help.
Set emjupy-ssh-host when no ssh process can be found – a tunnel made
outside Emacs by other means:
(setq emjupy-ssh-host "box")
For several servers, an alist keyed by server label:
(setq emjupy-ssh-host '(("localhost:9999" . "box") ("localhost:8888" . "other")))
TRAMP cannot reuse the tunnel, which forwards the Jupyter HTTP port, not an SSH server's.
Notebooks on another machine
emjupy-open-this-notebook opens an .ipynb that you reached through
TRAMP on its own machine. It makes a server there and a tunnel to it,
through any hops (see Using a notebook), which also
says what you see while the server starts. The rest of this page is for
a server that you started and tunnelled into.
A kernel on another machine needs no configuration. emjupy reaches it
over the Jupyter server's WebSocket. It reaches the language server the
same way, when jupyter-lsp runs beside the kernel (see
Language server support). The shadow file
stays in a temporary directory on this machine. emjupy sends its
contents over the socket, with paths rewritten to the kernel's working
directory. So completion, documentation and M-. into your own modules
describe the (remote) machine the code runs on.
| Kernel runs | jupyter - lsp = on the server | M-. into your modules |
|---|---|---|
| this machine | either | works |
| another machine | installed | works |
| another machine | not installed | no – the fallback server here cannot see them |
Without jupyter-lsp, over SSH
Without jupyter-lsp but with SSH access, you can use a language server
of your own: name the host. emjupy then puts the shadow file next to
the notebook on that host, over TRAMP, and eglot starts the language
server there.
(setq emjupy-language-support 'local emjupy-shadow-host "/ssh:box:")
emjupy-shadow-host is a host, not a path. The directory comes from
the kernel, so /srv/nb/analysis becomes /ssh:box:/srv/nb/analysis.
When the option is unset, the shadow file goes to a local temporary
directory. M-. then does not find your own modules, but nothing is
written to a path that means something else on this machine. The option
has no effect while the server's language server is in use, because the
shadow file is then always local. It applies only to the local route,
whether chosen or fallen back to. emjupy-shadow-directory overrides
where the file goes. emjupy-shadow-beside-notebook nil stops placing
it next to the notebook.
M-x emjupy-list-notebooks
Lists directories, notebooks, other files, then the server's kernels. Within each group, the most recently modified comes first, except Kernels have no modification time, and are sorted by name.
Opening files from the listing
RET on a file in the server dashboard opens it, by what emjupy can
address:
| Condition | Result |
|---|---|
emjupy-remote-root set |
the file, writable |
| the address names a real host | the file over TRAMP, writable |
| server runs on this machine | the file, writable |
| the server's root is known | read-only copy over HTTP |
| none of the above | an error saying what to set |
The HTTP copy uses the Contents API. It needs only that a kernel has reported where the server's files are (see below). The copy is read-only, and its header line names the original path.
Multiple notebooks & multiple servers
Each server owns its token and XSRF cookie. Each notebook owns its
kernel. Each kernel owns its WebSocket and its table of requests in
flight. So several notebooks from several servers can be open at once,
over several ssh tunnels on different local ports. Run emjupy-login
once per tunnel:
M-x emjupy-login RET 8888 RET ;; tunnel to machine A M-x emjupy-login RET 9999 RET ;; tunnel to machine B
The workflow it is built around:
- On the remote host, start a notebook kernel on port X.
- Locally,
ssh -L X:localhost:X user@remote. M-x emjupy-login RET X RET.
emjupy adopts the kernel running behind the port, so opening a
notebook puts you in the REPL you started. A port with no kernel gets a
fresh one; a port with several asks which. Each port keeps its own
kernel, so several remote sessions stay live. C-c M-z gives a
notebook another kernel. Logging in again to a known server updates its
token on the same object, so its notebooks stay valid.
C-c M-z marks kernels driving another open notebook [in
use]: two notebooks on one kernel share interpreter state, rarely what
is wanted.
Notebook buffers are named *emjupy: PATH [SERVER]*, because one path
can exist on two servers. The eglot shadow file is qualified the same
way, so the analysis.ipynb of two servers do not overwrite each other.
If a tunnel drops, the remote kernel keeps running. emjupy reconnects
by itself, every few seconds, for emjupy-reconnect-window seconds
(30). It reopens a tunnel that it opened itself, for a notebook opened
through TRAMP, on the same port.
A notebook opened again through a new tunnel is the same notebook, if it is on the same machine and comes from the server on the same port there. emjupy returns to its buffer, with its edits. The same path from a server on another port is a different notebook, and opens separately.
A cell that was running is not lost. The server keeps the kernel's messages and hands them to the same session when it reconnects, so the cell gets everything it printed. A cell that you run while emjupy reconnects runs once the connection is back. If a run ended while disconnected and its end was not kept, emjupy ends the cell too; its output may be incomplete. If a cell was sent just as the connection dropped, emjupy says that it may not have run, and names the key that runs it again.
If the connection does not return in time, emjupy ends the running
cells. emjupy-reconnect-kernel (C-c C-x C-c) then reconnects by
hand, and so does running a cell. Session state survives either way.
emjupy-reconnect-window nil disables reconnecting.
emjupy does not wait for a kernel that the server says it no longer has,
because it died or was shut down. If the server does not answer at all,
emjupy says that it may have stopped. emjupy also notices a kernel that
is shut down or culled while a cell runs on it and its connection stays
open: after emjupy-kernel-check-interval seconds (30) without a
message, it asks the server. nil never asks.
Access tokens
emjupy resolves the token without asking where it can. It uses an
explicit argument; otherwise the server's registered token; otherwise
none, which is common for tunnelled servers started with
--IdentityProvider.token. Only then does it prompt.
C-u M-x emjupy-login always prompts.
If the token is the problem
A tunnelled server still has a token. emjupy remembers it per server,
so you type it again only to check the server from the shell, with
curl for example. C-u M-x emjupy-status lists each open notebook
with its server.
Where the server's files are
Jupyter does not publish its root directory, deliberately. emjupy works it out from two facts. The kernel reports the directory it runs in, which is the notebook's own. The Contents API reports the notebook's path relative to the root. Take the second off the end of the first:
kernel cwd /home/you/project/analysis notebook path analysis/run.ipynb root /home/you/project
So emjupy-remote-root rarely needs setting: once a notebook's kernel
starts, browsing that server's files works. Set it when you do not want
the derived root, as for a local mirror, or a path through a TRAMP hop
that emjupy cannot infer. It then takes precedence. A kernel started
outside the notebook's folder does not match; emjupy then says it cannot
tell, rather than invent a path.
In the dashboard, d opens what the cursor is on. A directory opens in
Dired. A notebook or a file opens its directory, with point on it.
Recovering from a dropped connection
Suspending the machine kills a tunnelled WebSocket, and one dying
mid-render can leave the buffer unlike the cells.
M-x emjupy-reconnect-kernel (C-c C-x C-c) checks the buffer against
the cells and redraws if they differ: the cells are the notebook, the
buffer a drawing of them. M-x emjupy-re-render does the same by hand.
