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.
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(); } }) ;
});
});
Line Magic: line starts with %sql
My attempt: Change the regex to ^%sql
but it did not work.
%sql select * from Products limit 5;
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)
In the image we can see that cell magic %sql commands are not highlighted. I want them to be highlighted.
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(); }
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With