Skip to content

Commit

Permalink
add save as svg button (in addition to save as pdf) + fix packaging i…
Browse files Browse the repository at this point in the history
…ssues (#21)
  • Loading branch information
slayoo authored Aug 17, 2024
1 parent 6ba8141 commit d7217d5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 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])
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tool.setuptools_scm]

# TODO
[build-system]
requires = ['setuptools==56.0.0', 'setuptools-scm==6.0.1']

[project.urls]
"Homepage" = "https://github.com/open-atmos/jupyter-utils"
"Source" = "https://github.com/open-atmos/jupyter-utils"
"Tracker" = "https://github.com/open-atmos/jupyter-utils/issues"
"Documentation" = "https://open-atmos.github.io/jupyter-utils"
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@ def get_long_description():
packages=find_packages(include=['open_atmos_jupyter_utils', 'open_atmos_jupyter_utils.*']),
long_description=get_long_description(),
long_description_content_type="text/markdown",
project_urls={
"Tracker": "https://github.com/open-atmos/jupyter-utils/issues",
"Documentation": "https://open-atmos.github.io/jupyter-utils",
"Source": "https://github.com/open-atmos/jupyter-utils"
}
url='https://github.com/open-atmos/jupyter-utils',
)

0 comments on commit d7217d5

Please sign in to comment.