Introduction

[1]:
# handle launching on colab
if "google.colab" in str(get_ipython()):
    # running on CoLab
    %pip install -U "ipylabel>=0.1.7"

    from google.colab import output

    output.enable_custom_widget_manager()
[2]:
from __future__ import annotations

from ipywidgets import Output

import ipylabel

basic example

[3]:
text_widget = ipylabel.TextWidget(
    text="Мама мыла раму",
    labels=["protein", "DNA", "RNA", "Also NO"],
    colors=["#ff0000", "#00ff00", "#0000ff", "#f0f0ff"],
    result=[{"start": 0, "end": 5, "label": "DNA"}],
)

text_widget.observe(lambda c: print(c), ["result", "finished"])
text_widget

if outout of observed isn’t rendered

[4]:
output = Output()

text_widget_2 = ipylabel.TextWidget(
    text="Мама мыла раму",
    labels=["protein", "DNA", "RNA", "Also NO"],
    colors=["#ff0000", "#00ff00", "#0000ff", "#f0f0ff"],
    result=[{"start": 0, "end": 5, "label": "DNA"}],
)

@output.capture()
def finished_and_res_changed(change):
    # print("Value changed to", change)
    if change["name"] == "finished" and change["new"]:
        answer = None
    else:
        answer = change["owner"].result
    print(answer)

text_widget_2.observe(finished_and_res_changed, ["result", "finished"])
display(text_widget_2, output)
[ ]: