text_extensions_for_pandas/jupyter/widget/table.py
def DataFrameTableComponent(widget) -> ipw.Widget:
    """Component representing the complete table of a dataframe widget."""
    # Avoids circular import
    from text_extensions_for_pandas.array.span import Span
    from text_extensions_for_pandas.array.token_span import TokenSpan

    # Locally-scoped change event handlers for the table

    def SpanOnBeginChange(change, span, text, column_name, index):
        new_span = Span(
            widget._df.get(column_name)[index].target_text, change["new"], span.end
        )
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)
        widget._update_document()

    def SpanOnEndChange(change, span, text, column_name, index):
        new_span = Span(
            widget._df.get(column_name)[index].target_text, span.begin, change["new"]
        )
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)
        widget._update_document()

    def TokenSpanOnBeginChange(
        change, token_span, text, column_name, index, data_begin, data_end
    ):
        new_span = TokenSpan(token_span.tokens, change["new"], token_span.end_token)
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)

        data_begin.max = data_end.value - 1
        data_end.min = data_begin.value + 1
        widget._update_document()

    def TokenSpanOnEndChange(
        change, token_span, text, column_name, index, data_begin, data_end
    ):
        new_span = TokenSpan(token_span.tokens, token_span.begin_token, change["new"])
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)

        data_begin.max = data_end.value - 1
        data_end.min = data_begin.value + 1
        widget._update_document()

    def MetaDataOnChange(change, index):
        widget._metadata_column[index] = change["new"]

    # Button click handlers

    def AddRowClickHandler(b):
        new_data = dataframe[len(dataframe) - 1 :]
        new_data.index = [len(dataframe)]
        widget._metadata_column = widget._metadata_column.append(
            pd.Series([False], index=[len(dataframe)])
        )
        widget._df = dataframe.append(new_data)
        widget._update()

    # Component rendering logic

    dataframe = widget._df

    table_columns = []

    # Generate the first column as the index + metadata controls box
    index_column = []
    index_column.append(ipw.HTML("<b>index</b>"))
    for index in dataframe.index:
        select_box = ipw.Checkbox(value=bool(widget._metadata_column[index]))
        select_box.observe(
            lambda change, index=index: MetaDataOnChange(change, index), names="value"
        )
        cell = ipw.HBox(
            [ipw.HTML(str(index)), select_box],
            layout=ipw.Layout(justify_content="flex-end", margin="0"),
        )
        index_column.append(cell)
    index_column_widget = ipw.VBox(index_column)
    index_column_widget.add_class("tep--dfwidget--table-index")
    table_columns.append(index_column_widget)

    # Generate the remaining columns as either static or interactive variants
    for column in dataframe.columns:
        is_interactive = widget.interactive_columns[column]
        table_columns.append(
            DataFrameTableColumnComponent(
                widget,
                is_interactive,
                dataframe[column],
                SpanOnBeginChange,
                SpanOnEndChange,
                TokenSpanOnBeginChange,
                TokenSpanOnEndChange,
            )
        )

    # After the table has been rendered, append any table controls below.
    button = ipw.Button(description="Add Row")
    button.on_click(AddRowClickHandler)

    # Finally, construct and return the widget structure.
    table = ipw.HBox(children=table_columns)
    table.add_class("tep--dfwidget--table")

    table_container = ipw.VBox(children=[table, button])
    table_container.add_class("tep--dfwidget--table-container")

    return table_container
Component representing the complete table of a dataframe widget.
Component representing the complete table of a dataframe widget.
Component representing the complete table of a dataframe widget .
def DataFrameTableComponent(widget) -> ipw.Widget:
    from text_extensions_for_pandas.array.span import Span
    from text_extensions_for_pandas.array.token_span import TokenSpan
    def SpanOnBeginChange(change, span, text, column_name, index):
        new_span = Span(
            widget._df.get(column_name)[index].target_text, change["new"], span.end
        )
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)
        widget._update_document()
    def SpanOnEndChange(change, span, text, column_name, index):
        new_span = Span(
            widget._df.get(column_name)[index].target_text, span.begin, change["new"]
        )
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)
        widget._update_document()
    def TokenSpanOnBeginChange(
        change, token_span, text, column_name, index, data_begin, data_end
    ):
        new_span = TokenSpan(token_span.tokens, change["new"], token_span.end_token)
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)
        data_begin.max = data_end.value - 1
        data_end.min = data_begin.value + 1
        widget._update_document()
    def TokenSpanOnEndChange(
        change, token_span, text, column_name, index, data_begin, data_end
    ):
        new_span = TokenSpan(token_span.tokens, token_span.begin_token, change["new"])
        widget._df.get(column_name)[index] = new_span
        text.value = str(new_span)
        data_begin.max = data_end.value - 1
        data_end.min = data_begin.value + 1
        widget._update_document()
    def MetaDataOnChange(change, index):
        widget._metadata_column[index] = change["new"]
    def AddRowClickHandler(b):
        new_data = dataframe[len(dataframe) - 1 :]
        new_data.index = [len(dataframe)]
        widget._metadata_column = widget._metadata_column.append(
            pd.Series([False], index=[len(dataframe)])
        )
        widget._df = dataframe.append(new_data)
        widget._update()
    dataframe = widget._df
    table_columns = []
    index_column = []
    index_column.append(ipw.HTML("<b>index</b>"))
    for index in dataframe.index:
        select_box = ipw.Checkbox(value=bool(widget._metadata_column[index]))
        select_box.observe(
            lambda change, index=index: MetaDataOnChange(change, index), names="value"
        )
        cell = ipw.HBox(
            [ipw.HTML(str(index)), select_box],
            layout=ipw.Layout(justify_content="flex-end", margin="0"),
        )
        index_column.append(cell)
    index_column_widget = ipw.VBox(index_column)
    index_column_widget.add_class("tep--dfwidget--table-index")
    table_columns.append(index_column_widget)
    for column in dataframe.columns:
        is_interactive = widget.interactive_columns[column]
        table_columns.append(
            DataFrameTableColumnComponent(
                widget,
                is_interactive,
                dataframe[column],
                SpanOnBeginChange,
                SpanOnEndChange,
                TokenSpanOnBeginChange,
                TokenSpanOnEndChange,
            )
        )
    button = ipw.Button(description="Add Row")
    button.on_click(AddRowClickHandler)
    table = ipw.HBox(children=table_columns)
    table.add_class("tep--dfwidget--table")
    table_container = ipw.VBox(children=[table, button])
    table_container.add_class("tep--dfwidget--table-container")
    return table_container
