%config magic #923

merged 10 commits into from Oct 29, 2011


None yet

3 participants

minrk commented Oct 24, 2011

should close #903

Adds %config magic, which allows %config Foo.bar = 5 configuration of IPython configurables.

The Magic class keeps a list of configurables which will be updated by the change, so any objects that should be accessible to this magic should be appended to shell.configurables. I started with everything I saw as configurable in InteractiveShell.


Use just %config to see what classes are available, and %config Class to get the trait info for that class.

When setting values via%config Class.trait = value It is evaluated with user_ns in globals, so you can do arbitrary things like:

In [4]: default = 'png'

In [5]: %config InlineBackendConfig.figure_format = raw_input('what figure format should we use? ') or default

Of course, this magic reveals just how much we don't use traits/config properly. Almost everything is attached to the InteractiveShell object, and has an effect exactly once during an init_foo() method, rather than allowing config propagation via _trait_changed() methods.

For instance, IPCompleter has an omit__names attribute, but the configurable is InteractiveShell.readline_omit__names, which is clearly wrong.

We've done a good job with config in new code, but I think existing code needs a pretty hefty pass to get configurables attached to the right objects, and getting logic like %colors into shell._colors_changed.

minrk commented Oct 25, 2011

Moving discussion from #903 to here (PR), pinging @fperez and @ellisonbg to keep conversation updates linked up.

I've renamed InlineBackendConfig -> InlineBackend (with warning on old name, for any of those still lingering on it).

Here's a quick question: Should we switch the inline figure default back to svg from png? PNG is the default, because it's a good deal sharper, and for figure snobs it does look better than SVG. However, it is less capable, in that the SVG+XHTML export will lack figures in the default state (see #735). Since it's easy to change the figure format, maybe we should change the default to the more capable one, and let the nicer-looking format be configurable?

A possible issue would be that the svg dpi is tuned such that svg and png figures are the same size in the QtConsole, but I find that they are not the same in a browser using the notebook, and the png size is more sensible. It is also true that png is just a much more portable and reliable (if not editable) format.

minrk commented Oct 25, 2011

I updated some docs, to cover using the %config magic to configure the inline backend, and moved the completer configurables to the completer class, where they belong.


@fperez will probably have thoughts on what image format should be default. I am fine moving to png and it does look nicer in many cases. Also, having it as the default will bring png bugs in matplotlib to light more readily.

There may be some browser tuning we can/should do to make the figure sizes of png/svg the same.

minrk commented Oct 25, 2011

@ellisonbg - I should clarify. The default is png, because it looks better. I'm wondering if we should change it to svg because some functionality (xhtml export) is unavailable with the default.

fperez commented Oct 25, 2011

On Tue, Oct 25, 2011 at 2:14 PM, Min RK

@ellisonbg - I should clarify.  The default is png, because it looks better.  I'm wondering if we should change it to svg because some functionality (xhtml export) is unavailable with the default.

We switched from svg to png a while back in deference to the
qtconsole, b/c the Qt svg renderer doesn't implement the full spec,
only a subset. Mpl figures do use features beyond this subset, such
as path clipping, and therefore the figures could have ugly artifacts
in the console.

Browsers tend to have much more robust svg support, so I could see
reconsidering this decision and simply having users configure their
qtconsole app separately if they so desire...


@minrk: oops, that was a brain typo, I do realize the default in png. I am fine moving to svg given the support in browsers is much better than the qt console.

fperez commented Oct 28, 2011

@minrk, note that I'm getting a failure in the test suite when I merge this onto master:

FAIL: Doctest: IPython.core.magic.Magic.magic_config
Traceback (most recent call last):
  File "/home/fperez/usr/lib/python2.6/site-packages/IPython/testing/plugin/ipdoctest.py", line 265, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for IPython.core.magic.Magic.magic_config
  File "/home/fperez/usr/lib/python2.6/site-packages/IPython/core/magic.py", line 3627, in magic_config

File "/home/fperez/usr/lib/python2.6/site-packages/IPython/core/magic.py", line 3657, in IPython.core.magic.Magic.magic_config
Failed example:
    get_ipython().magic(u"config IPCompleter")
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.6/doctest.py", line 1253, in __run
        compileflags, 1) in test.globs
      File "<doctest IPython.core.magic.Magic.magic_config[1]>", line 1, in <module>
        get_ipython().magic(u"config IPCompleter")
      File "/home/fperez/usr/lib/python2.6/site-packages/IPython/core/interactiveshell.py", line 1998, in magic
        result = fn(magic_args)
      File "/home/fperez/usr/lib/python2.6/site-packages/IPython/core/magic.py", line 3707, in magic_config
        help = re.sub(r'^\-\-', '', help, flags=re.MULTILINE)
    TypeError: sub() got an unexpected keyword argument 'flags'

