ipython-sql icon indicating copy to clipboard operation
ipython-sql copied to clipboard

Support syntax highlighting

Open mdekstrand opened this issue 10 years ago • 7 comments

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!

mdekstrand avatar Aug 26 '15 21:08 mdekstrand

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).

xiaochuanyu avatar Feb 14 '17 03:02 xiaochuanyu

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

ViktorQvarfordt avatar Jan 01 '18 21:01 ViktorQvarfordt

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)

ViktorQvarfordt avatar Jan 01 '18 21:01 ViktorQvarfordt

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?

ayushiagarwal avatar Apr 18 '18 21:04 ayushiagarwal

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.

maxrothman avatar Oct 04 '18 19:10 maxrothman

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.

psychemedia avatar Dec 13 '18 15:12 psychemedia

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

mgab avatar Feb 12 '21 18:02 mgab