ipython-sql
ipython-sql copied to clipboard
Support syntax highlighting
ipython-sql looks like a very valuable resource, but it would be easier to use (especially for teaching) if it supported syntax highlighting of SQL code.
As per a conversation on Twitter, this is possible; it should just be a matter of implementing appropriate support in ipython-sql: https://twitter.com/Mbussonn/status/636626829269442562
Thanks for the great tool!
Created PR #75 for this. You can also run the following in any cell to enable syntax coloring:
from IPython.display import display_javascript
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};"
display_javascript(js, raw=True)
This only need to run once per notebook (like imports).
Running the above snippet gives:
Javascript error adding output!
TypeError: IPython.CodeCell.config_defaults is undefined
See your browser Javascript console for more details.
And the Javascript console output is:
TypeError: IPython.CodeCell.config_defaults is undefined
Stack trace:
@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412 line 21632 > eval:1:1
append_javascript@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:21632:13
OutputArea.prototype.append_mime_type@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:21576:32
OutputArea.prototype.append_display_data@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:21542:13
OutputArea.prototype.append_output@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:21235:13
OutputArea.prototype.handle_output@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:21148:9
output@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:22912:21
Kernel.prototype._handle_output_message@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:24670:13
i@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:4:5486
Kernel.prototype._handle_iopub_message@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:24697:20
Kernel.prototype._finish_ws_message@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:24489:24
Kernel.prototype._handle_ws_message/this._msg_queue<@http://localhost:8889/static/notebook/js/main.min.js?v=6a166b31c5a20f809e44ec2d50e40412:24480:39
Apparently the name of the property config_defaults has changed to options_default, so the new version is:
from IPython.display import display_javascript
js = "IPython.CodeCell.options_default.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};"
display_javascript(js, raw=True)
Hi, For me the syntax highlighting only works for first cell when the notebook is loaded! For other cell even if I type the same piece of code in cell, it does not work:
from IPython.display import display_javascript js = "IPython.CodeCell.options_default.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};" display_javascript(js, raw=True)
Can some one please help on the issue?
I'd love to be able to use a notebook-style workflow when working with sql, but the lack of syntax highlighting and autocompletion in ipython-sql is really a drag. I can also verify that syntax highlighting does not work by default, and that after running the above snippet, only the next cell is properly highlighted.
A snippet for enabling sql syntax highlighting is provided in this issue — https://github.com/jupyter/notebook/issues/2453#issuecomment-298023243 — although I haven't tried it.
Any update on this topic?
The proposed code doesn't seem to work with jupyter lab. I get a Javascript Error: IPython is not defined error, that seems to be expected by the changes introduced in jupyter lab
I'd say I had syntax highlight for cells with %%sql magic a few weeks ago, but I'm not 100% sure and the fact that this issue is still open makes me doubt even more... In any case, I don't have them now for sure :P
So not totally sure this is not solved yet, or I'm just reviving some old issue because I broke something in my machine... 😅
Sorry for the noise if it's the latter