From ae7c95fb2c6bbc60fa1b1d2f5f24ba32322b8f7d Mon Sep 17 00:00:00 2001 From: Robert Schroll Date: Wed, 27 Jul 2022 16:52:23 -0700 Subject: [PATCH 1/2] Enable syntax highlighting for %%sql cells This should work with a variety of older and newer versions of Jupyter. It tries to avoid clobbering any existing highlighting modes for SQL, but that's probably overkill. Restarting and rerunning the import will end up with multiple identical regexes in the list, but that doesn't seem to be a problem. Fixing this would be difficult because, in JS, /a/ !== /a/. This is not working in JupyterLab, which seems to have a whole different way to deal with syntax highlighting. --- src/sql/magic.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sql/magic.py b/src/sql/magic.py index f2c3c3207..96fd98951 100644 --- a/src/sql/magic.py +++ b/src/sql/magic.py @@ -286,9 +286,12 @@ def _persist_dataframe(self, raw, conn, user_ns, append=False): def load_ipython_extension(ip): """Load the extension in IPython.""" - # this fails in both Firefox and Chrome for OS X. - # I get the error: TypeError: IPython.CodeCell.config_defaults is undefined - - # js = "IPython.CodeCell.config_defaults.highlight_modes['magic_sql'] = {'reg':[/^%%sql/]};" - # display_javascript(js, raw=True) + js = """ + let codeCell = (Jupyter ?? IPython).CodeCell; + let highlightModes = (codeCell.options_default ?? codeCell.config_defaults).highlight_modes; + if (!highlightModes['magic_sql']) + highlightModes['magic_sql'] = {'reg': []}; + highlightModes['magic_sql']['reg'].push(/^%%sql/); + """ + display_javascript(js, raw=True) ip.register_magics(SqlMagic) From 3b19cb440031dd43487b7a33cbd1cebc6eaa4968 Mon Sep 17 00:00:00 2001 From: Robert Schroll Date: Wed, 27 Jul 2022 17:21:15 -0700 Subject: [PATCH 2/2] Better check for Jupyter or IPython globals The null coalescing operator doesn't work for undefined names (only undefined properties), so we'll look for the global in the window namespace. If neither exists, as in JupyterLab, give up. --- src/sql/magic.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/sql/magic.py b/src/sql/magic.py index 96fd98951..a585b28b3 100644 --- a/src/sql/magic.py +++ b/src/sql/magic.py @@ -287,11 +287,13 @@ def load_ipython_extension(ip): """Load the extension in IPython.""" js = """ - let codeCell = (Jupyter ?? IPython).CodeCell; - let highlightModes = (codeCell.options_default ?? codeCell.config_defaults).highlight_modes; - if (!highlightModes['magic_sql']) - highlightModes['magic_sql'] = {'reg': []}; - highlightModes['magic_sql']['reg'].push(/^%%sql/); + let codeCell = (window.Jupyter ?? window.IPython)?.CodeCell; + if (codeCell) { + let highlightModes = (codeCell.options_default ?? codeCell.config_defaults).highlight_modes; + if (!highlightModes['magic_sql']) + highlightModes['magic_sql'] = {'reg': []}; + highlightModes['magic_sql']['reg'].push(/^%%sql/); + } """ display_javascript(js, raw=True) ip.register_magics(SqlMagic)