Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of a single cell in a jupyter notebook / jupyterlab?

I design a notebook so that variables that could be changed by the user are grouped into distinct cells throughout the notebook. I would like to highlight those cells with a different background color so that it is obvious to the user where the knobs are.

How could I achieve that?

NB: This related question was about static code highlighting (for a manual) and the accepted answer proposed to basically put everything in markup comments. In my case, I want highlighted code to be in a runnable cell.

like image 851
P-Gn Avatar asked Mar 22 '18 13:03

P-Gn


People also ask

How do you color a cell in Jupyter notebook?

🎨 1. We can use colour names or hexadecimal colour code: Example: <font color=green>green text</font>, <font color=blue>*blue italised text*</font> and <font color=#FF0000>**red bold text**</font>. If you would like to explore more colour names, this may come in handy.

How do I change the background color in Jupyter lab?

By default, theme of the JupyterLab interface is light. To change the theme, navigate to the Settings menu, Select JupyterLab Theme >> JupyterLab Dark.

How do you select specific cells in a Jupyter notebook?

Select Multiple Cells: Shift + J or Shift + Down selects the next sell in a downwards direction. You can also select sells in an upwards direction by using Shift + K or Shift + Up . Once cells are selected, you can then delete / copy / cut / paste / run them as a batch.

How do you highlight a cell in Jupyter notebook?

Show activity on this post. Then a button with a lightbulb icon will appear on your Jupyter notebook toolbar. Pressing that button will highlight the selected lines (or, if there is no selection, the current line) in the current code cell.


2 Answers

Small addition to krassowski's code (tried to add it as comment but couldn't get the formatting to work).

from IPython.core.magic import register_cell_magic
from IPython.display import HTML, display

@register_cell_magic
def bgc(color, cell=None):
    script = (
        "var cell = this.closest('.jp-CodeCell');"
        "var editor = cell.querySelector('.jp-Editor');"
        "editor.style.background='{}';"
        "this.parentNode.removeChild(this)"
    ).format(color)

    display(HTML('<img src onerror="{}">'.format(script)))

This way you can use it both as magic and with normal function call:

bgc('yellow')
bla = 'bla'*3

or

%%bgc yellow
bla = 'bla'*3
like image 112
Gabe Avatar answered Oct 19 '22 08:10

Gabe


Here you go (assuming that you use Python kernel):

from IPython.display import HTML, display

def set_background(color):    
    script = (
        "var cell = this.closest('.jp-CodeCell');"
        "var editor = cell.querySelector('.jp-Editor');"
        "editor.style.background='{}';"
        "this.parentNode.removeChild(this)"
    ).format(color)
    
    display(HTML('<img src onerror="{}" style="display:none">'.format(script)))

Then use it like this:

set_background('honeydew')

The solution is a bit hacky, and I would be happy to see a more elegant one. Demo:

enter image description here

Tested in Firefox 60 and Chrome 67 using JupyterLab 0.32.1.

Edit to have it as cell magic, you could simply do:

from IPython.core.magic import register_cell_magic

@register_cell_magic
def background(color, cell):
    set_background(color)
    return eval(cell)

and use it like:

%%background honeydew
my_important_param = 42
like image 21
krassowski Avatar answered Oct 19 '22 07:10

krassowski