Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I customize text color in IPython?

I'd like to customize the color of text in IPython, but am not sure how to do it.

I know that in Python, I can do this by ending sys.ps1 and sys.ps2 with an ANSI color code such as

sys.ps1=">>> \001\033[0m\033[34m\002" 

But the corresponding approach, using PromptManager.in_template, does not work for IPython. For example

c = get_config() c.PromptManager.in_template = 'In [{count}] : {color.Blue}' 

has no effect on the color of text after the prompt.

Is there a way to change the color of text in IPython?

like image 395
orome Avatar asked Jan 02 '13 21:01

orome


1 Answers

Colorize and Syntax Style in IPython

First you have to create a ipython profile ~/.iphyton/ipython_config.py in your home directory. ipython_config.py. The easiest way to do so is to run the following command:

ipython profile create 

In case you are using ipython3 start

ipython3 profile create 

This will install a profile_default directory and some scripts in your ~/.ipython; otherwise find a file and copy it in your ~/.ipython/profile_default/ directory.

Make a backup of this file with:

cp ~/.ipython/profile_default/ipython_config.py \    ~/.ipython/profile_default/ipython_config.py_backup 

Open the ~/.ipython/profile_default/ipython_config.py with an text editor of your choice and search for following settig and comment it out if you like it (delete the '#'):

  • c.InteractiveShell.color_info = True

  • c.InteractiveShell.colors = 'Linux'

  • c.TerminalInteractiveShell.highlighting_style = 'monokai'

  • c.TerminalInteractiveShell.highlight_matching_brackets = True

and so on. There are many usefull settings which are disabled by default; you have only to comment them out (deleting the `#' ).

Style Files

On Ubuntu/Debian you have to install the pygments package

sudo apt install python3-pygments 

or

sudo pip3 install pygments  

The style files can be found in following directory:

/path/to/your/python/site-packages/pygments/styles/, e.g. /usr/lib/python3/dist-packages/pygments/styles/monokai.py

Alternatively, you can also list your installed styles with pygmentize:

pygmentize -L styles 
like image 133
abu_bua Avatar answered Sep 23 '22 18:09

abu_bua