为Jupyter笔记本电脑魔术添加语法高亮

leb*_*dov 6 syntax-highlighting ipython codemirror ipython-notebook jupyter

我正在尝试弄清楚如何为单元格内的CodeMirror支持的语言(cypher)激活CodeMirror语法高亮,以获得自定义Jupyter单元格魔术(%%mymagic).魔法与特殊内核无关 - 它只运行Python命令来处理输入到我要突出显示的单元格中的字符串.据我所知,这表面上可以使用类似的东西来完成

from notebook.services.config.manager import ConfigManager
cm = ConfigManager()
cm.update('notebook', {'CodeCell': {'highlight_modes': {'magic_cypher': {'reg': '^%%mymagic'}}}})
Run Code Online (Sandbox Code Playgroud)

在实现魔法的类中.但是,我似乎无法让它工作; 当我在以开头的单元格中输入内容时,没有突出显示的变化%%mymagic.上述方法准确吗?'magic_cypher'需要具有特定格式吗?魔术是否需要以某种方式指定CodeMirror与所需突出显示语言关联的MIME类型?我使用的是笔记本5.0.0,jupyter_core 4.3.0和python 2.7.13.

leb*_*dov 10

当放入~/.jupyter/custom/custom.js笔记本5.x 时,以下代码适用于SQL :

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(); } }) ;
  });
});
Run Code Online (Sandbox Code Playgroud)

幸得托马斯ķ此信息!