Is there a way to use SQL syntax highlighting in triple-quoted literals in Jupyter notebook?

Question:

I’m doing ETL development using pyspark in a Jupyter notebook. I generally prefer to use SQL queries instead of pyspark functions, since I find SQL more readable than pyspark functions most of the time. However, SQL queries in Python scripts take the form of literal strings, which are treated as, well, strings, not code, when it comes to coloring the text.

To make development easier for myself, I want to color triple-quoted literal strings the same way that SQL code would be highlighted if it stood on its own in an IDE or text editor.

Is this possible?

For example, take the following code:

print('Hello world')
sql = """
      SELECT myid, myname, CAST(mydate AS DATE)
      FROM myschema.mytable
      WHERE something
      """
execute_sql(sql)

If possible, I would like that string to appear like so, while not changing it’s characteristics of being a string:

enter image description here

Asked By: Jacob Bayer

||

Answers:

Here’s the answer:

https://github.com/CybercentreCanada/jupyterlab-sql-editor

This package has a feature that allows you to highlight SQL syntax within a string as well as run sql directly in the notebook. This is designed specifically for spark-sql, which is what I’m using.

Answered By: Jacob Bayer