@@ -2259,6 +2259,80 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [
22592259 self toColumnAt: each applyElementwise: aBlock ]
22602260]
22612261
2262+ { #category : #converting }
2263+ DataFrame >> toHtml [
2264+ " Prints the DataFrame as an HTML formatted table"
2265+
2266+ | html columnWidths dataFrame |
2267+ dataFrame := self copy.
2268+ dataFrame addColumn: dataFrame rowNames named: ' #' atPosition: 1 .
2269+ html := WriteStream on: String new .
2270+ html
2271+ nextPutAll: ' <table border="1" class="dataframe">' ;
2272+ cr;
2273+ nextPutAll: ' <thead>' ;
2274+ cr;
2275+ nextPutAll: ' <tr style="text-align: left;">' .
2276+
2277+ columnWidths := dataFrame columnNames collect: [ :columnName |
2278+ | maxWidth |
2279+ maxWidth := columnName size.
2280+ dataFrame rows do: [ :row |
2281+ | value |
2282+ value := row at: columnName.
2283+ maxWidth := maxWidth max: value printString size ].
2284+ maxWidth ].
2285+
2286+ dataFrame columnNames withIndexDo: [ :columnName :index |
2287+ | paddedColumnName |
2288+ paddedColumnName := columnName padRightTo: (columnWidths at: index).
2289+ html
2290+ nextPutAll: ' <th>' ;
2291+ nextPutAll: paddedColumnName;
2292+ nextPutAll: ' </th>' ;
2293+ cr ].
2294+
2295+ html
2296+ nextPutAll: ' </tr>' ;
2297+ cr;
2298+ nextPutAll: ' </thead>' ;
2299+ cr;
2300+ nextPutAll: ' <tbody>' ;
2301+ cr.
2302+
2303+ dataFrame asArrayOfRows do: [ :row |
2304+ html nextPutAll: ' <tr>' .
2305+
2306+ row withIndexDo: [ :value :index |
2307+ | paddedValue |
2308+ paddedValue := value printString padRightTo:
2309+ (columnWidths at: index).
2310+ index = 1
2311+ ifFalse: [
2312+ html
2313+ nextPutAll: ' <td>' ;
2314+ nextPutAll: paddedValue;
2315+ nextPutAll: ' </td>' ;
2316+ cr ]
2317+ ifTrue: [
2318+ html
2319+ nextPutAll: ' <th>' ;
2320+ nextPutAll: paddedValue;
2321+ nextPutAll: ' </th>' ;
2322+ cr ] ].
2323+
2324+ html
2325+ nextPutAll: ' </tr>' ;
2326+ cr ].
2327+
2328+ html
2329+ nextPutAll: ' </tbody>' ;
2330+ cr;
2331+ nextPutAll: ' </table>' .
2332+
2333+ ^ html contents
2334+ ]
2335+
22622336{ #category : #converting }
22632337DataFrame >> toLatex [
22642338 " Prints the DataFrame as a Latex formatted table"
0 commit comments