Skip to content

Highlighting Code

This example demonstrates how to create synchronized highlights across multiple elements using the Bylight library in Colight.

First, let's import the necessary libraries and define our sample text:

import colight.plot as Plot
from colight import extras
rhyme = """Roses are blue,
Violets are red,
In this rhyme's hue,
Colors are misled!"""

Use the extras.bylight function to highlight specific words in our text:

extras.bylight(rhyme, ["blue", "red", "Colors", "misled!"])

Animated Highlighting

We can create an animated version that highlights each word in sequence. Plot.Frames creates a reactive variable that we access via $state.frame. In this example we hide the slider.

Plot.Frames(
    [
        Plot.js("`frame: ${$state.frame}`") & extras.bylight(rhyme, pattern)
        for pattern in rhyme.split()
    ],
    fps=2,
    slider=False,
)