##// END OF EJS Templates
added default behavior, to allow for unhandled backends, such as 'MacOSX'
Benjamin Ragan-Kelley -
Show More
@@ -1,147 +1,147 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Pylab (matplotlib) support utilities.
2 """Pylab (matplotlib) support utilities.
3
3
4 Authors
4 Authors
5 -------
5 -------
6 Fernando Perez.
6 Fernando Perez.
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2009 The IPython Development Team
10 # Copyright (C) 2009 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from IPython.utils.decorators import flag_calls
20 from IPython.utils.decorators import flag_calls
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Main classes and functions
23 # Main classes and functions
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 def pylab_activate(user_ns, gui=None, import_all=True):
26 def pylab_activate(user_ns, gui=None, import_all=True):
27 """Activate pylab mode in the user's namespace.
27 """Activate pylab mode in the user's namespace.
28
28
29 Loads and initializes numpy, matplotlib and friends for interactive use.
29 Loads and initializes numpy, matplotlib and friends for interactive use.
30
30
31 Parameters
31 Parameters
32 ----------
32 ----------
33 user_ns : dict
33 user_ns : dict
34 Namespace where the imports will occur.
34 Namespace where the imports will occur.
35
35
36 gui : optional, string
36 gui : optional, string
37 A valid gui name following the conventions of the %gui magic.
37 A valid gui name following the conventions of the %gui magic.
38
38
39 import_all : optional, boolean
39 import_all : optional, boolean
40 If true, an 'import *' is done from numpy and pylab.
40 If true, an 'import *' is done from numpy and pylab.
41
41
42 Returns
42 Returns
43 -------
43 -------
44 The actual gui used (if not given as input, it was obtained from matplotlib
44 The actual gui used (if not given as input, it was obtained from matplotlib
45 itself, and will be needed next to configure IPython's gui integration.
45 itself, and will be needed next to configure IPython's gui integration.
46 """
46 """
47
47
48 # Initialize matplotlib to interactive mode always
48 # Initialize matplotlib to interactive mode always
49 import matplotlib
49 import matplotlib
50
50
51 # If user specifies a GUI, that dictates the backend, otherwise we read the
51 # If user specifies a GUI, that dictates the backend, otherwise we read the
52 # user's mpl default from the mpl rc structure
52 # user's mpl default from the mpl rc structure
53 g2b = {'tk': 'TkAgg',
53 g2b = {'tk': 'TkAgg',
54 'gtk': 'GTKAgg',
54 'gtk': 'GTKAgg',
55 'wx': 'WXAgg',
55 'wx': 'WXAgg',
56 'qt': 'Qt4Agg', # qt3 not supported
56 'qt': 'Qt4Agg', # qt3 not supported
57 'qt4': 'Qt4Agg' }
57 'qt4': 'Qt4Agg' }
58
58
59 if gui:
59 if gui:
60 # select backend based on requested gui
60 # select backend based on requested gui
61 backend = g2b[gui]
61 backend = g2b[gui]
62 else:
62 else:
63 backend = matplotlib.rcParams['backend']
63 backend = matplotlib.rcParams['backend']
64 # In this case, we need to find what the appropriate gui selection call
64 # In this case, we need to find what the appropriate gui selection call
65 # should be for IPython, so we can activate inputhook accordingly
65 # should be for IPython, so we can activate inputhook accordingly
66 b2g = dict(zip(g2b.values(),g2b.keys()))
66 b2g = dict(zip(g2b.values(),g2b.keys()))
67 gui = b2g[backend]
67 gui = b2g.get(backend, None)
68
68
69 # We must set the desired backend before importing pylab
69 # We must set the desired backend before importing pylab
70 matplotlib.use(backend)
70 matplotlib.use(backend)
71
71
72 # This must be imported last in the matplotlib series, after
72 # This must be imported last in the matplotlib series, after
73 # backend/interactivity choices have been made
73 # backend/interactivity choices have been made
74 import matplotlib.pylab as pylab
74 import matplotlib.pylab as pylab
75
75
76 # XXX For now leave this commented out, but depending on discussions with
76 # XXX For now leave this commented out, but depending on discussions with
77 # mpl-dev, we may be able to allow interactive switching...
77 # mpl-dev, we may be able to allow interactive switching...
78 #import matplotlib.pyplot
78 #import matplotlib.pyplot
79 #matplotlib.pyplot.switch_backend(backend)
79 #matplotlib.pyplot.switch_backend(backend)
80
80
81 pylab.show._needmain = False
81 pylab.show._needmain = False
82 # We need to detect at runtime whether show() is called by the user.
82 # We need to detect at runtime whether show() is called by the user.
83 # For this, we wrap it into a decorator which adds a 'called' flag.
83 # For this, we wrap it into a decorator which adds a 'called' flag.
84 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
84 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
85
85
86 # Import numpy as np/pyplot as plt are conventions we're trying to
86 # Import numpy as np/pyplot as plt are conventions we're trying to
87 # somewhat standardize on. Making them available to users by default
87 # somewhat standardize on. Making them available to users by default
88 # will greatly help this.
88 # will greatly help this.
89 exec ("import numpy\n"
89 exec ("import numpy\n"
90 "import matplotlib\n"
90 "import matplotlib\n"
91 "from matplotlib import pylab, mlab, pyplot\n"
91 "from matplotlib import pylab, mlab, pyplot\n"
92 "np = numpy\n"
92 "np = numpy\n"
93 "plt = pyplot\n"
93 "plt = pyplot\n"
94 ) in user_ns
94 ) in user_ns
95
95
96 if import_all:
96 if import_all:
97 exec("from matplotlib.pylab import *\n"
97 exec("from matplotlib.pylab import *\n"
98 "from numpy import *\n") in user_ns
98 "from numpy import *\n") in user_ns
99
99
100 matplotlib.interactive(True)
100 matplotlib.interactive(True)
101
101
102 print """
102 print """
103 Welcome to pylab, a matplotlib-based Python environment [backend: %s].
103 Welcome to pylab, a matplotlib-based Python environment [backend: %s].
104 For more information, type 'help(pylab)'.""" % backend
104 For more information, type 'help(pylab)'.""" % backend
105
105
106 return gui
106 return gui
107
107
108 # We need a little factory function here to create the closure where
108 # We need a little factory function here to create the closure where
109 # safe_execfile can live.
109 # safe_execfile can live.
110 def mpl_runner(safe_execfile):
110 def mpl_runner(safe_execfile):
111 """Factory to return a matplotlib-enabled runner for %run.
111 """Factory to return a matplotlib-enabled runner for %run.
112
112
113 Parameters
113 Parameters
114 ----------
114 ----------
115 safe_execfile : function
115 safe_execfile : function
116 This must be a function with the same interface as the
116 This must be a function with the same interface as the
117 :meth:`safe_execfile` method of IPython.
117 :meth:`safe_execfile` method of IPython.
118
118
119 Returns
119 Returns
120 -------
120 -------
121 A function suitable for use as the ``runner`` argument of the %run magic
121 A function suitable for use as the ``runner`` argument of the %run magic
122 function.
122 function.
123 """
123 """
124
124
125 def mpl_execfile(fname,*where,**kw):
125 def mpl_execfile(fname,*where,**kw):
126 """matplotlib-aware wrapper around safe_execfile.
126 """matplotlib-aware wrapper around safe_execfile.
127
127
128 Its interface is identical to that of the :func:`execfile` builtin.
128 Its interface is identical to that of the :func:`execfile` builtin.
129
129
130 This is ultimately a call to execfile(), but wrapped in safeties to
130 This is ultimately a call to execfile(), but wrapped in safeties to
131 properly handle interactive rendering."""
131 properly handle interactive rendering."""
132
132
133 import matplotlib
133 import matplotlib
134 import matplotlib.pylab as pylab
134 import matplotlib.pylab as pylab
135
135
136 #print '*** Matplotlib runner ***' # dbg
136 #print '*** Matplotlib runner ***' # dbg
137 # turn off rendering until end of script
137 # turn off rendering until end of script
138 is_interactive = matplotlib.rcParams['interactive']
138 is_interactive = matplotlib.rcParams['interactive']
139 matplotlib.interactive(False)
139 matplotlib.interactive(False)
140 safe_execfile(fname,*where,**kw)
140 safe_execfile(fname,*where,**kw)
141 matplotlib.interactive(is_interactive)
141 matplotlib.interactive(is_interactive)
142 # make rendering call now, if the user tried to do it
142 # make rendering call now, if the user tried to do it
143 if pylab.draw_if_interactive.called:
143 if pylab.draw_if_interactive.called:
144 pylab.draw()
144 pylab.draw()
145 pylab.draw_if_interactive.called = False
145 pylab.draw_if_interactive.called = False
146
146
147 return mpl_execfile
147 return mpl_execfile
General Comments 0
You need to be logged in to leave comments. Login now