@@ -8,6 +8,11 @@ import org.jetbrains.kotlinx.dataframe.RowValueFilter
88import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload
99import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
1010import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
11+ import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls
12+ import org.jetbrains.kotlinx.dataframe.documentation.DslGrammarLink
13+ import org.jetbrains.kotlinx.dataframe.documentation.ExcludeFromSources
14+ import org.jetbrains.kotlinx.dataframe.documentation.LineBreak
15+ import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
1116import org.jetbrains.kotlinx.dataframe.impl.api.MergedAttributes
1217import org.jetbrains.kotlinx.dataframe.impl.api.SingleAttribute
1318import org.jetbrains.kotlinx.dataframe.impl.api.encode
@@ -22,12 +27,91 @@ import kotlin.reflect.KProperty
2227
2328// region DataFrame
2429
30+ /* *
31+ * Formats the specified [columns\] or cells within the [DataFrame] such that
32+ * they have specific attributes applied to them when rendering the dataframe to HTML.
33+ *
34+ * This function does not immediately produce a [FormattedFrame], but instead it selects the columns to be formatted
35+ * and returns a [FormatClause] which serves as an intermediate step.
36+ *
37+ * @include [SelectingColumns.ColumnGroupsAndNestedColumnsMention]
38+ *
39+ * See [Selecting Columns][FormatSelectingColumns].
40+ *
41+ * The [FormatClause] allows to further narrow down the selection to individual cells
42+ * by selecting only certain rows, using [where][FormatClause.where],
43+ * and then finally specify how to format the cells using
44+ * [with][FormatClause.with] or [perRowCol][FormatClause.perRowCol].
45+ *
46+ * Check out the [Grammar].
47+ *
48+ * For more information: {@include [DocumentationUrls.Format]}
49+ */
50+ internal interface FormatDocs {
51+
52+ /* *
53+ * {@comment Version of [SelectingColumns] with correctly filled in examples}
54+ * @include [SelectingColumns] {@include [SetFormatOperationArg]}
55+ */
56+ interface FormatSelectingColumns
57+
58+ /* *
59+ * ## Format Operation Grammar
60+ * {@include [LineBreak]}
61+ * {@include [DslGrammarLink]}
62+ * {@include [LineBreak]}
63+ *
64+ * TODO
65+ */
66+ interface Grammar
67+ }
68+
69+ /* * {@set [SelectingColumns.OPERATION] [format][format]} */
70+ @ExcludeFromSources
71+ private interface SetFormatOperationArg
72+
73+ /* *
74+ * @include [FormatDocs]
75+ * ### This Format Overload
76+ */
77+ @ExcludeFromSources
78+ private interface CommonFormatDocs
79+
2580// region format
2681
82+ /* *
83+ * @include [CommonFormatDocs]
84+ * @include [SelectingColumns.Dsl] {@include [SetFormatOperationArg]}
85+ * ### Examples:
86+ * TODO example
87+ *
88+ * @param [columns\] The [columns-selector][ColumnsSelector] used to select the columns to be formatted.
89+ * If unspecified, all columns will be formatted.
90+ */
2791public fun <T , C > DataFrame<T>.format (columns : ColumnsSelector <T , C >): FormatClause <T , C > = FormatClause (this , columns)
2892
93+ /* *
94+ * @include [CommonFormatDocs]
95+ * @include [SelectingColumns.ColumnNames] {@include [SetFormatOperationArg]}
96+ * ### Examples:
97+ * TODO example
98+ *
99+ * @param [columns\] The names of the columns to be formatted.
100+ * If unspecified, all columns will be formatted.
101+ */
29102public fun <T > DataFrame<T>.format (vararg columns : String ): FormatClause <T , Any ?> = format { columns.toColumnSet() }
30103
104+ /* *
105+ * @include [CommonFormatDocs]
106+ *
107+ * This simply formats all columns. Optionally, you can specify which columns to format using a
108+ * [columns-selector][ColumnsSelector] or by [column names][String].
109+ *
110+ * ### Examples:
111+ * TODO example
112+ */
113+ public fun <T > DataFrame<T>.format (): FormatClause <T , Any ?> = FormatClause (this )
114+
31115@Deprecated(DEPRECATED_ACCESS_API )
32116@AccessApiOverload
33117public fun <T , C > DataFrame<T>.format (vararg columns : ColumnReference <C >): FormatClause <T , C > =
@@ -38,15 +122,14 @@ public fun <T, C> DataFrame<T>.format(vararg columns: ColumnReference<C>): Forma
38122public fun <T , C > DataFrame<T>.format (vararg columns : KProperty <C >): FormatClause <T , C > =
39123 format { columns.toColumnSet() }
40124
41- public fun <T > DataFrame<T>.format (): FormatClause <T , Any ?> = FormatClause (this )
42-
43125// endregion
44126
45127public fun <T , C > FormatClause <T , C >.perRowCol (formatter : RowColFormatter <T , C >): FormattedFrame <T > =
46128 formatImpl(formatter)
47129
130+ @Suppress(" UNCHECKED_CAST" )
48131public fun <T , C > FormatClause <T , C >.with (formatter : CellFormatter <C >): FormattedFrame <T > =
49- formatImpl { row, col -> formatter(row[col] ) }
132+ formatImpl { row, col -> formatter(row[col.name] as C ) }
50133
51134public fun <T , C > FormatClause <T , C >.where (filter : RowValueFilter <T , C >): FormatClause <T , C > =
52135 FormatClause (filter = filter, df = df, columns = columns, oldFormatter = oldFormatter)
@@ -119,14 +202,19 @@ public object FormattingDSL {
119202
120203public typealias RowColFormatter <T , C > = FormattingDSL .(DataRow <T >, DataColumn <C >) -> CellAttributes ?
121204
205+ /* *
206+ * A wrapper around a [DataFrame][df] with HTML formatting data.
207+ */
122208public class FormattedFrame <T >(internal val df : DataFrame <T >, internal val formatter : RowColFormatter <T , * >? = null ) {
123209 /* *
210+ * todo copy from toHtml
124211 * @return DataFrameHtmlData without additional definitions. Can be rendered in Jupyter kernel environments
125212 */
126213 public fun toHtml (configuration : DisplayConfiguration = DisplayConfiguration .DEFAULT ): DataFrameHtmlData =
127214 df.toHtml(getDisplayConfiguration(configuration))
128215
129216 /* *
217+ * todo copy from toStandaloneHtml
130218 * @return DataFrameHtmlData with table script and css definitions. Can be saved as an *.html file and displayed in the browser
131219 */
132220 public fun toStandaloneHtml (configuration : DisplayConfiguration = DisplayConfiguration .DEFAULT ): DataFrameHtmlData =
@@ -136,6 +224,23 @@ public class FormattedFrame<T>(internal val df: DataFrame<T>, internal val forma
136224 configuration.copy(cellFormatter = formatter as RowColFormatter <* , * >? )
137225}
138226
227+ /* *
228+ * An intermediate class used in the [format] operation.
229+ *
230+ * This class itself does nothing—it is just a transitional step before specifying
231+ * how to format the selected columns.
232+ * It must be followed by one of the positioning methods
233+ * to produce a new [FormattedFrame]; a [DataFrame] with HTML formatting data.
234+ *
235+ * Use the following function to filter the rows to format:
236+ * - [where][FormatClause.where] – filters the rows to format using a [RowValueFilter].
237+ *
238+ * Use the following functions to finalize the formatting:
239+ * - [with][FormatClause.with] – Specifies how to format the cells using a [CellFormatter].
240+ * - [perRowCol][FormatClause.perRowCol] – Specifies how to format each cell individually using a [RowColFormatter].
241+ *
242+ * See [Grammar][TODO] for more details.
243+ */
139244public class FormatClause <T , C >(
140245 internal val df : DataFrame <T >,
141246 internal val columns : ColumnsSelector <T , C >? = null ,
0 commit comments