Skip to content

Commit 3e04d2f

Browse files
committed
format kdocs
1 parent 4d4a73f commit 3e04d2f

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

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

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import org.jetbrains.kotlinx.dataframe.RowValueFilter
88
import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload
99
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
1010
import 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
1116
import org.jetbrains.kotlinx.dataframe.impl.api.MergedAttributes
1217
import org.jetbrains.kotlinx.dataframe.impl.api.SingleAttribute
1318
import 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+
*/
2791
public 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+
*/
29102
public 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
33117
public 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
38122
public 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

45127
public fun <T, C> FormatClause<T, C>.perRowCol(formatter: RowColFormatter<T, C>): FormattedFrame<T> =
46128
formatImpl(formatter)
47129

130+
@Suppress("UNCHECKED_CAST")
48131
public 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

51134
public 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

120203
public typealias RowColFormatter<T, C> = FormattingDSL.(DataRow<T>, DataColumn<C>) -> CellAttributes?
121204

205+
/**
206+
* A wrapper around a [DataFrame][df] with HTML formatting data.
207+
*/
122208
public 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+
*/
139244
public class FormatClause<T, C>(
140245
internal val df: DataFrame<T>,
141246
internal val columns: ColumnsSelector<T, C>? = null,

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/documentation/DocumentationUrls.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ internal interface DocumentationUrls {
6262
interface FillNA
6363
}
6464

65+
/** [See `format` on the documentation website.]({@include [Url]}/format.html) */
66+
interface Format
67+
6568
/** [See `NaN` and `NA` on the documentation website.]({@include [Url]}/nanAndNa.html) */
6669
interface NanAndNa {
6770

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal inline fun <T, C> FormatClause<T, C>.formatImpl(
6262
return FormattedFrame(df) { row, col ->
6363
val oldAttributes = oldFormatter?.invoke(FormattingDSL, row, col.cast())
6464
if (columns == null || columns.contains(col.name())) {
65-
val value = row[col] as C
65+
val value = row[col.name] as C
6666
if (filter == null || filter(row, value)) {
6767
oldAttributes and formatter(FormattingDSL, row.cast(), col.cast())
6868
} else {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.jetbrains.kotlinx.dataframe.api.FormattingDSL
1010
import org.jetbrains.kotlinx.dataframe.api.RowColFormatter
1111
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
1212
import org.jetbrains.kotlinx.dataframe.api.asNumbers
13+
import org.jetbrains.kotlinx.dataframe.api.format
1314
import org.jetbrains.kotlinx.dataframe.api.getColumnsWithPaths
1415
import org.jetbrains.kotlinx.dataframe.api.isColumnGroup
1516
import org.jetbrains.kotlinx.dataframe.api.isEmpty
@@ -546,6 +547,9 @@ public fun <T> DataFrame<T>.toStandaloneHTML(
546547
* By default, cell content is formatted as text
547548
* Use [RenderedContent.media] or [IMG], [IFRAME] if you need custom HTML inside a cell.
548549
*
550+
* To change the formatting of certain cells or columns in the dataframe,
551+
* use [DataFrame.format].
552+
*
549553
* The [DataFrameHtmlData] be saved as an *.html file and displayed in the browser.
550554
* If you save it as a file and find it in the project tree,
551555
* the ["Open in browser"](https://www.jetbrains.com/help/idea/editing-html-files.html#ws_html_preview_output_procedure) feature of IntelliJ IDEA will automatically reload the file content when it's updated
@@ -560,6 +564,10 @@ public fun <T> DataFrame<T>.toStandaloneHtml(
560564
/**
561565
* By default, cell content is formatted as text
562566
* Use [RenderedContent.media] or [IMG], [IFRAME] if you need custom HTML inside a cell.
567+
*
568+
* To change the formatting of certain cells or columns in the dataframe,
569+
* use [DataFrame.format].
570+
*
563571
* @return DataFrameHtmlData without additional definitions. Can be rendered in Jupyter kernel environments
564572
*/
565573
public fun <T> DataFrame<T>.toHtml(

0 commit comments

Comments
 (0)