ipython notebook background colour of a cell
Asked Answered
P

1

6

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.

Presumably answered 19/1, 2015 at 22:43 Comment(2)
Do you want to change the background color of the whole cell or just the part with the Terminal commands?Ackler
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, SaharPresumably
A
4

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

Ackler answered 26/1, 2015 at 21:17 Comment(4)
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.Presumably
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, SaharPresumably
Sorry, I meant double graves not double ticks! Raw cells are mend for something different, but as long as you stay in the notebook it should be ok.Ackler
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.Presumably

© 2022 - 2024 — McMap. All rights reserved.