0

I am using pygments to highlight some syntax in a jupyter notebook. Here a minimal code to reproduce the issue:

from IPython.display import display, HTML
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter

from helpers.mystyle import GhDarkStyle

fmtter = HtmlFormatter(style=GhDarkStyle)

query = """
SELECT
   *
FROM
   latest.tmp
"""
display(
    HTML(
        '<style type="text/css">{}</style>    {}'.format(
            fmtter.get_style_defs(".highlight"), highlight(query, SqlLexer(), fmtter)
        )
    )
)

the style I use is just a copy from the official pygments repo (https://github.com/pygments/pygments/blob/master/pygments/styles/gh_dark.py) in order to have better control on the elements.

The output of the display is correct running the code in a classical jupyter notebook:

enter image description here

but it fails within jupyter lab:

enter image description here

I am not an expert with css, so it is not clear to me where the error comes from (either jupyter-lab or pygments). Investigating a bit, I find that if I explicitly add the highligh attribute to the single <span> classes generated from the highlight function, at least I get to show a different color for the line background:

enter image description here

which however is suboptimal in my opinion.

I use:

  • pygments: 2.14.0
  • jupyter-lab: 3.5.3
  • in general everything contained in jupyter/scipy-notebook:python-3.10.8 docker image
Neo
  • 448
  • 7
  • 18
  • Yes, JupyterLab is built on very different components than the older classic Jupyter Notebook interface. For a lot of reasons, mostly to do with complexity it will be more difficult to just hack those old solutions onto JupyterLab. So you need to seek JupyterLab-specific solutions. You probably want to look into how JupyterLab themes handle such things, see [here](https://discourse.jupyter.org/t/i-want-to-know-if-there-is-a-better-way-to-code-highlight-code-in-cells-in-jupyter-labs/11594/2?u=fomightez). ... – Wayne Feb 16 '23 at 21:28
  • [This post](https://discourse.jupyter.org/t/change-jupyter-lab-css-values-from-within/17254?u=fomightez) addresses trying things out in working up to using themes. CSS is discussed [here](https://discourse.jupyter.org/t/problem-with-css-to-change-colors-in-jupyterlab-notebook/7146?u=fomightez). Some highlighting stuff is linked to from [here](https://discourse.jupyter.org/t/minor-ux-selected-text-highlighting-other-occurrences/7496/3?u=fomightez). – Wayne Feb 16 '23 at 21:28
  • I just saw [this thorough response](https://stackoverflow.com/a/75477958/8508004) which ties in to controlling colors in JupyterLab, too. – Wayne Feb 17 '23 at 16:11
  • Related to: https://stackoverflow.com/questions/49429585 – krassowski Feb 18 '23 at 10:52

0 Answers0