Create multidimensional table in ReportLab

I was debugging a ReportLab program which was used to display a multidimensional table (Tables within table). I used zip(*) function to prepare the data and found out it is not right, since Zip(*) creates an array of tuples not an array of arrays. 1. Review of data structure for table in ReportLab: ReportLab use array of arrays to prepare data for table, for example, data for a 2 column and 4 row table can be written in the following format in reportlab:
[ # table starts
['col1 on row1','col2 of row1'], # row 1 of table
['col1 of row2', 'col2 of row2'], # row 2 of table
['col1 of row3', 'col2 of row3'], # row 3 of table
['col1 of row4', 'col2 of row4'] # row 4 of table
] # table ends
Follow the same structure we could build multidimensional table, a 4-layer-array data structure:
[ #parent table start
[ # row 1 of parent table
[['dance', 'office.jpg'], ['office', 'dance.jpg'], ['panda', 'lock.jpg'], ['lock', 'shape.jpg'], ['shape', 'panda.jpg']], # child table 1 in row 1 of parent
[['herd', 'germ.jpg'], ['dock', 'house.jpg'], ['house', 'shrink.jpg'], ['shrink', 'herd.jpg'], ['germ', 'dock.jpg']] # child table 2 in row 1 of parent
], # end of row 1 of parent table
[ # row 2 of parent table
[['gift', 'letter.jpg'], ['desk', 'desk.jpg'], ['drum', 'world.jpg'], ['world', 'drum.jpg'], ['letter', 'gift.jpg']], # child table 1 in row 2 of parent
[['bee', 'study.jpg'], ['magnet', 'robot.jpg'], ['study', 'bee.jpg'], ['learn', 'magnet.jpg'], ['robot', 'learn.jpg']] # child table 2 in row 2 of parent
] # end of row 2 of parent table
]# parent table end
Here is the table created based on the upper data structure.
2. Issues of zip(*) I used this command and got a list of tuples which is not good to feed to ReportLab:
zip(*[word,image])
[('dance', 'office.jpg'), ('office', 'dance.jpg'), ('panda', 'lock.jpg'), ('lock', 'shape.jpg'), ('shape', 'panda.jpg'), ('herd', 'herd.jpg')]
Then I tried the following code. It worked.
map(list, zip(*[word,image]))
[['dance', 'office.jpg'], ['office', 'dance.jpg'], ['panda', 'lock.jpg'], ['lock', 'shape.jpg'], ['shape', 'panda.jpg'], ['herd', 'herd.jpg']]

No comments:

Post a Comment

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...