xonsh/xontrib/mpl.py
Noorhteen Raja NJ 38295a1dd9
Remove globals (#4280)
* refactor: remove usage of global variables in abbrevs.py

* chore: add flake8-mutable to prevent mutable defaults

* fix: abbrevs expand test

* refactor: add xonsh session singleton

* refactor: fix circular errors when using xonshSession as singleton

* refactor: remove black magicked builtin attributes

* style: black format tests as well

* refactor: update tests to use xonsh-session singleton

* refactor: update abbrevs to not use builtins

* test: remove DummyCommandsCache and patch orig class

* fix: failing test_command_completers

* test: use monkeypatch to update xession fixture

* fix: failing test_pipelines

* fix: failing test_main

* chore: run test suit as single invocation

* test: fix tests/test_xonsh.xsh

* refactor: remove builtins from docs/conf.py

* fix: mypy error in jobs

* fix: test error from test_main

* test: close xession error in test_command_completers

* chore: use pytest-cov for reporting coverage

this will include subprocess calls, and will increase coverage

* style:
2021-05-20 13:14:26 +03:00

65 lines
1.8 KiB
Python

"""Matplotlib xontribution. This xontrib should be loaded before matplotlib
is imported.
"""
from xonsh.tools import unthreadable
from xonsh.lazyasd import lazyobject
from xonsh.built_ins import XSH
__all__ = ()
@unthreadable
def mpl(args, stdin=None):
"""Hooks to matplotlib"""
from xontrib.mplhooks import show
show()
XSH.aliases["mpl"] = mpl
@lazyobject
def pylab_helpers():
try:
import matplotlib._pylab_helpers as m
except ImportError:
m = None
return m
@XSH.builtins.events.on_import_post_exec_module
def interactive_pyplot(module=None, **kwargs):
"""This puts pyplot in interactive mode once it is imported."""
if module.__name__ != "matplotlib.pyplot" or not XSH.env.get("XONSH_INTERACTIVE"):
return
# Since we are in interactive mode, let's monkey-patch plt.show
# to try to never block.
module.ion()
module._INSTALL_FIG_OBSERVER = False
plt_show = module.show
def xonsh_show(*args, **kwargs):
"""This is a monkey patched version of matplotlib.pyplot.show()
for xonsh's interactive mode. First it tries non-blocking mode
(block=False). If for some reason this fails, it will run show
in normal blocking mode (block=True).
"""
kwargs.update(block=False)
rtn = plt_show(*args, **kwargs)
figmanager = pylab_helpers.Gcf.get_active()
if figmanager is not None:
# unblocked mode failed, try blocking.
kwargs.update(block=True)
rtn = plt_show(*args, **kwargs)
return rtn
module.show = xonsh_show
# register figure drawer
@XSH.builtins.events.on_postcommand
def redraw_mpl_figure(**kwargs):
"""Redraws the current matplotlib figure after each command."""
pylab_helpers.Gcf.draw_all()