JupyterLab Pygments Theme

0.3.0 · active · verified Sat Mar 28

This package provides a syntax highlighting theme for Pygments, designed to leverage JupyterLab's CSS variables. It aims to integrate Pygments-generated HTML code blocks seamlessly with the visual styling of JupyterLab themes. The current version is 0.3.0, released on November 23, 2023, and it is actively maintained by the Jupyter Development Team.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `jupyterlab` Pygments style to highlight Python code and display it within a Jupyter environment. The `HtmlFormatter` is configured with `style='jupyterlab'` to apply the theme, and `full=True` to embed the necessary CSS.

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

# Example Python code to highlight
code_to_highlight = """
def greet(name):
    print(f"Hello, {name}!")

greet("World")
"""

# Create an HTML formatter using the 'jupyterlab' style
# The style 'jupyterlab' is made available by the jupyterlab-pygments package
formatter = HtmlFormatter(style='jupyterlab', full=True, cssclass='highlight-jupyterlab')

# Generate the HTML output with highlighting
highlighted_html = highlight(code_to_highlight, PythonLexer(), formatter)

# To display in a Jupyter environment, use IPython.display
# For standalone use, you would typically write highlighted_html to a file.
# The 'full=True' option includes the <style> tags, making it self-contained.
display(HTML(highlighted_html))

view raw JSON →