emacs-jupyter / jupyter

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

Feature request: render output (html) from wolfram kernel using `shr-render-buffer/region` #393

Open dalanicolai opened 2 years ago

dalanicolai commented 2 years ago

This feature request suggests/asks for a solution for #340.

When plotting using the wolfram kernel, I can copy the returned html to the scratch buffer and then do M-x shr-render-buffer/regionto render the image, so I guess it should be possible to somehow render the (html) result using shr directly. Using shr is really nice because it even correctly renders results containing multiple images directly. For example it renders correctly the output from the following source block

#+begin_src jupyter-Wolfram-Language :session wl
  {{0}, {1}, {0}, {-1}} // MatrixForm
  Plot[Sin[x],{x,0,Pi}]
#+end_src

Would it be possible to implement the option to use shr-render-buffer/region for rendering the output? Or should I address this request to org-mode directly (although it looks like ob-jupyter 'bypasses' much of the org-babel-insert-result functionality).

It would be amazing if shr could render over the html (via text-properties/overlay) so that the data is stored within the org file.

dalanicolai commented 2 years ago

So even better probably would be to use firefox (or chrome) with the headless --screenshot option, then add the produced image as a display property to (over) the original html.

Unfortunately, both the firefox and chrome screenshot options currently don't work here, so that I can not try it out.

dalanicolai commented 2 years ago

For people that are interested, for now I am using the following function to 'process' the results:

(defun org-babel-process-export-html-block ()
  (interactive)
  (let* ((element (org-element-context))
         (beg (org-element-property :begin element))
         (end (org-element-property :end element))
         (value (org-element-property :value element)))
    (with-temp-buffer
      (insert value)
      (shr-render-buffer (current-buffer)))
    (let ((html-contents (with-current-buffer "*html*"
                           (unless (text-properties-at (point))
                             (let ((contents (buffer-string)))
                               (kill-buffer)
                               contents)))))
      (when (print html-contents)
        (kill-region beg end)
        (goto-char beg)
        (insert "#+RESULTS\n: " html-contents "\n")))))

Call it with your cursor placed inside the results export-html block.