Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython.embed() does not use terminal colors

Tags:

python

ipython

I will frequently use IPython.embed() to examine the state of running code. In earlier versions of IPython, it correctly determines color capability of my terminal (xterm), and uses colored text. In the latest version of IPython (7.2.0), only black and white text are shown when using IPython.embed().

Based on the documentation, I should be able to override this default in the default profile by setting c.InteractiveShell.colors = 'Linux'. However, this setting only applies to standalone instances of IPython, and not embedded sessions.

I can correct this for each instance by using the %colors Linux magic method. However, this should work automatically without needing additional configuration after each embed.

If I start a standalone instance of IPython by running ipython3 directly, then the terminal colors are correctly set. However, this is not an option for my most common workflow.

This was tested using python 3.5.2, running on Linux Mint 19 (based on Ubuntu 18.04). The first ipython version on PyPI where this occurred is version 7.0.0. The previous version available, 6.5.0, correctly uses terminal colors with IPython.embed(). For now, I have reverted to the last working version of 6.5.0, but I would like to keep up-to-date on the most recent version.

like image 455
Eldritch Cheese Avatar asked Dec 26 '18 14:12

Eldritch Cheese


2 Answers

The easiest answer would be IPython.embed(colors="neutral")

like image 150
Omar Elrefaei Avatar answered Nov 08 '22 07:11

Omar Elrefaei


From inside the embedded shell, you can type config to get a list of the available config categories:

In [1]: config
Available objects for config:
     AliasManager
     DisplayFormatter
     HistoryManager
     IPCompleter
     InteractiveShellEmbed
     LoggingMagics
     MagicsManager
     OSMagics
     PrefilterManager
     ScriptMagics

You can see that InteractiveShell is not available, but InteractiveShellEmbed is.

The following snippet should set the colors correctly:

from IPython import embed
from traitlets.config import get_config
c = get_config()
c.InteractiveShellEmbed.colors = "Linux"
embed(config=c)
like image 12
Chris Lawlor Avatar answered Nov 08 '22 06:11

Chris Lawlor