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