adding syntax highlighting to Jupyter notebook cell magic

后端 未结 2 1629
半阙折子戏
半阙折子戏 2020-12-31 16:44

I\'m trying to figure out how to activate CodeMirror syntax highlighting for a CodeMirror-supported language (cypher) within a cell for a custom Jupyter cell magic (%%

相关标签:
2条回答
  • 2020-12-31 17:11

    The case where I've been successful doing this was in adding SQL highlighting for the %%sql magic. I did this by adding the following to ~/.jupyter/custom/custom.js. The first line adds the mode to the Codemirror configuration, the rest apply the style to any existing cells in the workbook that need it (later cells will get styled appropriately as they are created). I haven't been successful in having it happen when the magic is installed, although I expect that it is possible.

    IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
    IPython.notebook.events.one('kernel_ready.Kernel', function(){
        IPython.notebook.get_cells().map(function(cell){
            if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
    });
    
    0 讨论(0)
  • 2020-12-31 17:13

    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(); } }) ;
      });
    });
    

    Credit goes to Thomas K for this info!

    0 讨论(0)
提交回复
热议问题