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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pylab fix #1052
Merged
Merged
Pylab fix #1052
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd84e0e
Fix critical bug with pylab support inadvertently introduced in #648.
fperez cfd87e9
Refactor gui/pylab integration to eliminate code duplication.
fperez 0e21764
Move zmq event loop support into a separate file.
fperez 5c4e9a0
Minor cleanups after a check with pyflakes of the refactored code.
fperez 37afbb3
Add missing file.
fperez cf5afdd
Only import zeromq pieces in networked shells.
fperez a2fcd13
Fix inline backend logic and avoid tests if mpl not available.
fperez 36e96e8
Avoid calling inline config if no shell - per @minrk feedback on #1052
fperez 3f11378
fix docstring
fperez File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,7 +232,8 @@ def activate_matplotlib(backend): | |
# For this, we wrap it into a decorator which adds a 'called' flag. | ||
pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive) | ||
|
||
def import_pylab(user_ns, backend, import_all=True, shell=None): | ||
|
||
def import_pylab(user_ns, import_all=True): | ||
"""Import the standard pylab symbols into user_ns.""" | ||
|
||
# Import numpy as np/pyplot as plt are conventions we're trying to | ||
|
@@ -246,48 +247,57 @@ def import_pylab(user_ns, backend, import_all=True, shell=None): | |
) | ||
exec s in user_ns | ||
|
||
if shell is not None: | ||
exec s in shell.user_ns_hidden | ||
# If using our svg payload backend, register the post-execution | ||
# function that will pick up the results for display. This can only be | ||
# done with access to the real shell object. | ||
# | ||
from IPython.zmq.pylab.backend_inline import InlineBackend | ||
|
||
cfg = InlineBackend.instance(config=shell.config) | ||
cfg.shell = shell | ||
if cfg not in shell.configurables: | ||
shell.configurables.append(cfg) | ||
|
||
if backend == backends['inline']: | ||
from IPython.zmq.pylab.backend_inline import flush_figures | ||
from matplotlib import pyplot | ||
shell.register_post_execute(flush_figures) | ||
# load inline_rc | ||
pyplot.rcParams.update(cfg.rc) | ||
|
||
# Add 'figsize' to pyplot and to the user's namespace | ||
user_ns['figsize'] = pyplot.figsize = figsize | ||
shell.user_ns_hidden['figsize'] = figsize | ||
|
||
# Setup the default figure format | ||
fmt = cfg.figure_format | ||
select_figure_format(shell, fmt) | ||
|
||
# The old pastefig function has been replaced by display | ||
from IPython.core.display import display | ||
# Add display and display_png to the user's namespace | ||
user_ns['display'] = display | ||
shell.user_ns_hidden['display'] = display | ||
user_ns['getfigs'] = getfigs | ||
shell.user_ns_hidden['getfigs'] = getfigs | ||
|
||
if import_all: | ||
s = ("from matplotlib.pylab import *\n" | ||
"from numpy import *\n") | ||
exec s in user_ns | ||
if shell is not None: | ||
exec s in shell.user_ns_hidden | ||
|
||
|
||
def configure_shell(shell, backend, user_ns=None): | ||
"""Configure an IPython shell object for matplotlib use. | ||
|
||
Parameters | ||
---------- | ||
shell : InteractiveShell instance | ||
If None, this function returns immediately. | ||
|
||
user_ns : dict | ||
A namespace where all configured variables will be placed. If not given, | ||
the `user_ns` attribute of the shell object is used. | ||
""" | ||
if shell is None: | ||
return | ||
|
||
user_ns = shell.user_ns if user_ns is None else user_ns | ||
|
||
# If using our svg payload backend, register the post-execution | ||
# function that will pick up the results for display. This can only be | ||
# done with access to the real shell object. | ||
from IPython.zmq.pylab.backend_inline import InlineBackend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will make |
||
|
||
cfg = InlineBackend.instance(config=shell.config) | ||
cfg.shell = shell | ||
if cfg not in shell.configurables: | ||
shell.configurables.append(cfg) | ||
|
||
if backend == backends['inline']: | ||
from IPython.zmq.pylab.backend_inline import flush_figures | ||
from matplotlib import pyplot | ||
shell.register_post_execute(flush_figures) | ||
# load inline_rc | ||
pyplot.rcParams.update(cfg.rc) | ||
# Add 'figsize' to pyplot and to the user's namespace | ||
user_ns['figsize'] = pyplot.figsize = figsize | ||
|
||
# Setup the default figure format | ||
fmt = cfg.figure_format | ||
select_figure_format(shell, fmt) | ||
|
||
# The old pastefig function has been replaced by display | ||
from IPython.core.display import display | ||
# Add display and getfigs to the user's namespace | ||
user_ns['display'] = display | ||
user_ns['getfigs'] = getfigs | ||
|
||
|
||
def pylab_activate(user_ns, gui=None, import_all=True, shell=None): | ||
|
@@ -313,8 +323,8 @@ def pylab_activate(user_ns, gui=None, import_all=True, shell=None): | |
""" | ||
gui, backend = find_gui_and_backend(gui) | ||
activate_matplotlib(backend) | ||
import_pylab(user_ns, backend, import_all, shell) | ||
|
||
import_pylab(user_ns, import_all) | ||
configure_shell(shell, backend, user_ns) | ||
print """ | ||
Welcome to pylab, a matplotlib-based Python environment [backend: %s]. | ||
For more information, type 'help(pylab)'.""" % backend | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just not call this function
if shell is None
- no need for it to conditionally do nothing if it was given inappropriate args.