Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Clarify logic for repl displayhook.#22906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
timhoffm merged 1 commit intomatplotlib:mainfromanntzer:repldisplayhook3state
Apr 27, 2022
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletionslib/matplotlib/pyplot.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@
implicit and explicit interfaces.
"""

from enum import Enum
import functools
import importlib
import inspect
Expand DownExpand Up@@ -108,8 +109,15 @@ def _copy_docstring_and_deprecators(method, func=None):

## Global ##

_IP_REGISTERED = None
_INSTALL_FIG_OBSERVER = False

# The state controlled by {,un}install_repl_displayhook().
_ReplDisplayHook = Enum("_ReplDisplayHook", ["NONE", "PLAIN", "IPYTHON"])
_REPL_DISPLAYHOOK = _ReplDisplayHook.NONE


def _draw_all_if_interactive():
if matplotlib.is_interactive():
draw_all()


def install_repl_displayhook():
Expand All@@ -119,30 +127,25 @@ def install_repl_displayhook():

This works both with IPython and with vanilla python shells.
"""
global _IP_REGISTERED
global _INSTALL_FIG_OBSERVER
global _REPL_DISPLAYHOOK

if_IP_REGISTERED:
if_REPL_DISPLAYHOOK is _ReplDisplayHook.IPYTHON:
return

# See if we have IPython hooks around, if so use them.
# Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
# entries can also have been explicitly set to None.
mod_ipython = sys.modules.get("IPython")
if not mod_ipython:
_INSTALL_FIG_OBSERVER =True
_REPL_DISPLAYHOOK =_ReplDisplayHook.PLAIN
return
ip = mod_ipython.get_ipython()
if not ip:
_INSTALL_FIG_OBSERVER =True
_REPL_DISPLAYHOOK =_ReplDisplayHook.PLAIN
return

def post_execute():
if matplotlib.is_interactive():
draw_all()

ip.events.register("post_execute", post_execute)
_IP_REGISTERED = post_execute
_INSTALL_FIG_OBSERVER = False
ip.events.register("post_execute", _draw_all_if_interactive)
_REPL_DISPLAYHOOK = _ReplDisplayHook.IPYTHON

from IPython.core.pylabtools import backend2gui
# trigger IPython's eventloop integration, if available
Expand All@@ -161,16 +164,12 @@ def uninstall_repl_displayhook():
this will reset `sys.displayhook` to what ever function was there when
Matplotlib installed its displayhook, possibly discarding your changes.
"""
global _IP_REGISTERED
global _INSTALL_FIG_OBSERVER
if _IP_REGISTERED:
global _REPL_DISPLAYHOOK
if _REPL_DISPLAYHOOK is _ReplDisplayHook.IPYTHON:
from IPython import get_ipython
ip = get_ipython()
ip.events.unregister('post_execute', _IP_REGISTERED)
_IP_REGISTERED = None

if _INSTALL_FIG_OBSERVER:
_INSTALL_FIG_OBSERVER = False
ip.events.unregister("post_execute", _draw_all_if_interactive)
_REPL_DISPLAYHOOK = _ReplDisplayHook.NONE


draw_all = _pylab_helpers.Gcf.draw_all
Expand DownExpand Up@@ -810,7 +809,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
# FigureManager base class.
draw_if_interactive()

if_INSTALL_FIG_OBSERVER:
if_REPL_DISPLAYHOOK is _ReplDisplayHook.PLAIN:
fig.stale_callback = _auto_draw_if_interactive

if clear:
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp