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