I've been exploring several of ways of using coloured background markdown cells in notebooks (eg for emphasising activities or providing feedback to students) but have been encountering problems with each approach:
2) tweaking metadata and then styling on that basis - users select a cell and click a button to toggle the background colour.
// add button to make codecell commentate red
var commentateCodeCell = (function() {
* Set codecell to commentate
* @param {Object} cell current notebook cell
setcommentate = function (cell) {
console.log("Run setcommentate");
if (cell instanceof IPython.CodeCell) {
var prompt = cell.element.find('div.input_area');
//var output_area = cell.element.find('div.output_subarea');
var cp=cell.element.find('div.input').parent()
if (cell.metadata.commentate == true) {
cp.css("background-color","#eda7c3");
prompt.css("background-color","#f4cadb");
//output_area.css("background-color","#f5ffff");
prompt.css("background-color","#f5f5f5");
cp.css("background-color","#ffffff");
//output_area.css("background-color","#ffffff");
} else if (cell instanceof IPython.MarkdownCell) {
var prompt = cell.element;
//var cp=cell.element.find('div.input').parent()
if (cell.metadata.commentate==true) {
prompt.css("background-color","#f4cadb");
prompt.find('div.inner_cell').css("background-color","#f4cadb")
prompt.css("background-color","#ffffff")
prompt.find('div.inner_cell').css("background-color","#ffffff")
function togglecommentate() {
console.log("Run togglecommentate");
var cell = IPython.notebook.get_selected_cell();
if ((cell instanceof IPython.CodeCell) || (cell instanceof IPython.MarkdownCell)) {
if (!('commentate' in cell.metadata))
cell.metadata.commentate = true;
else cell.metadata.commentate = !cell.metadata.commentate
* Add run control buttons to toolbar and initialize codecells
IPython.toolbar.add_buttons_group([
id : 'commentate_codecell',
label : 'Toggle cell comment',
icon : 'fa-exclamation-circle',
callback : togglecommentate
function oustyle_notebook_commentate(){
console.log("Run oustyle_notebook_commentate");
/* loop through notebook and set style of commentate cell defined in metadata */
var cells = IPython.notebook.get_cells();
if ((cell instanceof IPython.CodeCell) || (cell instanceof IPython.MarkdownCell)) {
if ('commentate' in cell.metadata) {
//$([IPython.events]).on('create.Cell',create_cell);
$([IPython.events]).on('notebook_loaded.Notebook',oustyle_notebook_commentate());
Unfortunately, each approach comes with its own set of problems.
In approach 1, wrapping content in the div doesn't get styled correctly (line breaks and lists break; code highlighting is a bit scrappy): anyone know if there are any easy fixes to this?
In approach 2, in chrome on mac at least, opening a notebook doesn't always lead to the javascript loading and running, and hence the styling is not rendered. I don't know if this is a problem with my code (which is quite possible), with the event the code should fire on, or as a result of Google Chrome doing some cacheing foo? Again - any pointers as to what the issue might be would be appreciated.