open_atmos_jupyter_utils.show_plot

 1# pylint: disable=missing-module-docstring
 2
 3import os
 4from matplotlib import pyplot
 5from ipywidgets import HBox
 6from IPython.display import display
 7from IPython.core.interactiveshell import InteractiveShell
 8from IPython.core.pylabtools import select_figure_formats
 9from open_atmos_jupyter_utils.temporary_file import TemporaryFile
10
11
12def show_plot(filename=None, fig=pyplot, inline_format='svg'):
13    """ the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter 
14    (use instead of *.show()) """
15    link = save_and_make_link(fig, filename)
16    select_figure_formats(InteractiveShell.instance(), {inline_format})
17    pyplot.show()
18    if 'QUARTO_EXECUTE_INFO' not in os.environ:
19        display(link)
20
21
22def save_and_make_link(fig, filename=None):
23    """ saves a figure as pdf and returns a Jupyter display()-able click-to-download widget """
24    temporary_files = [
25        TemporaryFile(suffix=suffix, filename=(
26            filename if filename is None else
27            ((filename if not filename.endswith('.pdf') else filename[:-4]) + suffix)
28        ))
29        for suffix in ('.pdf', '.svg')
30    ]
31    for temporary_file in temporary_files:
32        fig.savefig(temporary_file.absolute_path, bbox_inches='tight')
33    return HBox([temporary_file.make_link_widget() for temporary_file in temporary_files])
def show_plot( filename=None, fig=<module 'matplotlib.pyplot' from '/opt/hostedtoolcache/Python/3.11.18/x64/lib/python3.11/site-packages/matplotlib/pyplot.py'>, inline_format='svg'):
13def show_plot(filename=None, fig=pyplot, inline_format='svg'):
14    """ the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter 
15    (use instead of *.show()) """
16    link = save_and_make_link(fig, filename)
17    select_figure_formats(InteractiveShell.instance(), {inline_format})
18    pyplot.show()
19    if 'QUARTO_EXECUTE_INFO' not in os.environ:
20        display(link)

the missing click-to-save-as-pdf-or-svg button for matplotlib/Jupyter (use instead of *.show())