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
rhyme = """Roses are blue,
Violets are red,
In this rhyme's hue,
Colors are misled!"""

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

Plot.bylight(rhyme, ["blue", "red", "Colors", "misled!"])
Traceback (most recent call last):
  File "/home/runner/work/colight/colight/packages/colight-prose/src/colight_prose/executor.py", line 139, in execute_block
    result.value = eval(block._eval_code, self.env)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/colight/colight/docs/src/colight_docs/examples/bylight.py", line 9, in <module>
    import colight.plot as Plot
    ^^^^^^^^^^^^
AttributeError: module 'colight.plot' has no attribute 'bylight'

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}`") & Plot.bylight(rhyme, pattern)
        for pattern in rhyme.split()
    ],
    fps=2,
    slider=False,
)