>>  raise self.failureException(self.format_failure(<StringIO.StringIO instance at 0x4482cb0>.getvalue()))

Do you see it too? If not, we can try to debug it together on my box... Problem seen on python 2.6, ubuntu 10.10, 64 bit.

fperez commented Oct 28, 2011

ps - I think if you want to pass flags in a 2.6-compatible manner to a regexp, you must compile it and then use the .sub() method of the compiled regexp. In 2.6, the re module doesn't support flags in the methods. The signature for re.sub changed:


re.sub(pattern, repl, string[, count])


re.sub(pattern, repl, string[, count, flags])
minrk commented Oct 28, 2011

good catch, I didn't realize it had changed. I'll compile&call.

minrk commented Oct 28, 2011

regex is now compiled, should work on 2.6.

fperez commented Oct 28, 2011

Heads-up @minrk: this one popped a conflict after I just merged the read-only branch... Can you give it a quick rebase? I was about to work on it and saw the conflicts...

minrk added some commits Oct 24, 2011
@minrk minrk move useful update_config method to Configurable from Application
This method should be used whenever updating the config of an object.
It is useful for all configurables, not just Applications.
@minrk minrk allow using instance values in place of defaults in Configurable.clas…
@minrk minrk add %config magic for configuring IPython 63ad149
@minrk minrk filter %config class list to exclude classes with no configurable traits 8837523
@minrk minrk Rename InlineBackendConfig -> InlineBackend
include deprecation warning on old name
@minrk minrk DisplayFormatter.formatters should not be configurable
it is a dict of instances, which are not allowed in config
@minrk minrk add reference to `%config InlineBackend` in %pylab docstring
also cleaned up %config and %pylab docstrings to be a little more sphinx-autogen friendly.
@minrk minrk update qtconsole doc with discussion of InlineBackend config 859e6ae
@minrk minrk move completer configurables to IPCompleter where they belong
* InteractiveShell.readline_omit__names -> IPCompleter.omit__names
* InteractiveShell.readline_merge_completions -> IPCompleter.merge_completions

add test for IPCompleter.omit__names, which also covers IPCompleter
as a configurable.

update %config doctest to match, and replace Completer with IPCompleter
in TerminalIPythonApp.classes
@minrk minrk compile sub expr in %config for 2.6 compatibility 1cec181
minrk commented Oct 29, 2011

rebased - tiny conflict with PR #921.

fperez commented Oct 29, 2011

Looks good, ran lots of tests on 2.6 and 2.7, via screen and normal logins. After some initial weird glitching, that went away with a pyc cleanout, all looks good now so I think it's good to merge.

I'm going to leave the image default discussion to happen separately so we don't hold this PR forever, made #944 for that.

Merging now, thanks for the great work! This wil be very useful.

Final comment: adding mention of %config to the main configuration section in the docs is probably a good idea. Since that would be a docs-only patch, just go ahead and commit it any time if you get a chance (and if you can't do it now, let me know and I can take a crack at it later). But I'll merge the code now.

@fperez fperez merged commit b725362 into ipython:master Oct 29, 2011
@Carreau Carreau added a commit to Carreau/ipykernel that referenced this pull request Jul 6, 2016
@Carreau Carreau Don't use IPython.utils.warn (Deprecated).
And InlineBackaedConfig was renamed in  0.12 (2011):


So we can likely remove the warning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment