Skip to content

Commit 2c50b78

Browse files
formatHeader fixes
1 parent 3e9f7a7 commit 2c50b78

File tree

5 files changed

+97
-35
lines changed

5 files changed

+97
-35
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/format.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ public class FormattedFrame<T>(
838838
public fun getDisplayConfiguration(configuration: DisplayConfiguration): DisplayConfiguration =
839839
configuration.copy(
840840
cellFormatter = formatter as RowColFormatter<*, *>?,
841-
headerFormatter = headerFormatter as HeaderColFormatter<*>?,
841+
headerFormatter = headerFormatter,
842842
)
843843
}
844844

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/formatHeader.kt

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
55
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
66
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
77
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
8+
import org.jetbrains.kotlinx.dataframe.impl.api.formatHeaderImpl
89
import org.jetbrains.kotlinx.dataframe.impl.getColumnPaths
910

1011
// region docs
@@ -48,7 +49,7 @@ public class HeaderFormatClause<T, C>(
4849
/**
4950
* **Experimental API. It may be changed in the future.**
5051
*
51-
* Selects [columns] whose headers should be formatted.
52+
* Selects [columns] whose headers should be formatted. If unspecified, all columns will be formatted.
5253
*
5354
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause]
5455
* which must be finalized using [HeaderFormatClause.with].
@@ -138,38 +139,7 @@ public fun <T> FormattedFrame<T>.formatHeader(): HeaderFormatClause<T, Any?> =
138139
* applied to its children unless explicitly overridden.
139140
*/
140141
@Suppress("UNCHECKED_CAST")
141-
public fun <T, C> HeaderFormatClause<T, C>.with(formatter: HeaderColFormatter<C>): FormattedFrame<T> {
142-
val selectedPaths = df.getColumnPaths(UnresolvedColumnsPolicy.Skip, columns).toSet()
143-
val oldHeader = oldHeaderFormatter
144-
145-
val composedHeader: HeaderColFormatter<Any?> = { col ->
146-
val path = col.path
147-
// Merge attributes from selected parents
148-
val parentAttributes = if (path.size > 1) {
149-
val parentPaths = (0 until path.size - 1).map { i -> path.take(i + 1) }
150-
parentPaths
151-
.map { p -> ColumnWithPath(df[p], p) }
152-
.map { parentCol ->
153-
if (parentCol.path in selectedPaths) {
154-
oldHeader?.invoke(FormattingDsl, parentCol as ColumnWithPath<C>)
155-
} else {
156-
null
157-
}
158-
}
159-
.reduceOrNull(CellAttributes?::and)
160-
} else {
161-
null
162-
}
163-
164-
val typedCol = col as ColumnWithPath<C>
165-
166-
val existingAttr = oldHeader?.invoke(FormattingDsl, typedCol)
167-
val newAttr = if (path in selectedPaths) formatter(FormattingDsl, typedCol) else null
168-
169-
parentAttributes and (existingAttr and newAttr)
170-
}
171-
172-
return FormattedFrame(df, oldCellFormatter, composedHeader)
173-
}
142+
public fun <T, C> HeaderFormatClause<T, C>.with(formatter: HeaderColFormatter<C>): FormattedFrame<T> =
143+
formatHeaderImpl(formatter)
174144

175145
// endregion
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.api
2+
3+
import org.jetbrains.kotlinx.dataframe.api.FormattedFrame
4+
import org.jetbrains.kotlinx.dataframe.api.FormattingDsl
5+
import org.jetbrains.kotlinx.dataframe.api.HeaderColFormatter
6+
import org.jetbrains.kotlinx.dataframe.api.HeaderFormatClause
7+
import org.jetbrains.kotlinx.dataframe.api.and
8+
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
9+
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
10+
import org.jetbrains.kotlinx.dataframe.impl.getColumnPaths
11+
12+
internal fun <T, C> HeaderFormatClause<T, C>.formatHeaderImpl(formatter: HeaderColFormatter<C>): FormattedFrame<T> {
13+
val selectedPaths = df.getColumnPaths(UnresolvedColumnsPolicy.Skip, columns).toSet()
14+
val oldHeader = oldHeaderFormatter
15+
16+
val composedHeader: HeaderColFormatter<Any?> = { col ->
17+
val typedCol = col as ColumnWithPath<C>
18+
val existingAttr = oldHeader?.invoke(FormattingDsl, typedCol)
19+
val newAttr = if (col.path in selectedPaths) formatter(FormattingDsl, typedCol) else null
20+
existingAttr and newAttr
21+
}
22+
23+
return FormattedFrame(df, oldCellFormatter, composedHeader)
24+
}

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import org.jetbrains.kotlinx.dataframe.jupyter.RenderedContent
3939
import org.jetbrains.kotlinx.dataframe.name
4040
import org.jetbrains.kotlinx.dataframe.nrow
4141
import org.jetbrains.kotlinx.dataframe.size
42+
import org.jetbrains.kotlinx.dataframe.util.DISPLAY_CONFIGURATION
43+
import org.jetbrains.kotlinx.dataframe.util.DISPLAY_CONFIGURATION_COPY
4244
import org.jetbrains.kotlinx.dataframe.util.TO_HTML
4345
import org.jetbrains.kotlinx.dataframe.util.TO_HTML_REPLACE
4446
import org.jetbrains.kotlinx.dataframe.util.TO_STANDALONE_HTML
@@ -873,6 +875,67 @@ public data class DisplayConfiguration(
873875
public val DEFAULT: DisplayConfiguration = DisplayConfiguration()
874876
}
875877

878+
/** For binary compatibility. */
879+
@Deprecated(
880+
message = DISPLAY_CONFIGURATION,
881+
level = DeprecationLevel.HIDDEN,
882+
)
883+
public constructor(
884+
rowsLimit: Int? = 20,
885+
nestedRowsLimit: Int? = 5,
886+
cellContentLimit: Int = 40,
887+
cellFormatter: RowColFormatter<*, *>? = null,
888+
decimalFormat: RendererDecimalFormat = RendererDecimalFormat.DEFAULT,
889+
isolatedOutputs: Boolean = flagFromEnv("LETS_PLOT_HTML_ISOLATED_FRAME"),
890+
localTesting: Boolean = flagFromEnv("KOTLIN_DATAFRAME_LOCAL_TESTING"),
891+
useDarkColorScheme: Boolean = false,
892+
enableFallbackStaticTables: Boolean = true,
893+
downsizeBufferedImage: Boolean = true,
894+
): this (
895+
rowsLimit,
896+
nestedRowsLimit,
897+
cellContentLimit,
898+
cellFormatter,
899+
null,
900+
decimalFormat,
901+
isolatedOutputs,
902+
localTesting,
903+
useDarkColorScheme,
904+
enableFallbackStaticTables,
905+
downsizeBufferedImage
906+
)
907+
908+
/** For binary compatibility. */
909+
@Deprecated(
910+
message = DISPLAY_CONFIGURATION_COPY,
911+
level = DeprecationLevel.HIDDEN,
912+
)
913+
public fun copy(
914+
rowsLimit: Int? = this.rowsLimit,
915+
nestedRowsLimit: Int? = this.nestedRowsLimit,
916+
cellContentLimit: Int = this.cellContentLimit,
917+
cellFormatter: RowColFormatter<*, *>? = this.cellFormatter,
918+
decimalFormat: RendererDecimalFormat = this.decimalFormat,
919+
isolatedOutputs: Boolean = this.isolatedOutputs,
920+
localTesting: Boolean = this.localTesting,
921+
useDarkColorScheme: Boolean = this.useDarkColorScheme,
922+
enableFallbackStaticTables: Boolean = this.enableFallbackStaticTables,
923+
downsizeBufferedImage: Boolean = this.downsizeBufferedImage,
924+
): DisplayConfiguration =
925+
DisplayConfiguration(
926+
rowsLimit,
927+
nestedRowsLimit,
928+
cellContentLimit,
929+
cellFormatter,
930+
null,
931+
decimalFormat,
932+
isolatedOutputs,
933+
localTesting,
934+
useDarkColorScheme,
935+
enableFallbackStaticTables,
936+
downsizeBufferedImage
937+
)
938+
876939
/** DSL accessor. */
877940
public operator fun invoke(block: DisplayConfiguration.() -> Unit): DisplayConfiguration = apply(block)
878941
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ internal const val INSERT_AFTER_COL_PATH =
143143
"This `after()` overload will be removed in favor of `after { }` with Column Selection DSL. $MESSAGE_1_0"
144144
internal const val INSERT_AFTER_COL_PATH_REPLACE = "this.after { columnPath }"
145145

146+
internal const val DISPLAY_CONFIGURATION = "This constructor is only here for binary compatibility. $MESSAGE_1_0"
147+
148+
internal const val DISPLAY_CONFIGURATION_COPY = "This function is only here for binary compatibility. $MESSAGE_1_0"
149+
150+
146151
// endregion
147152

148153
// region WARNING in 1.0, ERROR in 1.1

0 commit comments

Comments
 (0)