Search code examples
jupyter-notebookbackground-color

ipython notebook background colour of a cell


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.


Solution

  • 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