emacs-jupyter / jupyter

An interface to communicate with Jupyter kernels.
GNU General Public License v3.0
914 stars 89 forks source link

How do I know when `org-babel-execute-src-block` fails due to an exception? #498

Closed NightMachinery closed 2 months ago

NightMachinery commented 9 months ago

How do I know when org-babel-execute-src-block fails due to an exception?

Currently, it seems to always return nil.

My use case is that I want execution of subsequent cells to stop if there is an exception:

  (cl-defun night/h-org-babel-execute-with-move-fn
      (&key
       (move-fn (lambda () nil))
       (no-blocks-message "No source blocks found."))
    "Execute org-mode source blocks in a specified direction using MOVE-FN.
MOVE-FN should be a function that moves to the next or previous source block.
NO-BLOCKS-MESSAGE is a message string to display if no blocks are found in the specified direction."
    (cl-block 'blk
      (save-excursion
        ;; If not inside a source block, attempt to find the next or previous one.
        (when (not (org-in-src-block-p))
          (unless (funcall move-fn)
            (message no-blocks-message)
            (cl-return-from blk)))

        ;; Begin execution of source blocks.
        (while (org-in-src-block-p)
          (org-babel-execute-src-block)
          (unless (funcall move-fn)
            (cl-return-from blk)))
        (message "Execution of source blocks completed."))))

  (defun night/org-babel-execute-after ()
    "Execute the current org-mode source block and all following ones."
    (interactive)
    (night/h-org-babel-execute-with-move-fn
     :move-fn 'org-babel-next-src-block
     :no-blocks-message "No source blocks found ahead."))

  (defun night/org-babel-execute-before ()
    "Execute the current org-mode source block and all preceding ones."
    (interactive)
    (night/h-org-babel-execute-with-move-fn
     :move-fn 'org-babel-previous-src-block
     :no-blocks-message "No source blocks found before."))
NightMachinery commented 4 months ago

This issue is severely impacting my productivity. Can someone comment on whether it's possible at all or not? I can send a PR if the feature can be added without too much refactoring.

nnicandro commented 2 months ago

Let me know if 51799ec works for you.

NightMachinery commented 2 months ago

@nnicandro Thanks, that looks great. Can the new behavior be disabled by an option? Sometimes I need the old behavior. 😅 (I send a whole subtree and close the SSH connection with the remote server, leaving it to run in the background.)

nnicandro commented 2 months ago

You can switch to the new behavior by setting jupyter-org-queue-requests to a non-nil value and then switch back to the old behavior by setting it to nil (the default). I guess I should add a toggle command for this.