-
Notifications
You must be signed in to change notification settings - Fork 75
Format header #1459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndreiKingsley
wants to merge
19
commits into
master
Choose a base branch
from
formatHeader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Format header #1459
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cac1b7f
(Junie) add formatHeader
AndreiKingsley 30059fb
formatting
AndreiKingsley 92eae1d
api dump
AndreiKingsley 80b15fd
Merge branch 'master' into formatHeader
AndreiKingsley 5b9e1a9
disable :samples module
AndreiKingsley 1f08fbc
disable :samples module
AndreiKingsley b74d091
remove formatHeader overloads
AndreiKingsley 029a71c
remove formatHeader overloads
AndreiKingsley 40caf4e
formatHeader kdocs
AndreiKingsley c32e9b8
formatHeader ktlint format
AndreiKingsley 0658670
api update
AndreiKingsley 8a76753
enable samples
AndreiKingsley e103160
formatHeader docs
AndreiKingsley 7843040
formatHeader docs korro and sample
AndreiKingsley 967f611
ktlint format
AndreiKingsley 3e9f7a7
formatHeader KDocs fix
AndreiKingsley 2c50b78
formatHeader fixes
AndreiKingsley 133850f
ktlint format
AndreiKingsley a533a76
dump api
AndreiKingsley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/formatHeader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package org.jetbrains.kotlinx.dataframe.api | ||
|
||
import org.jetbrains.kotlinx.dataframe.ColumnsSelector | ||
import org.jetbrains.kotlinx.dataframe.DataFrame | ||
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath | ||
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet | ||
import org.jetbrains.kotlinx.dataframe.impl.api.formatHeaderImpl | ||
|
||
// region docs | ||
|
||
/** | ||
* A lambda used to format a column header (its displayed name) when rendering a dataframe to HTML. | ||
* | ||
* The lambda runs in the context of [FormattingDsl] and receives the [ColumnWithPath] of the header to format. | ||
* Return a [CellAttributes] (or `null`) describing the CSS you want to apply to the header cell. | ||
* | ||
* Examples: | ||
* - Center the header: `attr("text-align", "center")` | ||
* - Make it bold: `bold` | ||
* - Set a custom color: `textColor(rgb(10, 10, 10))` | ||
*/ | ||
public typealias HeaderColFormatter<C> = FormattingDsl.(col: ColumnWithPath<C>) -> CellAttributes? | ||
|
||
/** | ||
* An intermediate class used in the header-format operation [formatHeader]. | ||
* | ||
* This class itself does nothing—it represents a selection of columns whose headers will be formatted. | ||
* Finalize this step by calling [with] to produce a new [FormattedFrame]. | ||
* | ||
* Header formatting is additive and supports nested column groups: styles specified for a parent group | ||
* are inherited by its child columns unless overridden for the child. | ||
*/ | ||
public class HeaderFormatClause<T, C>( | ||
internal val df: DataFrame<T>, | ||
internal val columns: ColumnsSelector<T, C> = { all().cast() }, | ||
internal val oldHeaderFormatter: HeaderColFormatter<C>? = null, | ||
internal val oldCellFormatter: RowColFormatter<T, *>? = null, | ||
) { | ||
override fun toString(): String = | ||
"HeaderFormatClause(df=$df, columns=$columns, oldHeaderFormatter=$oldHeaderFormatter, oldCellFormatter=$oldCellFormatter)" | ||
} | ||
|
||
// endregion | ||
|
||
// region DataFrame.formatHeader | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Selects [columns] whose headers should be formatted. If unspecified, all columns will be formatted. | ||
* | ||
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] | ||
* which must be finalized using [HeaderFormatClause.with]. | ||
*/ | ||
public fun <T, C> DataFrame<T>.formatHeader(columns: ColumnsSelector<T, C>): HeaderFormatClause<T, C> = | ||
HeaderFormatClause(this, columns) | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Selects [columns] whose headers should be formatted. | ||
* | ||
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] | ||
* which must be finalized using [HeaderFormatClause.with]. | ||
*/ | ||
public fun <T> DataFrame<T>.formatHeader(vararg columns: String): HeaderFormatClause<T, Any?> = | ||
formatHeader { columns.toColumnSet() } | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Selects all columns for header formatting. | ||
* | ||
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] | ||
* which must be finalized using [HeaderFormatClause.with]. | ||
*/ | ||
public fun <T> DataFrame<T>.formatHeader(): HeaderFormatClause<T, Any?> = HeaderFormatClause(this) | ||
|
||
// endregion | ||
|
||
// region FormattedFrame.formatHeader | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Selects [columns] whose headers should be formatted. | ||
* | ||
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] | ||
* which must be finalized using [HeaderFormatClause.with]. | ||
*/ | ||
public fun <T, C> FormattedFrame<T>.formatHeader(columns: ColumnsSelector<T, C>): HeaderFormatClause<T, C> = | ||
HeaderFormatClause( | ||
df = df, | ||
columns = columns, | ||
oldHeaderFormatter = headerFormatter as HeaderColFormatter<C>?, | ||
oldCellFormatter = formatter, | ||
) | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Selects [columns] whose headers should be formatted. | ||
* | ||
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] | ||
* which must be finalized using [HeaderFormatClause.with]. | ||
*/ | ||
public fun <T> FormattedFrame<T>.formatHeader(vararg columns: String): HeaderFormatClause<T, Any?> = | ||
formatHeader { columns.toColumnSet() } | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Selects all columns for header formatting. | ||
* | ||
* This does not immediately produce a [FormattedFrame]; instead it returns a [HeaderFormatClause] | ||
* which must be finalized using [HeaderFormatClause.with]. | ||
*/ | ||
public fun <T> FormattedFrame<T>.formatHeader(): HeaderFormatClause<T, Any?> = | ||
HeaderFormatClause( | ||
df = df, | ||
oldHeaderFormatter = headerFormatter, | ||
oldCellFormatter = formatter, | ||
) | ||
|
||
// endregion | ||
|
||
// region terminal operations | ||
|
||
/** | ||
* **Experimental API. It may be changed in the future.** | ||
* | ||
* Creates a new [FormattedFrame] that uses the specified [HeaderColFormatter] to format the selected headers. | ||
* | ||
* Header formatting is additive: attributes from already-applied header formatters are combined with the newly | ||
* | ||
* returned attributes using [CellAttributes.and]. If a parent column group is selected, its attributes are | ||
* applied to its children unless explicitly overridden. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
public fun <T, C> HeaderFormatClause<T, C>.with(formatter: HeaderColFormatter<C>): FormattedFrame<T> = | ||
formatHeaderImpl(formatter) | ||
|
||
// endregion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/formatHeader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.jetbrains.kotlinx.dataframe.impl.api | ||
|
||
import org.jetbrains.kotlinx.dataframe.api.FormattedFrame | ||
import org.jetbrains.kotlinx.dataframe.api.FormattingDsl | ||
import org.jetbrains.kotlinx.dataframe.api.HeaderColFormatter | ||
import org.jetbrains.kotlinx.dataframe.api.HeaderFormatClause | ||
import org.jetbrains.kotlinx.dataframe.api.and | ||
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath | ||
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy | ||
import org.jetbrains.kotlinx.dataframe.impl.getColumnPaths | ||
|
||
internal fun <T, C> HeaderFormatClause<T, C>.formatHeaderImpl(formatter: HeaderColFormatter<C>): FormattedFrame<T> { | ||
val selectedPaths = df.getColumnPaths(UnresolvedColumnsPolicy.Skip, columns).toSet() | ||
val oldHeader = oldHeaderFormatter | ||
|
||
val composedHeader: HeaderColFormatter<Any?> = { col -> | ||
val typedCol = col as ColumnWithPath<C> | ||
val existingAttr = oldHeader?.invoke(FormattingDsl, typedCol) | ||
val newAttr = if (col.path in selectedPaths) formatter(FormattingDsl, typedCol) else null | ||
existingAttr and newAttr | ||
} | ||
|
||
return FormattedFrame(df, oldCellFormatter, composedHeader) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to introduce an Experimental OptIn for this API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe kdoc note is enough.