Skip to content

Commit 9bb0415

Browse files
committed
Handle :file parameter in a cleaner way
Temporarily remove the :file parameter from the src-block parameters so that `org-babel` does insert it as the result of the src-block.
1 parent 70f0429 commit 9bb0415

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

ob-jupyter.el

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,30 @@ Do this only if the file exists in
234234
(file-exists-p link-path))
235235
(delete-file link-path))))))))
236236

237+
;; TODO: A cleaner way to do this. Maybe just removing the file parameter from
238+
;; the arguments all together and adding a new file slot in
239+
;; `jupyter-org-request'. The real issue is that we have to deal with the fact
240+
;; the `org-babel' assumes how the results of a code block are formatted is
241+
;; controlled by the header arguments, but Jupyter controls the way that
242+
;; results are displayed through the contents of the messages passed to Emacs
243+
;; during the evaluation of a code block.
244+
(defun org-babel-jupyter-temporarily-clear-file-param (params)
245+
"Destructively remove the file result parameter from PARAMS.
246+
Re-add the file parameters on the next call to
247+
`org-babel-after-execute-hook'."
248+
(let* ((result-params (assq :result-params params))
249+
(fresult (member "file" result-params))
250+
(fparam (assq :file params)))
251+
(setcar fresult "scalar")
252+
(delq fparam params)
253+
(cl-labels
254+
((reset
255+
()
256+
(setcar fresult "file")
257+
(when fparam (nconc params (list fparam)))
258+
(remove-hook 'org-babel-after-execute-hook #'reset t)))
259+
(add-hook 'org-babel-after-execute-hook #'reset nil t))))
260+
237261
(defun org-babel-execute:jupyter (body params)
238262
"Execute BODY according to PARAMS.
239263
BODY is the code to execute for the current Jupyter `:session' in
@@ -254,6 +278,8 @@ the PARAMS alist."
254278
:code code))))
255279
(when (member "replace" (assq :result-params params))
256280
(org-babel-jupyter-cleanup-file-links))
281+
(when (member "file" (assq :result-params params))
282+
(org-babel-jupyter-temporarily-clear-file-param params))
257283
(cond
258284
((or (equal (alist-get :async params) "yes")
259285
(plist-member params :async))

0 commit comments

Comments
 (0)