Skip to content

Commit

Permalink
move context manager into a common place
Browse files Browse the repository at this point in the history
  • Loading branch information
nvaytet committed Oct 13, 2022
1 parent d89d5a9 commit 168be20
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
20 changes: 1 addition & 19 deletions src/plopp/graphics/fig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,15 @@

from ..core.utils import number_to_variable, name_with_unit
from ..core import View
from .io import fig_to_bytes
from .utils import fig_to_bytes, silent_mpl_figure
from .mesh import Mesh
from .line import Line

from contextlib import contextmanager
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipp import DataArray, to_unit
from typing import Any, Tuple


@contextmanager
def silent_mpl_figure():
"""
Context manager to prevent automatic generation of figures in Jupyter.
"""
backend_ = mpl.get_backend()
revert = False
if 'inline' in backend_:
mpl.use("Agg")
revert = True
with plt.ioff():
yield
if revert:
mpl.use(backend_)


class Figure(View):

def __init__(self,
Expand Down
14 changes: 0 additions & 14 deletions src/plopp/graphics/io.py

This file was deleted.

5 changes: 3 additions & 2 deletions src/plopp/graphics/point_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..core.limits import find_limits, fix_empty_range
from ..core.utils import name_with_unit
from .color_mapper import ColorMapper
from .io import fig_to_bytes
from .utils import fig_to_bytes, silent_mpl_figure
from ..widgets import ToggleTool

import numpy as np
Expand Down Expand Up @@ -90,7 +90,8 @@ def _set_points_colors(self):
def _update_colorbar(self):
dpi = 96
height_inches = 0.89 * self._figsize[1] / dpi
cbar_fig = plt.figure(figsize=(height_inches * 0.2, height_inches), dpi=dpi)
with silent_mpl_figure():
cbar_fig = plt.figure(figsize=(height_inches * 0.2, height_inches), dpi=dpi)
cbar_ax = cbar_fig.add_axes([0.05, 0.02, 0.25, 1.0])
_ = ColorbarBase(cbar_ax,
cmap=self.color_mapper.cmap,
Expand Down
2 changes: 1 addition & 1 deletion src/plopp/graphics/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .fig import Figure
from .io import fig_to_bytes
from .utils import fig_to_bytes


class StaticFig(Figure):
Expand Down
32 changes: 32 additions & 0 deletions src/plopp/graphics/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from contextlib import contextmanager
from io import BytesIO
import matplotlib as mpl


def fig_to_bytes(fig, form='png'):
"""
Convert a Matplotlib figure to png (default) or svg bytes.
"""
buf = BytesIO()
fig.savefig(buf, format=form, bbox_inches='tight')
buf.seek(0)
return buf.getvalue()


@contextmanager
def silent_mpl_figure():
"""
Context manager to prevent automatic generation of figures in Jupyter.
"""
backend_ = mpl.get_backend()
revert = False
if 'inline' in backend_:
mpl.use("Agg")
revert = True
with mpl.pyplot.ioff():
yield
if revert:
mpl.use(backend_)

0 comments on commit 168be20

Please sign in to comment.