Skip to content

Commit d7217d5

Browse files
authored
add save as svg button (in addition to save as pdf) + fix packaging issues (#21)
1 parent 6ba8141 commit d7217d5

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![PyPI version](https://badge.fury.io/py/open-atmos-jupyter-utils.svg)](https://pypi.org/project/open-atmos-jupyter-utils)
66

77
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:
8-
- [``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)
8+
- [``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)
99
- [``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
1010
- [``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
1111

open_atmos_jupyter_utils/show_plot.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# pylint: disable=missing-module-docstring
22

33
from matplotlib import pyplot
4+
from ipywidgets import HBox
45
from IPython.display import display
56
from IPython.core.interactiveshell import InteractiveShell
67
from IPython.core.pylabtools import select_figure_formats
78
from open_atmos_jupyter_utils.temporary_file import TemporaryFile
89

910

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

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

pyproject.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[tool.setuptools_scm]
2+
3+
# TODO
4+
[build-system]
5+
requires = ['setuptools==56.0.0', 'setuptools-scm==6.0.1']
6+
7+
[project.urls]
8+
"Homepage" = "https://github.com/open-atmos/jupyter-utils"
9+
"Source" = "https://github.com/open-atmos/jupyter-utils"
10+
"Tracker" = "https://github.com/open-atmos/jupyter-utils/issues"
11+
"Documentation" = "https://open-atmos.github.io/jupyter-utils"

setup.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,5 @@ def get_long_description():
2626
packages=find_packages(include=['open_atmos_jupyter_utils', 'open_atmos_jupyter_utils.*']),
2727
long_description=get_long_description(),
2828
long_description_content_type="text/markdown",
29-
project_urls={
30-
"Tracker": "https://github.com/open-atmos/jupyter-utils/issues",
31-
"Documentation": "https://open-atmos.github.io/jupyter-utils",
32-
"Source": "https://github.com/open-atmos/jupyter-utils"
33-
}
29+
url='https://github.com/open-atmos/jupyter-utils',
3430
)

0 commit comments

Comments
 (0)