From e8c5062700bb053a1bbc8f5801017e32229ce851 2010-01-06 00:39:22 From: Fernando Perez Date: 2010-01-06 00:39:22 Subject: [PATCH] Added --gui to match %gui use, better docs and behavior for %pylab code. Now both %gui and %pylab have identical syntax if used at startup as --gui and --pylab. This can be useful to people setting up aliases or other shell-based support. Also cleaned up %pylab so it doesn't pollute the output of %who with all loaded symbols. --- diff --git a/IPython/core/ipapp.py b/IPython/core/ipapp.py index 960ba3b..aa5fa90 100755 --- a/IPython/core/ipapp.py +++ b/IPython/core/ipapp.py @@ -244,22 +244,39 @@ cl_args = ( action='store_true', dest='Global.force_interact', default=NoConfigDefault, help="If running code from the command line, become interactive afterwards.") ), + + # Options to start with GUI control enabled from the beginning + (('--gui',), dict( + type=str, dest='Global.gui', default=NoConfigDefault, + help="Enable GUI event loop integration ('qt', 'wx', 'gtk').", + metavar='gui-mode') + ), + + (('--pylab',), dict( + type=str, dest='Global.pylab', default=NoConfigDefault, + nargs='?', const='auto', metavar='gui-mode', + help="Pre-load matplotlib and numpy for interactive use. "+ + "If no value is given, the gui backend is matplotlib's, else use "+ + "one of: ['tk', 'qt', 'wx', 'gtk'].") + ), + + # Legacy GUI options. Leave them in for backwards compatibility, but the + # 'thread' names are really a misnomer now. (('--wthread','-wthread'), dict( action='store_true', dest='Global.wthread', default=NoConfigDefault, - help="Enable wxPython event loop integration.") + help="Enable wxPython event loop integration "+ + "(DEPRECATED, use --gui wx)") ), (('--q4thread','--qthread','-q4thread','-qthread'), dict( action='store_true', dest='Global.q4thread', default=NoConfigDefault, - help="Enable Qt4 event loop integration. Qt3 is no longer supported.") + help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+ + "(DEPRECATED, use --gui qt)") ), (('--gthread','-gthread'), dict( action='store_true', dest='Global.gthread', default=NoConfigDefault, - help="Enable GTK event loop integration.") + help="Enable GTK event loop integration. "+ + "(DEPRECATED, use --gui gtk)") ), - (('--pylab',), dict( - action='store_true', dest='Global.pylab', default=NoConfigDefault, - help="Pre-load matplotlib and numpy for interactive use.") - ) ) @@ -298,6 +315,7 @@ class IPythonApp(Application): super(IPythonApp, self).create_default_config() # Eliminate multiple lookups Global = self.default_config.Global + # Set all default values Global.display_banner = True @@ -312,13 +330,18 @@ class IPythonApp(Application): Global.interact = True # No GUI integration by default - Global.wthread = False - Global.q4thread = False - Global.gthread = False - + Global.gui = False # Pylab off by default Global.pylab = False + # Deprecated versions of gui support that used threading, we support + # them just for bacwards compatibility as an alternate spelling for + # '--gui X' + Global.qthread = False + Global.q4thread = False + Global.wthread = False + Global.gthread = False + def create_command_line_config(self): """Create and return a command line config loader.""" return IPythonAppCLConfigLoader( @@ -415,32 +438,41 @@ class IPythonApp(Application): Global = self.master_config.Global # Select which gui to use - if Global.wthread: + if Global.gui: + gui = Global.gui + # The following are deprecated, but there's likely to be a lot of use + # of this form out there, so we might as well support it for now. But + # the --gui option above takes precedence. + elif Global.wthread: gui = inputhook.GUI_WX - elif Global.q4thread: + elif Global.qthread: gui = inputhook.GUI_QT elif Global.gthread: gui = inputhook.GUI_GTK else: gui = None + # Using --pylab will also require gui activation, though which toolkit + # to use may be chosen automatically based on mpl configuration. if Global.pylab: activate = self.shell.enable_pylab + if Global.pylab == 'auto': + gui = None + else: + gui = Global.pylab else: # Enable only GUI integration, no pylab activate = inputhook.enable_gui if gui or Global.pylab: try: - m = "Enabling GUI event loop integration, toolkit=%s, pylab=%s"\ - % (gui, Global.pylab) - self.log.info(m) + self.log.info("Enabling GUI event loop integration, " + "toolkit=%s, pylab=%s" % (gui, Global.pylab) ) activate(gui) except: self.log.warn("Error in enabling GUI event loop integration:") self.shell.showtraceback() - def _load_extensions(self): """Load all IPython extensions in Global.extensions. diff --git a/IPython/core/iplib.py b/IPython/core/iplib.py index 82144d8..f5f8e66 100644 --- a/IPython/core/iplib.py +++ b/IPython/core/iplib.py @@ -827,7 +827,7 @@ class InteractiveShell(Component, Magic): # An auxiliary namespace that checks what parts of the user_ns were # loaded at startup, so we can list later only variables defined in # actual interactive use. Since it is always a subset of user_ns, it - # doesn't need to be seaparately tracked in the ns_table + # doesn't need to be separately tracked in the ns_table. self.user_config_ns = {} # A namespace to keep track of internal data structures to prevent @@ -2451,13 +2451,36 @@ class InteractiveShell(Component, Magic): #------------------------------------------------------------------------- def enable_pylab(self, gui=None): + """Activate pylab support at runtime. + + This turns on support for matplotlib, preloads into the interactive + namespace all of numpy and pylab, and configures IPython to correcdtly + interact with the GUI event loop. The GUI backend to be used can be + optionally selected with the optional :param:`gui` argument. + + Parameters + ---------- + gui : optional, string + + If given, dictates the choice of matplotlib GUI backend to use + (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or + 'gtk'), otherwise we use the default chosen by matplotlib (as + dictated by the matplotlib build-time options plus the user's + matplotlibrc configuration file). """ - """ - gui = pylab_activate(self.user_ns, gui) + # We want to prevent the loading of pylab to pollute the user's + # namespace as shown by the %who* magics, so we execute the activation + # code in an empty namespace, and we update *both* user_ns and + # user_config_ns with this information. + ns = {} + gui = pylab_activate(ns, gui) + self.user_ns.update(ns) + self.user_config_ns.update(ns) + # Now we must activate the gui pylab wants to use, and fix %run to take + # plot updates into account enable_gui(gui) self.magic_run = self._pylab_magic_run - #------------------------------------------------------------------------- # Things related to IPython exiting #------------------------------------------------------------------------- diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py index 64f7f8a..be57a3a 100755 --- a/IPython/core/prefilter.py +++ b/IPython/core/prefilter.py @@ -845,16 +845,11 @@ class AutoHandler(PrefilterHandler): pre = line_info.pre continue_prompt = line_info.continue_prompt obj = line_info.ofind(self)['obj'] - #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg # This should only be active for single-line input! if continue_prompt: - # XXX - Ugly hack! We are breaking on multiline input and I'm out - # of time tonight to disentangle the component hirerarchy issue - # here... Fix this more cleanly later. self.shell.log(line,line,continue_prompt) - return line force_auto = isinstance(obj, IPyAutocall)