Skip to content

Commit

Permalink
add save as svg button (in addition to save as pdf)
Browse files Browse the repository at this point in the history
  • Loading branch information
slayoo committed Aug 17, 2024
1 parent 6ba8141 commit 824f363
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![PyPI version](https://badge.fury.io/py/open-atmos-jupyter-utils.svg)](https://pypi.org/project/open-atmos-jupyter-utils)

Utility routines used in Jupyter notebooks in [PySDM](https://github.com/open-atmos/PySDM), [PyMPDATA](https://github.com/open-atmos/PyMPDATA) and [PyPartMC](https://github.com/open-atmos/PyPartMC) projects:
- [``show_plot()``](https://open-atmos.github.io/jupyter-utils/show_plot.html) - a drop-in replacement for matplotlib's show() displaying the figure using vector graphics (svg) by default and offering a download-as-pdf widget just below (on Colab the widget triggers Google Drive download)
- [``show_plot()``](https://open-atmos.github.io/jupyter-utils/show_plot.html) - a drop-in replacement for matplotlib's show() displaying the figure inline using vector graphics (svg) by default and offering a download-as-pdf-or-svg widget just below (on Colab the widget triggers Google Drive download)
- [``TemporaryFile``](https://open-atmos.github.io/jupyter-utils/temporary_file.html) - a class equipped with ``make_link_widget()`` method returning a click-to-download Colab-compatible widget to be display()-ed in a Jupyter notebook
- [``pip_install_on_colab('package_a', 'package_b', ...)``](https://open-atmos.github.io/jupyter-utils/pip_install_on_colab.html) - a function handling execution of ``pip`` (and ``ldconfig``) on Colab

Expand Down
16 changes: 12 additions & 4 deletions open_atmos_jupyter_utils/show_plot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# pylint: disable=missing-module-docstring

from matplotlib import pyplot
from ipywidgets import HBox
from IPython.display import display
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.pylabtools import select_figure_formats
from open_atmos_jupyter_utils.temporary_file import TemporaryFile


def show_plot(filename=None, fig=pyplot, inline_format='svg'):
""" the missing click-to-save-as-pdf button for matplotlib/Jupyter (use instead of *.show()) """
""" the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter
(use instead of *.show()) """
link = save_and_make_link(fig, filename)
select_figure_formats(InteractiveShell.instance(), {inline_format})
pyplot.show()
Expand All @@ -17,6 +19,12 @@ def show_plot(filename=None, fig=pyplot, inline_format='svg'):

def save_and_make_link(fig, filename=None):
""" saves a figure as pdf and returns a Jupyter display()-able click-to-download widget """
temporary_file = TemporaryFile(suffix='.pdf', filename=filename)
fig.savefig(temporary_file.absolute_path, bbox_inches='tight')
return temporary_file.make_link_widget()
temporary_files = [
TemporaryFile(suffix=suffix, filename=(
filename if filename is None or not filename.endswith('.pdf') else filename[:-4]
) + suffix)
for suffix in ('.pdf', '.svg')
]
for temporary_file in temporary_files:
fig.savefig(temporary_file.absolute_path, bbox_inches='tight')
return HBox([temporary_file.make_link_widget() for temporary_file in temporary_files])

0 comments on commit 824f363

Please sign in to comment.