Skip to content

Commit d0167f1

Browse files
committed
rendering movies.ipynb using new method
1 parent 3c413da commit d0167f1

File tree

4 files changed

+157
-125
lines changed

4 files changed

+157
-125
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/Utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal fun <T> ColumnWithPath<T>.changePath(path: ColumnPath): ColumnWithPath<
5757

5858
internal fun <T> BaseColumn<T>.addParentPath(path: ColumnPath) = addPath(path + name)
5959

60-
/* TODO internal */ public fun <T> BaseColumn<T>.addPath(): ColumnWithPath<T> = addPath(pathOf(name))
60+
internal fun <T> BaseColumn<T>.addPath(): ColumnWithPath<T> = addPath(pathOf(name))
6161

6262
internal fun ColumnPath.depth() = size - 1
6363

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/html.kt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ import org.jetbrains.kotlinx.dataframe.AnyCol
55
import org.jetbrains.kotlinx.dataframe.AnyFrame
66
import org.jetbrains.kotlinx.dataframe.AnyRow
77
import org.jetbrains.kotlinx.dataframe.DataFrame
8-
import org.jetbrains.kotlinx.dataframe.api.*
8+
import org.jetbrains.kotlinx.dataframe.api.FormattingDSL
9+
import org.jetbrains.kotlinx.dataframe.api.RowColFormatter
10+
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
11+
import org.jetbrains.kotlinx.dataframe.api.asNumbers
12+
import org.jetbrains.kotlinx.dataframe.api.getColumnsWithPaths
13+
import org.jetbrains.kotlinx.dataframe.api.isColumnGroup
14+
import org.jetbrains.kotlinx.dataframe.api.isEmpty
15+
import org.jetbrains.kotlinx.dataframe.api.isNumber
16+
import org.jetbrains.kotlinx.dataframe.api.isSubtypeOf
17+
import org.jetbrains.kotlinx.dataframe.api.rows
18+
import org.jetbrains.kotlinx.dataframe.api.take
919
import org.jetbrains.kotlinx.dataframe.columns.BaseColumn
1020
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
1121
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
22+
import org.jetbrains.kotlinx.dataframe.columns.depth
1223
import org.jetbrains.kotlinx.dataframe.impl.DataFrameSize
1324
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
1425
import org.jetbrains.kotlinx.dataframe.impl.renderType
@@ -27,8 +38,7 @@ import java.io.File
2738
import java.io.InputStreamReader
2839
import java.net.URL
2940
import java.nio.file.Path
30-
import java.util.LinkedList
31-
import java.util.Random
41+
import java.util.*
3242
import kotlin.io.path.writeText
3343

