Skip to content

Commit 64b166d

Browse files
committed
org: Launch a local notebook if one isn't available
When specifying a server session on the local host, like /jpy:localhost:id or /jpy:127.0.0.1#8885:id and it doesn't look like a notebook has been launched on the specified port, ask the user to launch one first before attempting to initiate a client.
1 parent e7e7690 commit 64b166d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

ob-jupyter.el

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,22 @@ session."
324324

325325
(cl-defmethod org-babel-jupyter-initiate-client ((session org-babel-jupyter-server-session) kernel)
326326
(let* ((rsession (org-babel-jupyter-session-name session))
327-
(url (jupyter-tramp-url-from-file-name rsession))
328-
(server (jupyter-server :url url)))
327+
(server (with-parsed-tramp-file-name rsession nil
328+
(when (member host '("127.0.0.1" "localhost"))
329+
(setq port (tramp-file-name-port-or-default v))
330+
(when (jupyter-port-available-p port)
331+
(if (y-or-n-p (format "Notebook not started on port %s. Launch one? "
332+
port))
333+
;; TODO: Specify authentication? But then
334+
;; how would you get the token for the
335+
;; login that happens in
336+
;; `jupyter-tramp-server-from-file-name'.
337+
(jupyter-launch-notebook port)
338+
(user-error "Launch a notebook on port %s first." port))))
339+
(jupyter-tramp-server-from-file-name rsession))))
329340
(unless (jupyter-server-has-kernelspec-p server kernel)
330-
(error "No kernelspec matching \"%s\" exists at %s" kernel url))
341+
(error "No kernelspec matching \"%s\" exists at %s"
342+
kernel (oref server url)))
331343
;; Language aliases may not exist for the kernels that are accessible on
332344
;; the server so ensure they do.
333345
(org-babel-jupyter-aliases-from-kernelspecs

0 commit comments

Comments
 (0)