text_extensions_for_pandas/jupyter/widget/span.py
def DataFrameDocumentComponent(widget, text, spans): # -> ipw.Widget
    """A component that renders the context of a document by generating visisble
    highlights for a column of spans."""

    # Create some color tracking variables
    _color_map = {}
    _color_index = 0

    # Create stacks
    begin_stack = []
    end_stack = []

    for entry in spans:
        span = entry["span"]
        i_loc = entry["i_loc"]  # Index in the dataframe
        column = entry["column"]  # Column header
        if span.begin < span.end:
            bisect.insort(begin_stack, (span.begin, i_loc, column))
            bisect.insort(end_stack, (span.end, i_loc, column))

    begin_stack = collections.deque(begin_stack)
    end_stack = collections.deque(end_stack)

    start_index = 0
    open_spans = []

    document_elements: list(str) = []

    while len(begin_stack) > 0 and len(end_stack) > 0:
        if begin_stack[0][0] < end_stack[0][0]:
            if len(open_spans) == 0:
                document_elements = document_elements + _get_linebreak_text_array(
                    text[start_index : begin_stack[0][0]]
                )

                start_index = begin_stack[0][0]
                # Add the span's ID to the open ID list
                open_spans.append(begin_stack[0][1])
                begin_stack.popleft()
            else:
                span_tag = _get_span_tag(
                    widget._df, widget._tag_display, begin_stack[0]
                )
                span_text = text[start_index : begin_stack[0][0]]
                span_color, _color_map, _color_index = _get_span_color(
                    widget._color_mode,
                    begin_stack[0],
                    span_tag,
                    _color_map,
                    _color_index,
                )
                document_elements.append(
                    DocumentSpan(
                        text=text[start_index : begin_stack[0][0]],
                        tag=span_tag,
                        show_tag=False,
                        bgcolor=span_color,
                        span_indices=open_spans,
                    )
                )

                start_index = begin_stack[0][0]
                # Add the span's ID to the open ID list
                open_spans.append(begin_stack[0][1])
                begin_stack.popleft()
        else:
            span_tag = _get_span_tag(widget._df, widget._tag_display, end_stack[0])
            span_text = text[start_index : end_stack[0][0]]
            span_color, _color_map, _color_index = _get_span_color(
                widget._color_mode, end_stack[0], span_tag, _color_map, _color_index
            )
            document_elements.append(
                DocumentSpan(
                    text=span_text,
                    tag=span_tag,
                    show_tag=True,
                    bgcolor=span_color,
                    span_indices=open_spans,
                )
            )

            start_index = end_stack[0][0]
            open_spans.remove(end_stack[0][1])
            end_stack.popleft()

    while len(end_stack) > 0:
        span_tag = _get_span_tag(widget._df, widget._tag_display, end_stack[0])
        span_text = text[start_index : end_stack[0][0]]
        span_color, _color_map, _color_index = _get_span_color(
            widget._color_mode, end_stack[0], span_tag, _color_map, _color_index
        )
        document_elements.append(
            DocumentSpan(
                text=span_text,
                tag=span_tag,
                show_tag=True,
                bgcolor=span_color,
                span_indices=open_spans,
            )
        )

        start_index = end_stack[0][0]
        open_spans.remove(end_stack[0][1])
        end_stack.popleft()

    document_elements.append(text[start_index:])

    return ipw.HTML(
        f"""
            <div class='document'>
                {"".join(document_elements)}
            </div>
        """
    )
A component that renders the context of a document by generating visisble
    highlights for a column of spans.
A component that renders the context of a document by generating visisble
highlights for a column of spans.
A component that renders the context of a document by generating visisble highlights for a column of spans .
def DataFrameDocumentComponent(widget, text, spans): 
    _color_map = {}
    _color_index = 0
    begin_stack = []
    end_stack = []
    for entry in spans:
        span = entry["span"]
        i_loc = entry["i_loc"]  
        column = entry["column"]  
        if span.begin < span.end:
            bisect.insort(begin_stack, (span.begin, i_loc, column))
            bisect.insort(end_stack, (span.end, i_loc, column))
    begin_stack = collections.deque(begin_stack)
    end_stack = collections.deque(end_stack)
    start_index = 0
    open_spans = []
    document_elements: list(str) = []
    while len(begin_stack) > 0 and len(end_stack) > 0:
        if begin_stack[0][0] < end_stack[0][0]:
            if len(open_spans) == 0:
                document_elements = document_elements + _get_linebreak_text_array(
                    text[start_index : begin_stack[0][0]]
                )
                start_index = begin_stack[0][0]
                open_spans.append(begin_stack[0][1])
                begin_stack.popleft()
            else:
                span_tag = _get_span_tag(
                    widget._df, widget._tag_display, begin_stack[0]
                )
                span_text = text[start_index : begin_stack[0][0]]
                span_color, _color_map, _color_index = _get_span_color(
                    widget._color_mode,
                    begin_stack[0],
                    span_tag,
                    _color_map,
                    _color_index,
                )
                document_elements.append(
                    DocumentSpan(
                        text=text[start_index : begin_stack[0][0]],
                        tag=span_tag,
                        show_tag=False,
                        bgcolor=span_color,
                        span_indices=open_spans,
                    )
                )
                start_index = begin_stack[0][0]
                open_spans.append(begin_stack[0][1])
                begin_stack.popleft()
        else:
            span_tag = _get_span_tag(widget._df, widget._tag_display, end_stack[0])
            span_text = text[start_index : end_stack[0][0]]
            span_color, _color_map, _color_index = _get_span_color(
                widget._color_mode, end_stack[0], span_tag, _color_map, _color_index
            )
            document_elements.append(
                DocumentSpan(
                    text=span_text,
                    tag=span_tag,
                    show_tag=True,
                    bgcolor=span_color,
                    span_indices=open_spans,
                )
            )
            start_index = end_stack[0][0]
            open_spans.remove(end_stack[0][1])
            end_stack.popleft()
    while len(end_stack) > 0:
        span_tag = _get_span_tag(widget._df, widget._tag_display, end_stack[0])
        span_text = text[start_index : end_stack[0][0]]
        span_color, _color_map, _color_index = _get_span_color(
            widget._color_mode, end_stack[0], span_tag, _color_map, _color_index
        )
        document_elements.append(
            DocumentSpan(
                text=span_text,
                tag=span_tag,
                show_tag=True,
                bgcolor=span_color,
                span_indices=open_spans,
            )
        )
        start_index = end_stack[0][0]
        open_spans.remove(end_stack[0][1])
        end_stack.popleft()
    document_elements.append(text[start_index:])
    return ipw.HTML(
        f"""
            <div class='document'>
                {"".join(document_elements)}
            </div>
        """
    )