def DataFrameTableComponent ( widget ) -> ipw . Widget : from text_extensions_for_pandas . array . span import Span from text_extensions_for_pandas . array . token_span import TokenSpan def SpanOnBeginChange ( change , span , text , column_name , index ) : new_span = Span ( widget . _df . get ( column_name ) [ index ] . target_text , change [ "new" ] , span . end ) widget . _df . get ( column_name ) [ index ] = new_span text . value = str ( new_span ) widget . _update_document ( ) def SpanOnEndChange ( change , span , text , column_name , index ) : new_span = Span ( widget . _df . get ( column_name ) [ index ] . target_text , span . begin , change [ "new" ] ) widget . _df . get ( column_name ) [ index ] = new_span text . value = str ( new_span ) widget . _update_document ( ) def TokenSpanOnBeginChange ( change , token_span , text , column_name , index , data_begin , data_end ) : new_span = TokenSpan ( token_span . tokens , change [ "new" ] , token_span . end_token ) widget . _df . get ( column_name ) [ index ] = new_span text . value = str ( new_span ) data_begin . max = data_end . value - 1 data_end . min = data_begin . value + 1 widget . _update_document ( ) def TokenSpanOnEndChange ( change , token_span , text , column_name , index , data_begin , data_end ) : new_span = TokenSpan ( token_span . tokens , token_span . begin_token , change [ "new" ] ) widget . _df . get ( column_name ) [ index ] = new_span text . value = str ( new_span ) data_begin . max = data_end . value - 1 data_end . min = data_begin . value + 1 widget . _update_document ( ) def MetaDataOnChange ( change , index ) : widget . _metadata_column [ index ] = change [ "new" ] def AddRowClickHandler ( b ) : new_data = dataframe [ len ( dataframe ) - 1 : ] new_data . index = [ len ( dataframe ) ] widget . _metadata_column = widget . _metadata_column . append ( pd . Series ( [ False ] , index = [ len ( dataframe ) ] ) ) widget . _df = dataframe . append ( new_data ) widget . _update ( ) dataframe = widget . _df table_columns = [ ] index_column = [ ] index_column . append ( ipw . HTML ( "<b>index</b>" ) ) for index in dataframe . index : select_box = ipw . Checkbox ( value = bool ( widget . _metadata_column [ index ] ) ) select_box . observe ( lambda change , index = index : MetaDataOnChange ( change , index ) , names = "value" ) cell = ipw . HBox ( [ ipw . HTML ( str ( index ) ) , select_box ] , layout = ipw . Layout ( justify_content = "flex-end" , margin = "0" ) , ) index_column . append ( cell ) index_column_widget = ipw . VBox ( index_column ) index_column_widget . add_class ( "tep--dfwidget--table-index" ) table_columns . append ( index_column_widget ) for column in dataframe . columns : is_interactive = widget . interactive_columns [ column ] table_columns . append ( DataFrameTableColumnComponent ( widget , is_interactive , dataframe [ column ] , SpanOnBeginChange , SpanOnEndChange , TokenSpanOnBeginChange , TokenSpanOnEndChange , ) ) button = ipw . Button ( description = "Add Row" ) button . on_click ( AddRowClickHandler ) table = ipw . HBox ( children = table_columns ) table . add_class ( "tep--dfwidget--table" ) table_container = ipw . VBox ( children = [ table , button ] ) table_container . add_class ( "tep--dfwidget--table-container" ) return table_container
Component representing the complete table of a dataframe widget.
Component representing the complete table of a dataframe widget .
"""Component representing the complete table of a dataframe widget.""" # Avoids circular import # Locally-scoped change event handlers for the table # Button click handlers # Component rendering logic # Generate the first column as the index + metadata controls box # Generate the remaining columns as either static or interactive variants # After the table has been rendered, append any table controls below. # Finally, construct and return the widget structure.
widget