Last active
May 27, 2023 06:44
-
-
Save Tuhin-thinks/d74ab87bbe3588a2740e7967eea78a24 to your computer and use it in GitHub Desktop.
Revisions
-
Tuhin-thinks revised this gist
May 27, 2023 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -103,12 +103,15 @@ def convert_md_table(): padding=padding, alignment=alignment) # set the markdown table to the output text area document['md'].value = md_table def clear_input(): document['md'].value = "" document['convert-md'].bind('click', lambda ev: convert_md_table()) document['clear'].bind('click', lambda ev: clear_input()) # if __name__ == '__main__': # price_list = ["01", "Banana", 5.00, "unit", "02", "Mango", 20.00, "kg", " 03", "Apple", 15.00, "kg", " 04", -
Tuhin-thinks created this gist
May 27, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,128 @@ from typing import Literal, List, Any, Union from browser import document, prompt, html, window def print_formatted(_header: List[str], _items: List[List[Union[str, Any]]], padding=10, alignment="^"): """ Function to print header along with list of list in tabular format. :param alignment: ^ -center alignment; < -left alignment; > -right alignment :param _header: header strings for the table :param _items: list of list, representing collection of rows :param padding: padding count :return: """ header_formatter = "" for _ in _header: header_formatter += "{:_" + alignment + str(padding) + "}" print(header_formatter.format(*[_head for _head in _header])) for item_info in _items: format_row = ("{:" + alignment + str(padding) + "}") * len(item_info) print(format_row.format(*item_info)) def get_md_table(_header: List[str], _items: List[List[Union[str, Any]]], padding=10, fill_char="", alignment: Literal['left', 'right', 'center'] = "center"): """ To generate a table for markdown. :param _header: :param _items: :param padding: :param fill_char: :param alignment: :return: """ def __get_padding_line(): sep_formatter = "|{:" + fill_char + str(padding) + "}" header_separator = "" for_center = lambda: ":" + "-" * (padding - 2) + ":" for_right = lambda: "-" * (padding - 1) + ":" for_left = lambda: ":" + "-" * (padding - 1) # this is also responsible for adding the padding line & alignment of cell contents fill_char_list = for_center() if alignment == "center" else for_left() if alignment == "left" else for_right() for _ in _header: header_separator += sep_formatter header_separator += "|" header_separator = header_separator.format(*[fill_char_list for _ in _header]) return header_separator header_formatter = "" if alignment == "center": align_char = "^" elif alignment == "right": align_char = ">" else: align_char = "<" for _head in _header: _str = "|{:^" + str(padding) + "}" header_formatter += _str.format(_head) header_formatter += "|" # header = header_formatter.format(*[_head for _head in _header]) row_formatter = "" for _ in _header: row_formatter += "|{:" + align_char + str(padding) + "}" row_formatter = row_formatter + "|" rows = "" for item_info in _items: # if there is item missing add an empty string at the end padded_item_info = item_info + [""] * (len(_header) - len(item_info)) rows += row_formatter.format(*padded_item_info) rows += "\n" return header_formatter + "\n" + __get_padding_line() + "\n" + rows def convert_md_table(): """ Function to convert JSON data into markdown table. """ header = document['header'].value.split(",") # load items from json input, text area items = window.JSON.parse(document['items'].value) # load padding from input padding = int(document['padding'].value) # load alignment from input alignment = document['alignment'].value # get the markdown table md_table = get_md_table(_header=header, _items=items, padding=padding, alignment=alignment) # set the markdown table to the output text area document['output'].value = md_table document['convert-md'].bind('click', lambda ev: convert_md_table()) document['clear'].bind('click', lambda ev: document['output'].value = "") # if __name__ == '__main__': # price_list = ["01", "Banana", 5.00, "unit", "02", "Mango", 20.00, "kg", " 03", "Apple", 15.00, "kg", " 04", # "Papaya", 25.00, "unit", "5", "Guava", 15.00] # header = ['Item no', 'Item name', 'Price', 'Unit'] # row_width = 4 # max number of items in each row # clustered_price_list = [price_list[n:n + row_width] # for n in range(0, len(price_list), row_width)] # print_formatted(_items=clustered_price_list, _header=header, padding=20) # md_table = get_md_table(_header=header, _items=clustered_price_list, # padding=20, alignment="center") # print(md_table) # with open("table.md", "w") as f: # f.write(md_table)