2

You can use extensions or display helpers in IPython to make whatever syntax highlighting you'd like on output cells.

For some special cell magics, like %%javascript you can also see the input cell itself is rendered with that language's natural syntax highlighting.

How can you cause every input cell to be displayed with some chosen, non-Python syntax highlighting (regardless of any magics used on a cell, regardless of whether the cell embodies Python code, some other language).

In my case I am working with a custom-made cell magic for a proprietary language. The %%javascript syntax highlighting works well for this language, but if I have my own %%proprietarylang magic function, I obviously can't use %%javascript to help me with how the cell is displayed.

Is there a setting I can enable when I launch the notebook, or some property of the ipython object itself that can be programmatically set inside of my magic function, which will cause the same display logic to happen as if it was %%javascript.

I know that general-purpose on-the-fly syntax highlighting is not supported by the notebook currently. I'm asking specifically about how to make use of pre-existing syntax highlighting effects, such as that of %%javascript.

I've seen some documentation referring to IPython.config.cell_magic_highlight but this does not seem to exist anymore. Is there a standard replacement for it?

ely
  • 74,674
  • 34
  • 147
  • 228
  • You might be interested in writing a [kernel](https://github.com/ipython/ipython/wiki/IPython-kernels-for-other-languages) for your language. In IPython 3, kernels can specify what syntax highlighting to use. – Thomas K Feb 24 '15 at 22:31
  • I also [had already asked about that.](http://stackoverflow.com/questions/28517289/create-language-kernels-for-ipython-for-a-language-without-zeromq-bindings) – ely Feb 24 '15 at 22:41
  • Agreed with thomas on full kernels. – Matt Feb 24 '15 at 22:54

2 Answers2

4

To replace IPython.config.cell_magic_highlight, you can use something like

import IPython
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)

so cells which begin with %%fortran will be syntax-highlighted like FORTRAN. (However, they will still be evaluated as python if you do only this.)

Georg Sievelson
  • 300
  • 3
  • 7
  • The problem for me is that I'm looking to replace highlighting for cells that will begin with a custom cell magic, not any of the built-ins. I have a proprietary language called flow, and I've written, e.g. `%%flow` to evaluate that language in a cell. I want *that* cell to utilize the built-in JavaScript highlighting. Your solution seems like a good approach for people only dealing with pre-existing magic functions ... whereas I am asking about how to get custom syntax highlighting inside a cell that begins with a *custom* magic function. – ely Mar 07 '15 at 22:47
  • Perhaps I don't really understand what you want do. However, I was trying to add syntax coloring to a cell with a non-standard magic provided by a plugin (To be precise, I wanted @tikz cells provided by the tikzmagic extension to be syntax-highlighted in latex.), and this seems to work appropriately. I believe that using ['magic_javascript'] (or something very similar, perhaps text/javascript is needed) with {'reg':[/^%%flow/]} would do the highlighting you want. But perhaps I am mistaken. – Georg Sievelson Mar 08 '15 at 09:07
  • What version of IPython are you using? When I try this, I get "Javascript error adding output! TypeError: IPython.CodeCell.config_defaults is undefined See your browser Javascript console for more details." – ely Mar 08 '15 at 13:58
  • For example, I can't find `'config_defaults'` anywhere in the [source Javascript code](https://searchcode.com/codesearch/view/90256320/) for `CodeCell`. – ely Mar 08 '15 at 14:06
  • I see that this appears to literally have just been updated, so after installing the newest IPython release, I am able to get your example to work. Thanks! – ely Mar 08 '15 at 14:14
3

For recent IPython version, the selected answer no longer works. The 'config_default' property was renamed options_default (Ipython 6.0.0). The following works:

import IPython
js = "IPython.CodeCell.options_default.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)
vpquant
  • 81
  • 3