Change background color of output cell in JupyterLab

427 Views Asked by At

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
0

There are 0 best solutions below