0

Javascript Magic Cell in Jupyter Notebook

When I run a cell in my local notebook I see the above. How do I get Jupyter to apply syntax highlighting to these cells?

2 Answers 2

2

The following code works for SQL when placed in ~/.jupyter/custom/custom.js with notebook 5.x:

require(['notebook/js/codecell'], function(codecell) {
  codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
  Jupyter.notebook.get_cells().map(function(cell){
      if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

I've removed the SQL specific stuff as an edit, and it works! Apparently JS highlighting is available "out of the box."
0

JavaScript version:

%%js

require(['notebook/js/codecell'], (codecell) => {
  codecell.CodeCell.options_default.highlight_modes.magic_javascript = 
      {reg: [/^%%(?:js|javascript|script node)/]}
  Jupyter.notebook.events.one('kernel_ready.Kernel', () => {
    Jupyter.notebook.get_cells().map((cell) => {
      if (cell.cell_type == 'code'){
          cell.auto_highlight()
      }})
    })
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.