Skip to content

Commit d35b864

Browse files
committed
Add a script to expand nested frames in HTML samples. Expand nested frames in countDistinct website docs
1 parent f149860 commit d35b864

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

docs/StardustDocs/resources/api/countDistinct/countDistinctColumnsGroupBy.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@
497497

498498
call_DataFrame(function() { DataFrame.renderTable(0) });
499499

500+
(function () {
501+
function expandColumnGroups(df) {
502+
for (let col of df.cols) {
503+
if (col.parent === undefined && col.children.length > 0) col.expanded = true;
504+
}
505+
}
506+
507+
function expandNestedFrames(df, rootDf) {
508+
for (let col of df.cols) {
509+
for (let value of col.values) {
510+
if (value && value.frameId !== undefined) {
511+
rootDf.expandedFrames.add(value.frameId);
512+
let child = rootDf.childFrames[value.frameId];
513+
if (child) {
514+
expandColumnGroups(child);
515+
expandNestedFrames(child, rootDf);
516+
}
517+
}
518+
}
519+
}
520+
}
521+
522+
document.querySelectorAll("table.dataframe").forEach(function (table) {
523+
if (table.df && table.df.id === table.df.rootId) {
524+
let rootDf = table.df;
525+
expandNestedFrames(rootDf, rootDf);
526+
DataFrame.renderTable(rootDf.id);
527+
}
528+
});
529+
})();
500530
function sendHeight() {
501531
const table = document.querySelector('table.dataframe');
502532
if (!table) return;

docs/StardustDocs/resources/api/countDistinct/countDistinctGroupBy.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@
497497

498498
call_DataFrame(function() { DataFrame.renderTable(0) });
499499

500+
(function () {
501+
function expandColumnGroups(df) {
502+
for (let col of df.cols) {
503+
if (col.parent === undefined && col.children.length > 0) col.expanded = true;
504+
}
505+
}
506+
507+
function expandNestedFrames(df, rootDf) {
508+
for (let col of df.cols) {
509+
for (let value of col.values) {
510+
if (value && value.frameId !== undefined) {
511+
rootDf.expandedFrames.add(value.frameId);
512+
let child = rootDf.childFrames[value.frameId];
513+
if (child) {
514+
expandColumnGroups(child);
515+
expandNestedFrames(child, rootDf);
516+
}
517+
}
518+
}
519+
}
520+
}
521+
522+
document.querySelectorAll("table.dataframe").forEach(function (table) {
523+
if (table.df && table.df.id === table.df.rootId) {
524+
let rootDf = table.df;
525+
expandNestedFrames(rootDf, rootDf);
526+
DataFrame.renderTable(rootDf.id);
527+
}
528+
});
529+
})();
500530
function sendHeight() {
501531
const table = document.querySelector('table.dataframe');
502532
if (!table) return;

samples/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/DataFrameSampleHelper.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package org.jetbrains.kotlinx.dataframe.samples
22

33
import org.jetbrains.kotlinx.dataframe.DataColumn
4+
import org.jetbrains.kotlinx.dataframe.DataFrame
45
import org.jetbrains.kotlinx.dataframe.api.CodeString
6+
import org.jetbrains.kotlinx.dataframe.api.GroupBy
57
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
8+
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
9+
import org.jetbrains.kotlinx.dataframe.io.DisplayConfiguration
10+
import org.jetbrains.kotlinx.dataframe.io.toStandaloneHtml
611
import org.jetbrains.kotlinx.dataframe.samples.api.TestBase
712
import org.jetbrains.kotlinx.kandy.letsplot.samples.SampleHelper
813
import java.io.File
@@ -44,4 +49,48 @@ abstract class DataFrameSampleHelper(sampleName: String, subFolder: String = "sa
4449
fun DataColumn<*>.saveDfHtmlSample() {
4550
toDataFrame().saveDfHtmlSample()
4651
}
52+
53+
// TODO: might be changed as #1887 is fixed
54+
private val expandNestedFramesScript = DataFrameHtmlData(
55+
script =
56+
"""
57+
(function () {
58+
function expandColumnGroups(df) {
59+
for (let col of df.cols) {
60+
if (col.parent === undefined && col.children.length > 0) col.expanded = true;
61+
}
62+
}
63+
64+
function expandNestedFrames(df, rootDf) {
65+
for (let col of df.cols) {
66+
for (let value of col.values) {
67+
if (value && value.frameId !== undefined) {
68+
rootDf.expandedFrames.add(value.frameId);
69+
let child = rootDf.childFrames[value.frameId];
70+
if (child) {
71+
expandColumnGroups(child);
72+
expandNestedFrames(child, rootDf);
73+
}
74+
}
75+
}
76+
}
77+
}
78+
79+
document.querySelectorAll("table.dataframe").forEach(function (table) {
80+
if (table.df && table.df.id === table.df.rootId) {
81+
let rootDf = table.df;
82+
expandNestedFrames(rootDf, rootDf);
83+
DataFrame.renderTable(rootDf.id);
84+
}
85+
});
86+
})();
87+
""".trimIndent(),
88+
)
89+
90+
fun DataFrame<*>.toExpandedHtml() = toStandaloneHtml(
91+
configuration = DisplayConfiguration(enableFallbackStaticTables = false),
92+
getFooter = { "" },
93+
) + expandNestedFramesScript
94+
95+
fun GroupBy<*, *>.toExpandedHtml() = toDataFrame().toExpandedHtml()
4796
}

samples/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/CountDistinctSamples.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class CountDistinctSamples : DataFrameSampleHelper("countDistinct", "api") {
6868
// SampleStart
6969
df.groupBy { city }
7070
// SampleEnd
71+
.toExpandedHtml()
7172
.saveDfHtmlSample()
7273
}
7374

@@ -130,6 +131,7 @@ class CountDistinctSamples : DataFrameSampleHelper("countDistinct", "api") {
130131
background(firstNameToColor(firstName)) and textColor(black)
131132
}
132133
}
134+
.toExpandedHtml()
133135
.saveDfHtmlSample()
134136
}
135137
}

0 commit comments

Comments
 (0)