Package: tablat
tablatA simple way to print output in a tableBasic usageJust create aTableobject and give it a headers list and the data. The number of columns will be calculated from the number of headers.Code samplefrompathlibimportPathfromtablatimportTablefolder_path=Path('./')header=['FILE_NAME','FOLDER','FILES_IN']data=[]forfile_pathinfolder_path.iterdir():data.append(file_path.name)iffile_path.is_dir():data.extend(['YES',len([fforfinfile_path.iterdir()])])else:data.extend(['NO',0])my_table=Table(data,header)my_table.print_table()Note:print(my_table)is also validOutputInstallationYou can intall the package usingpip(Python Package Installer)pipinstalltablatorpython-mpipinstalltablatUsageCreating and modifyingTableTableobject can be initialized with the data or empty:my_table=Table(data=my_data,headers=my_headers)If it is initialized empty it can be modified or updated later:my_table=Table()my_table.headers=['FILE_NAME','IS_DIR']my_table.table_data=['My docs',True,'profile_pic.png',False]Table data can be expanded anytime:forfile_pathinPath('./').iterdir():my_table.add_data([file_path.name,file_path.is_dir()])LoadingTabledata by columnYou can use the methodset_column_content(column_dict)to init theTableby columns.
This method expects adictwith the column names askeysand a list with the column data asvalues.{"column_name1":["item11","item12","item13"],"column_name2":["item21","item21","item23"],...}Here is the example from the begining modified:frompathlibimportPathfromtablatimportTablefolder_path=Path('./')column_data={'FILE_NAME':[],'FOLDER':[],'FILES_IN':[]}forfile_pathintest_path.iterdir():column_data['FILE_NAME'].append(file_path.name)column_data['FOLDER'].append('Y'iffile_path.is_dir()else'N')column_data['FILES_IN'].append(len([fforfinfile_path.iterdir()])iffile_path.is_dir()else0)Table().set_column_content(column_data).print_table()The methodget_column_conten()returns adictwith the same structure even if you didn't initialized the table withset_column_content(column_dict).LoadingTabledata fromjsonThe methodload_data(file_path)initializes the table from ajsonfile. Thejsonfile should
have the same structure thatset_column_content(column_dict), column name askeyand column content list asvalue:{"column_name1":["item11","item12","item13"],"column_name2":["item21","item21","item23"],...}Modifying column alignmentBy default first column will be aligned to left and the rest to right. The alignment
follows the same encoding as the stringformatfunction, a character can be passed to set the alignment:Right align:>Left align:<Center align:^Column alignment can be changed withalignmentattribute orset_column_align()method.Withalignmentattribute you can provide a list with the alignment for each column:my_table.alignment=['^','^','>']Withset_column_align(index, align_char)you can change a specific column alignment (column index starts form 0):my_table.set_column_align(0,'<')Filtering columns to printBy defaultprint_table()will print all columns in the table, but you can filter what columns should be printed.print_table()have two optional arguments:show_columnsandhide_columns. This arguments expects a list with the indexes of the columns to print or hide respectively. If the two arguments are usedhide_columnswill be ignored.# Shows first and third columnmy_table.print_table(show_columns=[0,2])# Hide third column and shows the restmy_table.print_table(hide_columns=[2])Additional NotesYou can retrieve data form the table using indices# Get first row datamy_table[0]# Get third row, second columnmy_table[2][1]TablestyleSyling the table withTabStyleTabStyleclass is used to encapsulate style options for the table. Current values are:Table bordersRow separatorsColumn separatorsNote: default style iswith bordersandno separatorsfor rows and columnsUsingTabStyleto configure the style:formtablatimportTable,TabStyle# Style object with no borders and row separatorspref_style=TabStyle(borders=False,row_sep=True)...# Initializing Table with our prefered stylesome_tab=Table(data,headers,pref_style)...# Restoring Table default stylesome_tab.style=TabStyle()Tableobjects are initialized with a defaultTabStylethat can be modifiedmy_table=Table()# Disabling bordersmy_table.syle.borders=False# Modifying style properties at oncemy_table.style.update(col_sep=True,row_sep=True)Sample table with style modifications