Skip to content

Commit

Permalink
map_col(function, column_index) implemented and example added
Browse files Browse the repository at this point in the history
  • Loading branch information
nadaj committed Jan 29, 2018
1 parent 50c6215 commit 1b2660c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/templates/table_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@
# set class for every cell
table1.col_class('class_example_2')

#applies function to upper string for every cell
table1.map_col(lambda x: x.upper())

# set class for first column of each row
table2.col_class('class_example_1', 0)

#applies function to lower string for second column
table2.map_col(lambda x: x.lower(), 1)

# set class for every row
table3.row_class('class_example_3')

Expand Down
11 changes: 11 additions & 0 deletions tempy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ def row_class(self, css_class, row_index=None):
elif row_index >= 0 and (row_index < len(self.body.childs)):
self.body.childs[row_index].attr(klass=css_class)

def map_col(self, col_function, col_index=None):
# applies to every cell
if col_index is None:
for row in self.body.childs:
for cell in row.childs:
cell.childs[0] = col_function(cell.childs[0])
elif col_index >= 0:
for row in self.body.childs:
if col_index < len(row.childs):
row.childs[col_index].childs[0] = col_function(row.childs[col_index].childs[0])

class TempyListMeta:
"""Widget for lists, manages the automatic generation starting from iterables and dicts.
Plain iterables will produce plain lists, nested dicts will produce nested lists.
Expand Down

0 comments on commit 1b2660c

Please sign in to comment.