Skip to content

Commit e237ff0

Browse files
committed
Allow disabling automatically connecting to src-block sessions
* jupyter-org-client.el (jupyter-org-auto-connect): New customizable variable determining whether or not to automatically connect to a session. (jupyter-org-with-src-block-client): Evaluate body only when automatically connecting or the session is already initiated. * ob-jupyter.el (org-babel-jupyter-session-initiated-p): New function. (org-babel-edit-prep:jupyter): Take into account `jupyter-org-auto-connect`. closes #432
1 parent cf91d06 commit e237ff0

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

jupyter-org-client.el

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
(declare-function org-babel-python-table-or-string "ob-python" (results))
3535
(declare-function org-babel-jupyter-initiate-session "ob-jupyter" (&optional session params))
3636
(declare-function org-babel-jupyter-src-block-session "ob-jupyter" ())
37+
(declare-function org-babel-jupyter-session-initiated-p "ob-jupyter" (params))
3738
(declare-function org-babel-jupyter-language-p "ob-jupyter" (lang))
3839
(declare-function org-element-context "org-element" (&optional element))
3940
(declare-function org-element-create "org-element" (type &optional props &rest children))
@@ -55,6 +56,19 @@
5556
(declare-function org-inside-LaTeX-fragment-p "org" ())
5657
(declare-function org-toggle-latex-fragment "org" (&optional arg))
5758

59+
(defcustom jupyter-org-auto-connect t
60+
"Automatically establish a connection to a src-block session.
61+
If this variable is non-nil, then a connection to a src-block
62+
session is automatically established under certain conditions,
63+
e.g. during auto-completion. Otherwise there would have to be an
64+
available connection already if this variable is nil for features
65+
like auto-completion to work.
66+
67+
When this variable is nil, you can establish a connection to a
68+
session by, for example, executing a src-block."
69+
:group 'ob-jupyter
70+
:type 'boolean)
71+
5872
(defcustom jupyter-org-resource-directory "./.ob-jupyter/"
5973
"Directory used to store automatically generated image files.
6074
See `jupyter-org-image-file-name'."
@@ -417,7 +431,8 @@ returned."
417431

418432
(defmacro jupyter-org-with-src-block-client (&rest body)
419433
"Evaluate BODY with `jupyter-current-client' set to the session's client.
420-
A client is initialized if necessary.
434+
A client is initialized if needed when `jupyter-org-auto-connect'
435+
is non-nil.
421436
422437
If `point' is not inside the code of a Jupyter source block, BODY
423438
is not evaluated and nil is returned. Return the result of BODY
@@ -427,17 +442,21 @@ In addition to evaluating BODY with an active Jupyter client set,
427442
the `syntax-table' will be set to that of the REPL buffers."
428443
(declare (debug (body)))
429444
(let ((params (make-symbol "params"))
430-
(syntax (make-symbol "syntax")))
445+
(key (make-symbol "key"))
446+
(syntax (make-symbol "syntax"))
447+
(buffer (make-symbol "buffer")))
431448
`(jupyter-org-when-in-src-block
432-
(let* ((,params (car jupyter-org--src-block-cache))
433-
(jupyter-current-client
434-
(buffer-local-value 'jupyter-current-client
435-
(org-babel-jupyter-initiate-session
436-
(alist-get :session ,params) ,params)))
437-
(,syntax (jupyter-kernel-language-syntax-table
438-
jupyter-current-client)))
439-
(with-syntax-table ,syntax
440-
,@body)))))
449+
(let ((,params (car jupyter-org--src-block-cache)))
450+
(when (or jupyter-org-auto-connect
451+
(org-babel-jupyter-session-initiated-p ,params))
452+
(let* ((,buffer (org-babel-jupyter-initiate-session
453+
(alist-get :session ,params) ,params))
454+
(jupyter-current-client
455+
(buffer-local-value 'jupyter-current-client ,buffer))
456+
(,syntax (jupyter-kernel-language-syntax-table
457+
jupyter-current-client)))
458+
(with-syntax-table ,syntax
459+
,@body)))))))
441460

442461
(cl-defmethod jupyter-code-context ((_type (eql inspect))
443462
&context (major-mode org-mode))

ob-jupyter.el

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,20 @@ associated with the session found in INFO.
199199
200200
If the session is a Jupyter TRAMP file name, the
201201
`default-directory' of the edit buffer is set to the root
202-
directory the notebook serves."
202+
directory the notebook serves.
203+
204+
If `jupyter-org-auto-connect' is nil, this function does nothing
205+
if the session has not been initiated yet."
203206
(let* ((params (nth 2 info))
204207
(session (alist-get :session params))
205-
(client-buffer (org-babel-jupyter-initiate-session session params)))
206-
(jupyter-repl-associate-buffer client-buffer)
207-
(when (jupyter-tramp-file-name-p session)
208-
(setq default-directory (concat (file-remote-p session) "/")))))
208+
(client-buffer
209+
(when (or jupyter-org-auto-connect
210+
(org-babel-jupyter-session-initiated-p params))
211+
(org-babel-jupyter-initiate-session session params))))
212+
(when client-buffer
213+
(jupyter-repl-associate-buffer client-buffer)
214+
(when (jupyter-tramp-file-name-p session)
215+
(setq default-directory (concat (file-remote-p session) "/"))))))
209216

210217
(defun org-babel-jupyter--insert-variable-assignments (params)
211218
"Insert variable assignment lines from PARAMS into the `current-buffer'.
@@ -368,6 +375,11 @@ session."
368375
;; TODO: Would we always want to do this?
369376
(jupyter-server-name-client-kernel client sname)))))))
370377

378+
(defun org-babel-jupyter-session-initiated-p (params)
379+
"Return non-nil if the session corresponding to PARAMS is initiated."
380+
(let ((key (org-babel-jupyter-session-key params)))
381+
(gethash key org-babel-jupyter-session-clients)))
382+
371383
(defun org-babel-jupyter-initiate-session-by-key (session params)
372384
"Return the Jupyter REPL buffer for SESSION.
373385
If SESSION does not have a client already, one is created based

0 commit comments

Comments
 (0)