From d7934bc7834a872514c32d606b3fe299c415be8b 2010-10-21 07:23:33 From: MinRK Date: 2010-10-21 07:23:33 Subject: [PATCH] color settings from ipythonqt propagate down to the ZMQInteractiveShell in the Kernel --- diff --git a/IPython/frontend/qt/console/ipython_widget.py b/IPython/frontend/qt/console/ipython_widget.py index 278d767..dd2e594 100644 --- a/IPython/frontend/qt/console/ipython_widget.py +++ b/IPython/frontend/qt/console/ipython_widget.py @@ -330,24 +330,27 @@ class IPythonWidget(FrontendWidget): # 'IPythonWidget' interface #--------------------------------------------------------------------------- - def set_default_style(self, colors='light'): + def set_default_style(self, colors='lightbg'): """ Sets the widget style to the class defaults. Parameters: ----------- - colors : str, optional (default light) + colors : str, optional (default lightbg) Whether to use the default IPython light background or dark background or B&W style. """ - if colors=='light': + colors = colors.lower() + if colors=='lightbg': self.style_sheet = default_light_style_sheet self.syntax_style = default_light_syntax_style - elif colors=='dark': + elif colors=='linux': self.style_sheet = default_dark_style_sheet self.syntax_style = default_dark_syntax_style - elif colors=='bw': + elif colors=='nocolor': self.style_sheet = default_bw_style_sheet self.syntax_style = default_bw_syntax_style + else: + raise KeyError("No such color scheme: %s"%colors) #--------------------------------------------------------------------------- # 'IPythonWidget' protected interface diff --git a/IPython/frontend/qt/console/ipythonqt.py b/IPython/frontend/qt/console/ipythonqt.py index 96174f7..100404b 100644 --- a/IPython/frontend/qt/console/ipythonqt.py +++ b/IPython/frontend/qt/console/ipythonqt.py @@ -155,11 +155,30 @@ def main(): wgroup.add_argument('--stylesheet', type=str, help="path to a custom CSS stylesheet.") wgroup.add_argument('--colors', type=str, - help="Set the color scheme (light, dark, or bw). This is guessed\ + help="Set the color scheme (LightBG,Linux,NoColor). This is guessed\ based on the pygments style if not set.") args = parser.parse_args() + # parse the colors arg down to current known labels + if args.colors: + colors=args.colors.lower() + if colors in ('lightbg', 'light'): + colors='lightbg' + elif colors in ('dark', 'linux'): + colors='linux' + else: + colors='nocolor' + elif args.style: + if args.style=='bw': + colors='nocolor' + elif styles.dark_style(args.style): + colors='linux' + else: + colors='lightbg' + else: + colors=None + # Don't let Qt or ZMQ swallow KeyboardInterupts. import signal signal.signal(signal.SIGINT, signal.SIG_DFL) @@ -172,13 +191,15 @@ def main(): if not args.existing: # if not args.ip in LOCAL_IPS+ALL_ALIAS: # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS) - + kwargs = dict(ip=args.ip) if args.pure: kwargs['ipython']=False - elif args.pylab: - kwargs['pylab']=args.pylab - + else: + kwargs['colors']=colors + if args.pylab: + kwargs['pylab']=args.pylab + kernel_manager.start_kernel(**kwargs) kernel_manager.start_channels() @@ -197,23 +218,9 @@ def main(): # configure the style: if not args.pure: # only IPythonWidget supports styles - # parse the colors arg down to current known labels - if args.colors: - colors=args.colors.lower() - if colors in ('lightbg', 'light'): - colors='light' - elif colors in ('dark', 'linux'): - colors='dark' - else: - colors='nocolor' - else: - colors=None - lightbg = colors != 'linux' - if args.style: - # guess whether it's a dark style: widget.syntax_style = args.style - widget.style_sheet = styles.sheet_from_template(args.style, lightbg) + widget.style_sheet = styles.sheet_from_template(args.style, colors) widget._syntax_style_changed() widget._style_sheet_changed() elif colors: diff --git a/IPython/frontend/qt/console/styles.py b/IPython/frontend/qt/console/styles.py index b9e9255..e92eaf0 100644 --- a/IPython/frontend/qt/console/styles.py +++ b/IPython/frontend/qt/console/styles.py @@ -106,12 +106,12 @@ def get_colors(stylename): fgcolor = fgcolor ) -def sheet_from_template(name, colors='light'): +def sheet_from_template(name, colors='lightbg'): """Use one of the base templates, and set bg/fg/select colors.""" colors = colors.lower() - if colors=='light': + if colors=='lightbg': return default_light_style_template%get_colors(name) - elif colors=='dark': + elif colors=='linux': return default_dark_style_template%get_colors(name) elif colors=='nocolor': return default_bw_style_sheet diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index f271305..772c798 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -535,7 +535,7 @@ class GTKKernel(Kernel): #----------------------------------------------------------------------------- def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, - independent=False, pylab=False): + independent=False, pylab=False, colors=None): """Launches a localhost kernel, binding to the specified ports. Parameters @@ -566,6 +566,9 @@ def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, string is passed, matplotlib will use the specified backend. Otherwise, matplotlib's default backend will be used. + colors : None or string, optional (default None) + If not None, specify the color scheme. One of (NoColor, LightBG, Linux) + Returns ------- A tuple of form: @@ -581,6 +584,9 @@ def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, extra_arguments.append('--ip') if isinstance(ip, basestring): extra_arguments.append(ip) + if colors is not None: + extra_arguments.append('--colors') + extra_arguments.append(colors) return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', xrep_port, pub_port, req_port, hb_port, independent, extra_arguments) @@ -595,6 +601,10 @@ def main(): "Pre-load matplotlib and numpy for interactive use. If GUI is not \ given, the GUI backend is matplotlib's, otherwise use one of: \ ['tk', 'gtk', 'qt', 'wx', 'inline'].") + parser.add_argument('--colors', + type=str, dest='colors', + help="Set the color scheme (NoColor, Linux, and LightBG).", + metavar='ZMQInteractiveShell.colors') namespace = parser.parse_args() kernel_class = Kernel @@ -616,6 +626,8 @@ given, the GUI backend is matplotlib's, otherwise use one of: \ if kernel_class is None: raise ValueError('GUI is not supported: %r' % gui) pylabtools.activate_matplotlib(backend) + if namespace.colors: + ZMQInteractiveShell.colors=namespace.colors kernel = make_kernel(namespace, kernel_class, OutStream)