diff --git a/Readme.org b/Readme.org index 67ecbdc..a7e6ad7 100644 --- a/Readme.org +++ b/Readme.org @@ -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.]] diff --git a/ob-python-extras.el b/ob-python-extras.el index 150e6ac..d69cbc2 100644 --- a/ob-python-extras.el +++ b/ob-python-extras.el @@ -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\" @@ -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(), '', 'exec')) pymockbabel.display(outputs_and_file_paths, output_types, list_writer) @@ -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)))) diff --git a/python/pymockbabel.py b/python/pymockbabel.py index 495deb5..522f32f 100755 --- a/python/pymockbabel.py +++ b/python/pymockbabel.py @@ -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) @@ -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: @@ -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)