Skip to content

Commit

Permalink
List of modified elements (#120)
Browse files Browse the repository at this point in the history
* list of modified elements
  • Loading branch information
tavisit authored Aug 1, 2023
1 parent 36e25c7 commit de50334
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 17 deletions.
29 changes: 12 additions & 17 deletions qf_lib/documents_utils/document_exporting/element/df_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,22 @@ def modify_data(self, location: Optional[Union[Any, Sequence[Any], Tuple[Any, An
if data_type == DataType.INDEX:
if not self.index_styling:
self.index_styling = Style()
self._add_styles_classes(self.index_styling, data_to_update, styling_type, modify_data)
list_of_modified_elements = [self.index_styling]
elif data_type == DataType.ROW:
for row in self.rows_styles.loc[location]:
self._add_styles_classes(row, data_to_update, styling_type, modify_data)
list_of_modified_elements = self.rows_styles.loc[location].tolist()
elif data_type == DataType.COLUMN:
if not isinstance(location, list):
location = [location]
for column_name in location:
self._add_styles_classes(self.columns_styles[column_name], data_to_update, styling_type, modify_data)
location = location if isinstance(location, list) else [location]
list_of_modified_elements = [self.columns_styles[column_name] for column_name in location]
elif data_type == DataType.CELL:
if not isinstance(location[0], list):
location = ([location[0]], location[1])
if not isinstance(location[1], list):
location = (location[0], [location[1]])

for column_name in location[0]:
for row in location[1]:
self._add_styles_classes(self.styles.loc[row, column_name], data_to_update, styling_type,
modify_data)
location = tuple([item] if not isinstance(item, list) else item for item in location)
list_of_modified_elements = [self.styles.loc[row, column_name] for column_name in location[0] for row in location[1]]
elif data_type == DataType.TABLE:
self._add_styles_classes(self.table_styles, data_to_update, styling_type, modify_data)
list_of_modified_elements = [self.table_styles]
else:
list_of_modified_elements = []

for modified_element in list_of_modified_elements:
self._add_styles_classes(modified_element, data_to_update, styling_type, modify_data)

def iterrows(self):
return zip(self.data.iterrows(), self.styles.iterrows())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import unittest

import pandas as pd

from qf_lib.containers.dataframe.qf_dataframe import QFDataFrame
from qf_lib.containers.series.qf_series import QFSeries
from qf_lib.documents_utils.document_exporting.element.df_table import DFTable


class TestElements(unittest.TestCase):
def setUp(self):
# Sample data
data = {
('General', 'Name'): ['John', 'Alice', 'Bob'],
('General', 'Age'): [28, 24, 22],
('Contact', 'Email'): ['[email protected]', '[email protected]', '[email protected]'],
('Contact', 'Phone'): ['123-456-7890', '987-654-3210', '555-555-5555'],
('Address', 'City'): ['New York', 'San Francisco', 'Los Angeles'],
('Address', 'Zip Code'): [10001, 94101, 90001]
}

# Create a DataFrame with MultiIndex for both rows and columns
data_nested = pd.DataFrame(data)
data_nested.index = pd.MultiIndex.from_tuples([('Person', 0), ('Person', 1), ('Person', 2)],
names=['Category', 'ID'])

# Create a DataFrame with a MultiIndex for columns
data_nested_html = DFTable(data_nested, css_classes=["table", "wide-first-column"])
data_nested_html.add_index_class("wider-second-column")
data_nested_html.add_index_class("center-align")
data_nested_html.add_index_style({"background-color": "rgb(225, 225, 225)"})
data_nested_html.add_index_style({"border-color": "rgb(100, 0, 0)"})
data_nested_html.add_index_style({"color": "rgb(0, 1, 0)"})
data_nested_html.remove_index_style({"color": "rgb(0, 1, 0)"})
dark_red = "#8B0000"
data_nested_html.add_rows_styles(data_nested_html.model.data.index.tolist()[::2],
{"background-color": "rgb(225, 225, 225)"})
data_nested_html.add_columns_styles(data_nested_html.columns[0], {"width": "30%", "text-align": "center"})
data_nested_html.add_columns_styles(data_nested_html.columns[0], {"color": "red"})
data_nested_html.add_cells_styles(data_nested_html.columns[1],
("Person", 1),
{"background-color": "rgb(100, 100, 255)", "color": dark_red})
data_nested_html.add_cells_styles([data_nested_html.columns[0], data_nested_html.columns[4]],
[("Person", 0), ("Person", 1)],
{"background-color": "rgb(100, 255, 255)", "color": dark_red})
self.data_nested_html = data_nested_html

# Create a DataFrame with MultiIndex for both rows and columns
data_nested_2 = pd.DataFrame(data)
data_nested_2.index = pd.MultiIndex.from_tuples([('Person', 0), ('Person', 1), ('Person', 2)],
names=['Category', 'ID'])

# Create a DataFrame with a MultiIndex for columns
data_nested_2_html = DFTable(data_nested_2, css_classes=["table", "wide-first-column"], include_index=False)
dark_red = "#8B08B0"
data_nested_2_html.add_cells_styles([data_nested_2_html.columns[1], data_nested_2_html.columns[3]],
[("Person", 1), ("Person", 2)],
{"background-color": "rgb(255, 100, 255)", "color": dark_red})
self.data_nested_2_html = data_nested_2_html

def test_index(self):
self.assertTrue(self.data_nested_html.model.index_styling is not None)
self.assertEqual(self.data_nested_html.model.index_styling.css_class, ['wider-second-column', 'center-align'])
self.assertEqual(self.data_nested_html.model.index_styling.style, {'background-color': 'rgb(225,225,225)',
'border-color': 'rgb(100,0,0)'})

self.assertTrue(self.data_nested_2_html.model.index_styling is None)

def test_columns(self):
self.assertTrue(self.data_nested_html.model.columns_styles is not None)
self.assertEqual(self.data_nested_html.model.columns_styles[('General', 'Name')].style,
{'width': '30%', 'text-align': 'center', 'color': 'red'})

def test_rows(self):
self.assertTrue(isinstance(self.data_nested_html.model.rows_styles, QFSeries))
self.assertEqual(self.data_nested_html.model.rows_styles.values[-1].style,
{'background-color': 'rgb(225,225,225)'})

def test_individual_cells(self):
self.assertTrue(isinstance(self.data_nested_html.model.styles, QFDataFrame))
self.assertTrue(self.data_nested_html.model.styles[("General", "Age")].iloc[1] is not None)
self.assertTrue(self.data_nested_html.model.styles[("General", "Age")].iloc[1] is not None)
self.assertEqual(self.data_nested_html.model.styles[("General", "Age")].iloc[1].style,
{'background-color': 'rgb(100,100,255)', 'color': '#8B0000'})


if __name__ == '__main__':
unittest.main()

0 comments on commit de50334

Please sign in to comment.