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.
🎨 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.
By default, theme of the JupyterLab interface is light. To change the theme, navigate to the Settings menu, Select JupyterLab Theme >> JupyterLab Dark.
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.
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.
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
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With