最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add syntax highlight to SQL line magic, cell magic and custom command in jupyter notebook? - Stack Overflow

programmeradmin4浏览0评论

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.

Case 1 (works)

Highlight cell magic (cell startswith %%sql)

Ref: adding syntax highlighting to Jupyter notebook cell magic

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

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;

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)

Example image

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

Related links

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

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.

Case 1 (works)

Highlight cell magic (cell startswith %%sql)

Ref: adding syntax highlighting to Jupyter notebook cell magic

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

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;

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)

Example image

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

Related links

  • adding syntax highlighting to Jupyter notebook cell magic
  • IPython change input cell syntax highlighting logic for entire session
Share Improve this question edited Sep 23, 2019 at 15:29 BhishanPoudel asked Sep 17, 2019 at 1:59 BhishanPoudelBhishanPoudel 17.2k25 gold badges118 silver badges188 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12 +50

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

You can open your files in colab's build-in text editor in order to enjoy correct highlights. It opens when you open a text file from the "Files" tab on the left and can be save with "ctr+s" shortcut.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论