3444
internal val tooltipLimit = 1000
@@ -185,7 +195,7 @@ public fun AnyFrame.toStaticHtml(
185195
val id = "static_df_${nextTableId()}"
186196
val flattenedCols = getColumnsWithPaths { cols { !it.isColumnGroup() }.recursively() }
187197
val colGrid = getColumnsHeaderGrid()
188-
val borders = colGrid.last().map { it.borders }
198+
val borders = colGrid.last().map { it.borders - Border.BOTTOM }
189199
val nestedRowsLimit = configuration.nestedRowsLimit
190200

191201
fun StringBuilder.emitTag(tag: String, attributes: String = "", tagContents: StringBuilder.() -> Unit) {
@@ -282,24 +292,27 @@ private enum class Border(val className: String) {
282292
BOTTOM("bottomBorder");
283293
}
284294

285-
private fun Set<Border>.toClass(): String = "class=\"${joinToString(" ") { it.className }}\""
295+
private fun Set<Border>.toClass(): String =
296+
if (isEmpty()) ""
297+
else "class=\"${joinToString(" ") { it.className }}\""
286298

287299
private data class ColumnWithPathWithBorder<T>(
288300
val columnWithPath: ColumnWithPath<T>? = null,
289301
val borders: Set<Border> = emptySet(),
290302
)
291303

292-
/** Returns the depth of the most-nested column in this group, starting at 0 */
293-
private fun ColumnGroup<*>.maxDepth(): Int = getColumnsWithPaths { all().recursively() }.maxOf { it.depth() }
304+
/** Returns the depth of the most-nested column in this df/group, starting at 0 */
305+
internal fun AnyFrame.maxDepth(): Int =
306+
getColumnsWithPaths { all().rec() }.maxOfOrNull { it.depth } ?: 0
294307

295308
/** Returns the max number of columns needed to display this column flattened */
296-
private fun BaseColumn<*>.maxWidth(): Int =
309+
internal fun BaseColumn<*>.maxWidth(): Int =
297310
if (this is ColumnGroup<*>) columns().sumOf { it.maxWidth() }.coerceAtLeast(1)
298311
else 1
299312

300313
/**
301314
* Given a [DataFrame], this function returns a depth-first "matrix" containing all columns
302-
* layed out in such a way that they can be used to render the header of a table. The
315+
* laid out in such a way that they can be used to render the header of a table. The
303316
* [ColumnWithPathWithBorder.columnWithPath] is `null` when nothing should be rendered in that cell.
304317
* Borders are included too, also for `null` cells.
305318
*
@@ -311,8 +324,8 @@ private fun BaseColumn<*>.maxWidth(): Int =
311324
* ```
312325
*/
313326
private fun AnyFrame.getColumnsHeaderGrid(): List<List<ColumnWithPathWithBorder<*>>> {
314-
val colGroup = asColumnGroup("")
315-
val maxDepth = colGroup.maxDepth()
327+
val colGroup = asColumnGroup()
328+
val maxDepth = maxDepth()
316329
val maxWidth = colGroup.maxWidth()
317330
val map =
318331
MutableList(maxDepth + 1) { MutableList(maxWidth) { ColumnWithPathWithBorder<Any?>() } }

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/rendering/RenderingTests.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.kotest.matchers.string.shouldNotContain
66
import org.jetbrains.kotlinx.dataframe.api.add
77
import org.jetbrains.kotlinx.dataframe.api.columnOf
88
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
9+
import org.jetbrains.kotlinx.dataframe.api.emptyDataFrame
910
import org.jetbrains.kotlinx.dataframe.api.group
1011
import org.jetbrains.kotlinx.dataframe.api.into
1112
import org.jetbrains.kotlinx.dataframe.api.move
@@ -14,19 +15,21 @@ import org.jetbrains.kotlinx.dataframe.api.toDataFrame
1415
import org.jetbrains.kotlinx.dataframe.io.DisplayConfiguration
1516
import org.jetbrains.kotlinx.dataframe.io.escapeHTML
1617
import org.jetbrains.kotlinx.dataframe.io.formatter
18+
import org.jetbrains.kotlinx.dataframe.io.maxDepth
1719
import org.jetbrains.kotlinx.dataframe.io.print
1820
import org.jetbrains.kotlinx.dataframe.io.renderToString
1921
import org.jetbrains.kotlinx.dataframe.io.renderToStringTable
2022
import org.jetbrains.kotlinx.dataframe.io.toHTML
2123
import org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer
2224
import org.jetbrains.kotlinx.dataframe.jupyter.RenderedContent
25+
import org.jetbrains.kotlinx.dataframe.samples.api.TestBase
2326
import org.jsoup.Jsoup
2427
import org.junit.Test
2528
import java.net.URL
2629
import java.text.DecimalFormatSymbols
2730
import kotlin.reflect.typeOf
2831

29-
class RenderingTests {
32+
class RenderingTests : TestBase() {
3033

3134
@Test
3235
fun `render row with unicode values as table`() {
@@ -131,18 +134,34 @@ class RenderingTests {
131134
val df = dataFrameOf("a", "b")(listOf(1, 1), listOf(2, 4))
132135
val actualHtml = df.toHTML()
133136

134-
actualHtml.body shouldContain """
137+
val body = actualHtml.body.lines().joinToString("") { it.trimStart() }
138+
139+
body shouldContain """
135140
<thead>
136141
<tr>
137-
<th>a</th><th>b</th>
142+
<th class="bottomBorder">a</th>
143+
<th class="bottomBorder">b</th>
138144
</tr>
139145
</thead>
140146
<tbody>
141147
<tr>
142-
<td>[1, 1]</td><td>[2, 4]</td>
148+
<td>[1, 1]</td>
149+
<td>[2, 4]</td>
143150
</tr>
144151
</tbody>
145152
</table>
146153
""".trimIndent().replace("\n", "")
147154
}
155+
156+
@Test
157+
fun `max depth`() {
158+
df.maxDepth() shouldBe 1
159+
dfGroup.maxDepth() shouldBe 2
160+
emptyDataFrame<Any>().maxDepth() shouldBe 0
161+
}
162+
163+
@Test
164+
fun `max width`() {
165+
// TODO
166+
}
148167
}

0 commit comments

Comments
 (0)