Skip to content

Commit

Permalink
Merge pull request #1 from nilaykumar/feat/image-transparency
Browse files Browse the repository at this point in the history
Adding the option for matplotlib image transparency
  • Loading branch information
ElleNajt authored Jan 7, 2025
2 parents 0a72999 + b525b7c commit 6f361be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Readme.org
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ Then blocks will be sent automatically when a traceback is detected in the respo
(ob-python-extras-load-alerts))
#+end_src

** Matplotlib image transparency
Matplotlib is configured to save and display images without transparency by
default. The default can be changed with ~(setq
ob-python-extras/transparent-images t)~. This default, in turn, can be
overridden at the org-src-block level with =:transparent nil= or =:transparent
t=.

* Examples:
[[file:tests/babel-formatting.org][See this org file for examples of the different functionality and configurations.]]

Expand Down
17 changes: 11 additions & 6 deletions ob-python-extras.el
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ finally:
;;;; Better output handling
;;;;; mix printing images and text

(defun ob-python-extras/wrap-org-babel-execute-python-mock-plt (orig body &rest args)
(defun ob-python-extras/wrap-org-babel-execute-python-mock-plt (orig body params &rest args)
(let* ((exec-file (make-temp-file "execution-code"))
(pymockbabel-script-location (ob-python-extras/find-python-scripts-dir)))

(pymockbabel-script-location (ob-python-extras/find-python-scripts-dir))
(src-info (org-babel-get-src-block-info))
(headers (nth 2 src-info))
(transparent-header (assoc :transparent headers)))
(with-temp-file exec-file (insert body))
(let* ((body (format "\
exec_file = \"%s\"
Expand All @@ -216,7 +218,7 @@ import os
import sys
sys.path.append(pymockbabel_script_location)
import pymockbabel
outputs_and_file_paths, output_types, list_writer = pymockbabel.setup(\"%s\")
outputs_and_file_paths, output_types, list_writer = pymockbabel.setup(\"%s\"%s)
with open(exec_file, 'r') as file:
exec(compile(file.read(), '<org babel source block>', 'exec'))
pymockbabel.display(outputs_and_file_paths, output_types, list_writer)
Expand All @@ -226,8 +228,11 @@ except:
pass "
exec-file
pymockbabel-script-location
(file-name-sans-extension (file-name-nondirectory buffer-file-name)))))
(apply orig body args))))
(file-name-sans-extension (file-name-nondirectory buffer-file-name))
(concat ", transparent="
(if transparent-header (if (equal (cdr transparent-header) "nil") "False" "True")
(if (and (boundp 'ob-python-extras/transparent-images) ob-python-extras/transparent-images) "True" "False"))))))
(apply orig body params args))))



Expand Down
7 changes: 4 additions & 3 deletions python/pymockbabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def stop_capturing(list_writer):
sys.stderr = list_writer._stderr


def mock_show(outputs_and_file_paths, output_types, org_babel_file_name):
def mock_show(outputs_and_file_paths, output_types, org_babel_file_name, transparent):
directory = os.path.join("plots", org_babel_file_name)
os.makedirs(directory, exist_ok=True)

Expand All @@ -59,14 +59,14 @@ def mock_show(outputs_and_file_paths, output_types, org_babel_file_name):
directory, f"plot_{timestamp}_{random.randint(0, 10000000)}.png"
)

plt.savefig(file_path)
plt.savefig(file_path, transparent=transparent)
outputs_and_file_paths.append(file_path)
output_types.append("Image")
plt.close()
gc.collect()


def setup(org_babel_file_name):
def setup(org_babel_file_name, transparent):
outputs_and_file_paths = []
output_types = []
if MATPLOTLIB_AVAILABLE:
Expand All @@ -77,6 +77,7 @@ def setup(org_babel_file_name):
outputs_and_file_paths=outputs_and_file_paths,
output_types=output_types,
org_babel_file_name=org_babel_file_name,
transparent=transparent
),
).start()
list_writer = start_capturing(outputs_and_file_paths, output_types)
Expand Down

0 comments on commit 6f361be

Please sign in to comment.