Skip to content

Commit

Permalink
add plt.close
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Oct 27, 2024
1 parent c57c8ce commit 45fb305
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions magicclass/widgets/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def set_plt(self, rc_context: dict[str, Any]) -> None:
...

@contextmanager
def set_plt(self, style: str = None, rc_context: dict[str, Any] = {}):
def set_plt(self, style: str | None = None, rc_context: dict[str, Any] = {}):
"""A context manager for inline plot in the logger widget."""
try:
import matplotlib as mpl
Expand Down Expand Up @@ -657,7 +657,9 @@ def set_plt(self, style: str = None, rc_context: dict[str, Any] = {}):
backend = mpl.get_backend()
show._called = False
try:
mpl.use("module://magicclass.widgets.logger")
if backend != "module://magicclass.widgets.logger":
plt.close("all")
mpl.use("module://magicclass.widgets.logger")
with plt.style.context(style), plt.rc_context(rc_context):
yield self
finally:
Expand Down
14 changes: 14 additions & 0 deletions magicclass/widgets/tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from magicclass.widgets import Logger

def test_logger():
log = Logger()
log.print("Hello")
log.print_table({"a": [1, 2], "b": [3, 4]})
log.print_table([["a", "b"], [4, -1], [4.3, None]])
log.print_html("<b>bold</b>")
with log.set_stdout():
print("Hello")
with log.set_plt():
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
log.clear()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ testing = [
"numpy",
"pandas>=1.5.0",
"polars>=0.17.10",
"matplotlib>=3.8.2",
]
docs = [
"mkdocs",
Expand Down

0 comments on commit 45fb305

Please sign in to comment.