@@ -20,6 +20,7 @@ def __init__(self):
2020 self .export_strategies = {
2121 "json" : self .export_to_json ,
2222 "csv" : self .export_to_csv ,
23+ "md" : self .export_to_markdown ,
2324 # Future formats can be added here
2425 }
2526
@@ -94,3 +95,27 @@ def export_to_csv(self, data: list[PromptRequestPiece], file_path: Path = None)
9495 writer = csv .DictWriter (f , fieldnames = fieldnames )
9596 writer .writeheader ()
9697 writer .writerows (export_data )
98+
99+ def export_to_markdown (self , data : list [PromptRequestPiece ], file_path : Path = None ) -> None : # type: ignore
100+ """
101+ Exports the provided data to a Markdown file at the specified file path.
102+ Each item in the data list is converted to a dictionary and formatted as a table.
103+
104+ Args:
105+ data (list[PromptRequestPiece]): The data to be exported, as a list of PromptRequestPiece instances.
106+ file_path (Path): The full path, including the file name, where the data will be exported.
107+
108+ Raises:
109+ ValueError: If no file_path is provided or if there is no data to export.
110+ """
111+ if not file_path :
112+ raise ValueError ("Please provide a valid file path for exporting data." )
113+ if not data :
114+ raise ValueError ("No data to export." )
115+ export_data = [piece .to_dict () for piece in data ]
116+ fieldnames = list (export_data [0 ].keys ())
117+ with open (file_path , "w" , newline = "" ) as f :
118+ f .write (f"| { ' | ' .join (fieldnames )} |\n " )
119+ f .write (f"| { ' | ' .join (['---' ] * len (fieldnames ))} |\n " )
120+ for row in export_data :
121+ f .write (f"| { ' | ' .join (str (row [field ]) for field in fieldnames )} |\n " )
0 commit comments