5

How may I change the background colour of a particular cell in iPython Notebook? For example, I'm writing a manual and I'd like to add some Terminal commands in a grey text box as in http://ipython.org/ipython-doc/1/interactive/nbconvert.html.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sahar
  • 741
  • 1
  • 8
  • 14
  • 1
    Do you want to change the background color of the whole cell or just the part with the Terminal commands? – Jakob Jan 20 '15 at 11:53
  • Thanks, Jakob, for the message. Either way would work for me. I just need to distinguish a Terminal command from the rest of my notes. Cheers, Sahar – Sahar Jan 26 '15 at 15:12

1 Answers1

3

Basically, you could write the terminal commands in preformatted code blocks within markdown cells. For single line (inline) code you can use double graves (``) like echo 1 (``echo 1``). This works exactly like here on Stackoverflow.

Longer code snippets (with syntax highlighting) may be better placed in fenced code blocks like

```bash
for i in *.jpg; do
display $i
done
```
here, bash specifies the syntax color. This renders in IPython 2.x like
enter image description here
If you want a different background style, you can use CSS styles. E.g. add the following lines in a code cell, or better (slightly adapted) to your custom.css.

%%html
<style type="text/css">
.rendered_html code  {
   background-color:#E8E8E8;
   padding: 3px;
};
</style>

With this the preformatted code looks like
enter image description here

Jakob
  • 19,815
  • 6
  • 75
  • 94
  • if I write ''echo 1'' within a Markdown cell, I just get it back in a plain shape without that nice grey highlighted area surrounding your `echo 1` text above. I don't understand why. I just noticed if I set the cell to "Raw NBConvert", instead of Markdown or Code, it keeps the text in a grey highlighted area and it would do the job I want for the moment. – Sahar Jan 27 '15 at 14:06
  • I understood the problem: I have to use triple ticks instead of double ticks to get a text in a nice shape. For example, ''echo 1'' (with ticks) within a Markdown cell returns in a plain shape, however '''echo 1''' returns nicely. Cheers, Sahar – Sahar Jan 27 '15 at 14:17
  • Sorry, I meant double [graves](http://unicode-table.com/en/search/?q=%60) not double ticks! Raw cells are mend for something different, but as long as you stay in the notebook it should be ok. – Jakob Jan 27 '15 at 14:20
  • You're right about Raw Cells. When I converted my notebook to html, I lost that grey highlighted feature I had in the notebook. I am gonna stick to ```bash``` thing you told me. Thanks a lot for your great help. As my score is low, I cannot "Like" your answer, but I could "accept" your answer. Cheers. – Sahar Jan 28 '15 at 15:28