17

I want to show some SQL queries inside a notebook. I neither need nor want them to run. I'd just like them to be well formatted. At the very least I want them to be indented properly with new lines, though keyword highlighting would be nice too. Does a solution for this exist already?

Batman
  • 8,571
  • 7
  • 41
  • 80

4 Answers4

20

If you set the cell as Markdown one you can write the sql query as code specifying the language (e.g. mysql)

``` mysql
SELECT *
FROM table_a AS a
LIMIT 10; 
```

This produces:

sql in notebook

It highlights the keywords. Unfortunately, it doesn't seem to deal with indentation which seems to be the main issue you are trying to deal with but maybe this helps.

Algold
  • 953
  • 8
  • 10
5

If you - like me - find yourself here because you want to highlight (and run) the %%sql magic, you're best of with the technique of this answer. Posting it here cause it took me quite some time before I found the correct keywords to my answer :)

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(); } }) ;
  });
});
Roelant
  • 4,508
  • 1
  • 32
  • 62
1

I found that this fixed the issue I was having.

``` sql

Produced styled code in edit mode but not when the cell was run.

``` mysql

Produced correct styling

0

You can format SQL cells with jupysql-plugin: enter image description here

Documentation

Eduardo
  • 1,383
  • 8
  • 13