Skip to content

Add DataFrame.renderToMarkdown(): String function#1760

Open
koperagen wants to merge 3 commits intomasterfrom
markdown-rendering
Open

Add DataFrame.renderToMarkdown(): String function#1760
koperagen wants to merge 3 commits intomasterfrom
markdown-rendering

Conversation

@koperagen
Copy link
Collaborator

fixes #525
First, i added more tests on original renderToString. Then a little refactoring to extract common logic. +new function

Original rendering has some quirks that can be seen in test asserts. For now i just preserved it as is, but probably let's improve it as well - it will be more obvious with new tests

@koperagen koperagen requested a review from Jolanrensen March 18, 2026 18:13
@koperagen koperagen self-assigned this Mar 18, 2026
@koperagen koperagen added the enhancement New feature or request label Mar 18, 2026
@Jolanrensen
Copy link
Collaborator

Original rendering has some quirks that can be seen in test asserts

What kind of quirks do you mean?

return PreparedTable(header, values, rowsCount, nrow)
}

public fun AnyFrame.renderToMarkdown(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this deserves its own file, no? :)

val expected =
"""
| | name | age | city |
|---:|---:|---:|---:|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do these colons do? I don't think I've seen them before in MD tables

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah! it's alignment, right? Uhm, maybe we should have "no alignment" as an option, as well as right/left alignment. There are some right-to-left languages that could cause issues with a default alignment. (Actually, they probably already break tables like these, they broke our toString() very much too)

// header
sb.append("|")
for (col in table.header) {
sb.append(" ${col.replace("|", "\\|")} |")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this logic already applied in prepareTable?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, no it isn't, maybe it should :)

escapeValue: (String) -> String = { it },
): PreparedTable {
val rowsCount = rowsLimit.coerceAtMost(nrow)
val cols = if (rowIndex) listOf((0 until rowsCount).toColumn()) + columns() else columns()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know ..< for until? :)

val decimalFormat =
if (precision >= 0) RendererDecimalFormat.fromPrecision(precision) else RendererDecimalFormat.of("%e")
top.values().map {
escapeValue(renderValueForStdout(it, valueLimit, decimalFormat = decimalFormat).truncatedContent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe create a new issue for exploring rendering nested frames as <details> like toStaticHtml? or at least mention this option in some kdoc :) Speaking of... small kdoc please? :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add feature to generate Markdown table

2 participants