如何在jupyter笔记本中为SQL行魔术,单元魔术和自定义命令添加语法突出显示? [英] How to add syntax highlight to SQL line magic, cell magic and custom command in jupyter notebook?

查看:206
本文介绍了如何在jupyter笔记本中为SQL行魔术,单元魔术和自定义命令添加语法突出显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在jupyter笔记本中突出显示SQL代码的方法. 我只能突出显示SQL单元魔术,而不能突出显示行魔术和自定义设置.

I was searching for ways to highlight SQL codes in jupyter notebook. I was able to highlight SQL cell magic only, but not line magic and custom settings.

突出显示单元格魔术(单元格以%% sql开头)

Highlight cell magic (cell startswith %%sql)

参考:为Jupyter笔记本电池魔术添加语法高亮显示

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

情况2(无效)

Line Magic:行以%sql
开头 我的尝试:将正则表达式更改为^%sql,但不起作用.

Case 2(does not work)

Line Magic: line starts with %sql
My attempt: Change the regex to ^%sql but it did not work.

%sql select * from Products limit 5;

情况3(无效)

如何语法突出显示自定义单元格(单元格以## %%开头)
我的尝试:尝试将正则表达式更改为^##%%sql

Case 3 (does not work)

How to syntax highlight custom cells (cell startswith ##%%)
My attempt: Tried to changing regex to ^##%%sql

##%%sql
q = " select * from customer limit 2;"
execute_query(q,dbname)

示例图片

在图像中,我们可以看到单元魔术%sql命令未突出显示. 我希望它们突出显示.

Example image

In the image we can see that cell magic %sql commands are not highlighted. I want them to be highlighted.

  • adding syntax highlighting to Jupyter notebook cell magic
  • IPython change input cell syntax highlighting logic for entire session

推荐答案

与第三个单元格一样,它甚至适用于分配.当前无法使用多种语言突出显示.因此,无论使用哪种语法,都将是Python或SQL语法.

This will work even for assignments, as in the third cell. Currently multiple languages highlighting unavailable. So it will be either Python or SQL syntax, whatever comes first.

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

这篇关于如何在jupyter笔记本中为SQL行魔术,单元魔术和自定义命令添加语法突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