def _display_dataframe(self, data, update=None): if update: table_id = 'table_{}'.format(update) html = _DATAFRAME_PAGINATION_TEMPLATE.format( dataframe_html=data.to_html(notebook=True, table_id=table_id), table_id=table_id) update_display(HTML(html), display_id=update) else: table_id = 'table_{}'.format(self._df_display_id) html = _DATAFRAME_PAGINATION_TEMPLATE.format( dataframe_html=data.to_html(notebook=True, table_id=table_id), table_id=table_id) display(HTML(html), display_id=self._df_display_id)
def _display_dataframe(self, data, update=None): table_id = 'table_{}'.format( update._df_display_id if update else self._df_display_id) columns = [{ 'title': '' }] + [{ 'title': str(column) } for column in data.columns] format_window_info_in_dataframe(data) # Convert the dataframe into rows, each row looks like # [column_1_val, column_2_val, ...]. rows = data.applymap(lambda x: str(x)).to_dict('split')['data'] # Convert each row into dict where keys are column index in the datatable # to be rendered and values are data from the dataframe. Column index 0 is # left out to hold the int index (not part of the data) from dataframe. # Each row becomes: {1: column_1_val, 2: column_2_val, ...}. rows = [{k + 1: v for k, v in enumerate(row)} for row in rows] # Add the dataframe int index (used as default ordering column) to datatable # column index 0 (will be rendered as the first column). # Each row becomes: # {1: column_1_val, 2: column_2_val, ..., 0: int_index_in_dataframe}. for k, row in enumerate(rows): row[0] = k script = _DATAFRAME_SCRIPT_TEMPLATE.format(table_id=table_id, columns=columns, data_as_rows=rows) script_in_jquery_with_datatable = ie._JQUERY_WITH_DATATABLE_TEMPLATE.format( customized_script=script) # Dynamically load data into the existing datatable if not empty. if update and not update._is_datatable_empty: display_javascript(Javascript(script_in_jquery_with_datatable)) else: html = _DATAFRAME_PAGINATION_TEMPLATE.format( table_id=table_id, script_in_jquery_with_datatable=script_in_jquery_with_datatable ) if update: if not data.empty: # Re-initialize a datatable to replace the existing empty datatable. update_display(HTML(html), display_id=update._df_display_id) update._is_datatable_empty = False else: # Initialize a datatable for the first time rendering. display(HTML(html), display_id=self._df_display_id) if not data.empty: self._is_datatable_empty = False
def test_update_display(): ip = get_ipython() with mock.patch.object(ip.display_pub, 'publish') as pub: with nt.assert_raises(TypeError): display.update_display('x') display.update_display('x', display_id='1') display.update_display('y', display_id='2') args, kwargs = pub.call_args_list[0] nt.assert_equal(args, ()) nt.assert_equal(kwargs, { 'data': { 'text/plain': repr('x') }, 'metadata': {}, 'transient': { 'display_id': '1', }, 'update': True, }) args, kwargs = pub.call_args_list[1] nt.assert_equal(args, ()) nt.assert_equal(kwargs, { 'data': { 'text/plain': repr('y') }, 'metadata': {}, 'transient': { 'display_id': '2', }, 'update': True, })
def test_update_display(): ip = get_ipython() with mock.patch.object(ip.display_pub, 'publish') as pub: with nt.assert_raises(TypeError): display.update_display('x') display.update_display('x', display_id='1') display.update_display('y', display_id='2') args, kwargs = pub.call_args_list[0] nt.assert_equal(args, ()) nt.assert_equal(kwargs, { 'data': { 'text/plain': repr('x') }, 'metadata': {}, 'transient': { 'display_id': '1', }, 'update': True, }) args, kwargs = pub.call_args_list[1] nt.assert_equal(args, ()) nt.assert_equal(kwargs, { 'data': { 'text/plain': repr('y') }, 'metadata': {}, 'transient': { 'display_id': '2', }, 'update': True, })
def test_update_display(): ip = get_ipython() with mock.patch.object(ip.display_pub, "publish") as pub: with nt.assert_raises(TypeError): display.update_display("x") display.update_display("x", display_id="1") display.update_display("y", display_id="2") args, kwargs = pub.call_args_list[0] nt.assert_equal(args, ()) nt.assert_equal( kwargs, { "data": { "text/plain": repr("x") }, "metadata": {}, "transient": { "display_id": "1" }, "update": True, }, ) args, kwargs = pub.call_args_list[1] nt.assert_equal(args, ()) nt.assert_equal( kwargs, { "data": { "text/plain": repr("y") }, "metadata": {}, "transient": { "display_id": "2" }, "update": True, }, )