Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Highlighting in iPython Notebook Markdown Cell

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')
```
like image 565
Dallas Avatar asked Dec 10 '12 03:12

Dallas


People also ask

How do you highlight markdown in Jupyter notebook?

Highlight Code If you want to highlight a function or some code within a plain text paragraph, you can use one backtick on each side of the text like this: `Here is some code!`

How do you use markdown cells in Jupyter notebook?

You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code. To use the Keyboard Shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the m key, or you can change a cell to Code by hitting the y key.

How do you add a markup in Jupyter notebook?

Markdown cells can be selected in Jupyter Notebook by using the drop-down or also by the keyboard shortcut 'm/M' immediately after inserting a new cell.

How do you edit a markdown cell in Jupyter notebook?

Just double click on the markdown cell. Edit what you want to and Run. It will reflect the changes.


3 Answers

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.

like image 106
gotgenes Avatar answered Nov 16 '22 03:11

gotgenes


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.

like image 37
Jakob Avatar answered Nov 16 '22 03:11

Jakob


In IPython 7.2.0 notebooks you can use:

Text `code` text

in a markdown cell to print highlighted code inline.

like image 29
kiae Avatar answered Nov 16 '22 03:11

kiae