def DataFrameDocumentComponent ( widget , text , spans ) : _color_map = { } _color_index = 0 begin_stack = [ ] end_stack = [ ] for entry in spans : span = entry [ "span" ] i_loc = entry [ "i_loc" ] column = entry [ "column" ] if span . begin < span . end : bisect . insort ( begin_stack , ( span . begin , i_loc , column ) ) bisect . insort ( end_stack , ( span . end , i_loc , column ) ) begin_stack = collections . deque ( begin_stack ) end_stack = collections . deque ( end_stack ) start_index = 0 open_spans = [ ] document_elements : list ( str ) = [ ] while len ( begin_stack ) > 0 and len ( end_stack ) > 0 : if begin_stack [ 0 ] [ 0 ] < end_stack [ 0 ] [ 0 ] : if len ( open_spans ) == 0 : document_elements = document_elements + _get_linebreak_text_array ( text [ start_index : begin_stack [ 0 ] [ 0 ] ] ) start_index = begin_stack [ 0 ] [ 0 ] open_spans . append ( begin_stack [ 0 ] [ 1 ] ) begin_stack . popleft ( ) else : span_tag = _get_span_tag ( widget . _df , widget . _tag_display , begin_stack [ 0 ] ) span_text = text [ start_index : begin_stack [ 0 ] [ 0 ] ] span_color , _color_map , _color_index = _get_span_color ( widget . _color_mode , begin_stack [ 0 ] , span_tag , _color_map , _color_index , ) document_elements . append ( DocumentSpan ( text = text [ start_index : begin_stack [ 0 ] [ 0 ] ] , tag = span_tag , show_tag = False , bgcolor = span_color , span_indices = open_spans , ) ) start_index = begin_stack [ 0 ] [ 0 ] open_spans . append ( begin_stack [ 0 ] [ 1 ] ) begin_stack . popleft ( ) else : span_tag = _get_span_tag ( widget . _df , widget . _tag_display , end_stack [ 0 ] ) span_text = text [ start_index : end_stack [ 0 ] [ 0 ] ] span_color , _color_map , _color_index = _get_span_color ( widget . _color_mode , end_stack [ 0 ] , span_tag , _color_map , _color_index ) document_elements . append ( DocumentSpan ( text = span_text , tag = span_tag , show_tag = True , bgcolor = span_color , span_indices = open_spans , ) ) start_index = end_stack [ 0 ] [ 0 ] open_spans . remove ( end_stack [ 0 ] [ 1 ] ) end_stack . popleft ( ) while len ( end_stack ) > 0 : span_tag = _get_span_tag ( widget . _df , widget . _tag_display , end_stack [ 0 ] ) span_text = text [ start_index : end_stack [ 0 ] [ 0 ] ] span_color , _color_map , _color_index = _get_span_color ( widget . _color_mode , end_stack [ 0 ] , span_tag , _color_map , _color_index ) document_elements . append ( DocumentSpan ( text = span_text , tag = span_tag , show_tag = True , bgcolor = span_color , span_indices = open_spans , ) ) start_index = end_stack [ 0 ] [ 0 ] open_spans . remove ( end_stack [ 0 ] [ 1 ] ) end_stack . popleft ( ) document_elements . append ( text [ start_index : ] ) return ipw . HTML ( f"""
            <div class='document'>
                {"".join(document_elements)}
            </div>
        """ )
A component that renders the context of a document by generating visisble
highlights for a column of spans.
A component that renders the context of a document by generating visisble highlights for a column of spans .
# -> ipw.Widget """A component that renders the context of a document by generating visisble
    highlights for a column of spans.""" # Create some color tracking variables # Create stacks # Index in the dataframe # Column header # Add the span's ID to the open ID list # Add the span's ID to the open ID list
widget
text
spans