Question

Is there a way to get a Markdown cell in iPython Notebooks to highlight syntax in code blocks?

For instance, in GitHub, one can get the desired effect via the following.

```python
>>>print('hello')
```
Was it helpful?

Solution

The GitHub Flavored Markdown-style of denoting code using the triple-backtick is now supported in IPython master branch on GitHub, and so will be included in the 1.0 release.

As Jakob noted, even prior to this, you could use regular markdown for code, in which you just need to indent your code by four spaces, and this continues to be a valid way of displaying code in your IPython notebook.

OTHER TIPS

using IPython 0.13.1 syntax highlighting is as easy as (in a markdown cell):

some text

    def foo():
        print 'bar'
        return 0

some text

Just, use a blank line before and indent the code (see example notebooks shipped with Ipython). This works for Python and some other languages.

In IPython 7.2.0 notebooks you can use:

Text `code` text

in a markdown cell to print highlighted code inline.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top