def _update(self): """Refresh the entire widget from scratch.""" with self._widget_output: clear_output(wait=True) with self._debug_output: clear_output() display(self._debug_output) display(HTML(f"<script>{_WIDGET_TABLE_CONVERT_SCRIPT}</script>")) display(HTML(f"<style>{_WIDGET_STYLE}</style>")) display(ipw.VBox([DataFrameWidgetComponent(widget=self)]))
def DataFrameWidgetComponent(widget: DataFrameWidget) -> ipw.Widget: """The base component of the dataframe widget""" # Create the render with a table. widget_components = [ tep_table.DataFrameTableComponent(widget=widget), ] # Try to generate a document. Will return NoneType if there are no spans to render. documents_widget = tep_span.DataFrameDocumentContainerComponent(widget=widget) if documents_widget: document_output = ipw.Output() document_output.add_class("tep--dfwidget--document-output") widget._document_output = document_output widget_components.append(document_output) with document_output: display(documents_widget) # Create and return a root widget node for all created components. root_widget = ipw.VBox(children=widget_components) root_widget.add_class("tep--dfwidget--root-container") return root_widget
def __init__( self, dataframe: pd.DataFrame, metadata_column: pd.Series = None, interactive_columns: list = None, ): """An instance of an interactive widget that will display Text Extension for Pandas types Span and TokenSpan in their document contexts beside a visualization of the backing dataframe. Provides interactive table elements, multiple Span coloring modes, and tools to analyze, modify, and extend DataFrame-backed datasets. :param dataframe: The DataFrame to visualize in the widget :type dataframe: pandas.DataFrame :param metadata_column: Series of selected values to pre-load into the index column, defaults to None :type metadata_column: pandas.Series, optional :param interactive_columns: List of column names to pre-set as interactive, defaults to None :type interactive_columns: list, optional """ if isinstance(dataframe.index, pd.MultiIndex): raise NotImplementedError( "There is currently no support for the pandas MultiIndex type. " "Use pandas DataFrame instead.") self._df = dataframe.copy(deep=True) # Refreshable Outputs self._widget_output = ipw.Output() self._debug_output = ipw.Output() self._widget_output.add_class("tep--dfwidget--output") self._document_output = None # Span Visualization Globals self._tag_display = None self._color_mode = "ROW" # Initialize selected column if metadata_column is not None: md_length = len(metadata_column) # Check that metadata matches the length of the index. If too short or too long, mutate if md_length < self._df.shape[0]: metadata_column = metadata_column + [ False for _ in range(md_length, self._df.shape[0]) ] elif md_length > self._df.shape[0]: metadata_column = metadata_column[:self._df.shape[0]] # Now we have a full starting array to create a series self._metadata_column = pd.Series(metadata_column, index=self._df.index) else: self._metadata_column = pd.Series( [False for i in range(self._df.shape[0])], index=self._df.index) # Initialize interactive columns self.interactive_columns = dict() for column in self._df.columns.values: self.interactive_columns[column] = False if interactive_columns: for column in interactive_columns: self.interactive_columns.update({column: True}) # Propagate initial values to components. self._update() # Attach the widget's script. with self._widget_output: display(HTML(f"<script>{_WIDGET_SCRIPT}</script>"))
def _update_document(self): """Only refresh the document display below the table.""" if self._document_output: with self._document_output: clear_output(wait=True) display(tep_span.DataFrameDocumentContainerComponent(self))