Skip to content

Commit 108728d

Browse files
committed
Add CellKind and handle DataFrameConvertable cells
Implemented the `CellKind` enum with a `DataFrameConvertable` type. Updated JSON writing logic to handle `DataFrameConvertable` cells and added utility function to check if an object is convertible to DataFrame.
1 parent 76c002c commit 108728d

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.jetbrains.kotlinx.dataframe.columns
2+
3+
public enum class CellKind {
4+
DataFrameConvertable {
5+
override fun toString(): String = "DataFrameConvertable"
6+
},
7+
}

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/io/writeJson.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.jetbrains.kotlinx.dataframe.api.isList
2222
import org.jetbrains.kotlinx.dataframe.api.rows
2323
import org.jetbrains.kotlinx.dataframe.api.schema
2424
import org.jetbrains.kotlinx.dataframe.api.take
25+
import org.jetbrains.kotlinx.dataframe.columns.CellKind
2526
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
2627
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
2728
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
@@ -38,6 +39,8 @@ import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.VERSION
3839
import org.jetbrains.kotlinx.dataframe.io.ARRAY_COLUMN_NAME
3940
import org.jetbrains.kotlinx.dataframe.io.Base64ImageEncodingOptions
4041
import org.jetbrains.kotlinx.dataframe.io.VALUE_COLUMN_NAME
42+
import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils
43+
import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils.isDataframeConvertable
4144
import org.jetbrains.kotlinx.dataframe.name
4245
import org.jetbrains.kotlinx.dataframe.ncol
4346
import org.jetbrains.kotlinx.dataframe.nrow
@@ -168,6 +171,22 @@ internal fun encodeValue(
168171
imageEncodingOptions: Base64ImageEncodingOptions? = null,
169172
): JsonElement =
170173
when {
174+
isDataframeConvertable(col[index]) -> if (col[index] == null) {
175+
JsonPrimitive(null)
176+
} else {
177+
val data = encodeFrameWithMetadata(
178+
KotlinNotebookPluginUtils.convertToDataFrame(col[index]!!),
179+
null,
180+
imageEncodingOptions,
181+
)
182+
buildJsonObject {
183+
put(DATA, data)
184+
putJsonObject(METADATA) {
185+
put(KIND, JsonPrimitive(CellKind.DataFrameConvertable.toString()))
186+
}
187+
}
188+
}
189+
171190
col.isList() -> col[index]?.let { list ->
172191
val values = (list as List<*>).map { convert(it) }
173192
JsonArray(values)

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/KotlinNotebookPluginUtils.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,36 @@ public object KotlinNotebookPluginUtils {
101101
}.toColumnSet()
102102
}
103103

104+
internal fun isDataframeConvertable(dataframeLike: Any?): Boolean =
105+
when (dataframeLike) {
106+
is Pivot<*>,
107+
is ReducedGroupBy<*, *>,
108+
is ReducedPivot<*>,
109+
is PivotGroupBy<*>,
110+
is ReducedPivotGroupBy<*>,
111+
is SplitWithTransform<*, *, *>,
112+
is Split<*, *>,
113+
is Merge<*, *, *>,
114+
is Gather<*, *, *, *>,
115+
is Update<*, *>,
116+
is Convert<*, *>,
117+
is FormattedFrame<*>,
118+
is AnyCol,
119+
is AnyRow,
120+
is GroupBy<*, *>,
121+
is AnyFrame,
122+
is DisableRowsLimitWrapper,
123+
is MoveClause<*, *>,
124+
is RenameClause<*, *>,
125+
is ReplaceClause<*, *>,
126+
is GroupClause<*, *>,
127+
is InsertClause<*>,
128+
is FormatClause<*, *>,
129+
-> true
130+
131+
else -> false
132+
}
133+
104134
/**
105135
* Converts [dataframeLike] to [AnyFrame].
106136
* If [dataframeLike] is already [AnyFrame] then it is returned as is.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.jetbrains.kotlinx.dataframe.columns
2+
3+
public enum class CellKind {
4+
DataFrameConvertable {
5+
override fun toString(): String = "DataFrameConvertable"
6+
},
7+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.jetbrains.kotlinx.dataframe.api.isList
2222
import org.jetbrains.kotlinx.dataframe.api.rows
2323
import org.jetbrains.kotlinx.dataframe.api.schema
2424
import org.jetbrains.kotlinx.dataframe.api.take
25+
import org.jetbrains.kotlinx.dataframe.columns.CellKind
2526
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
2627
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
2728
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
@@ -38,6 +39,8 @@ import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.VERSION
3839
import org.jetbrains.kotlinx.dataframe.io.ARRAY_COLUMN_NAME
3940
import org.jetbrains.kotlinx.dataframe.io.Base64ImageEncodingOptions
4041
import org.jetbrains.kotlinx.dataframe.io.VALUE_COLUMN_NAME
42+
import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils
43+
import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils.isDataframeConvertable
4144
import org.jetbrains.kotlinx.dataframe.name
4245
import org.jetbrains.kotlinx.dataframe.ncol
4346
import org.jetbrains.kotlinx.dataframe.nrow
@@ -168,6 +171,22 @@ internal fun encodeValue(
168171
imageEncodingOptions: Base64ImageEncodingOptions? = null,
169172
): JsonElement =
170173
when {
174+
isDataframeConvertable(col[index]) -> if (col[index] == null) {
175+
JsonPrimitive(null)
176+
} else {
177+
val data = encodeFrameWithMetadata(
178+
KotlinNotebookPluginUtils.convertToDataFrame(col[index]!!),
179+
null,
180+
imageEncodingOptions,
181+
)
182+
buildJsonObject {
183+
put(DATA, data)
184+
putJsonObject(METADATA) {
185+
put(KIND, JsonPrimitive(CellKind.DataFrameConvertable.toString()))
186+
}
187+
}
188+
}
189+
171190
col.isList() -> col[index]?.let { list ->
172191
val values = (list as List<*>).map { convert(it) }
173192
JsonArray(values)

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/KotlinNotebookPluginUtils.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,36 @@ public object KotlinNotebookPluginUtils {
101101
}.toColumnSet()
102102
}
103103

104+
internal fun isDataframeConvertable(dataframeLike: Any?): Boolean =
105+
when (dataframeLike) {
106+
is Pivot<*>,
107+
is ReducedGroupBy<*, *>,
108+
is ReducedPivot<*>,
109+
is PivotGroupBy<*>,
110+
is ReducedPivotGroupBy<*>,
111+
is SplitWithTransform<*, *, *>,
112+
is Split<*, *>,
113+
is Merge<*, *, *>,
114+
is Gather<*, *, *, *>,
115+
is Update<*, *>,
116+
is Convert<*, *>,
117+
is FormattedFrame<*>,
118+
is AnyCol,
119+
is AnyRow,
120+
is GroupBy<*, *>,
121+
is AnyFrame,
122+
is DisableRowsLimitWrapper,
123+
is MoveClause<*, *>,
124+
is RenameClause<*, *>,
125+
is ReplaceClause<*, *>,
126+
is GroupClause<*, *>,
127+
is InsertClause<*>,
128+
is FormatClause<*, *>,
129+
-> true
130+
131+
else -> false
132+
}
133+
104134
/**
105135
* Converts [dataframeLike] to [AnyFrame].
106136
* If [dataframeLike] is already [AnyFrame] then it is returned as is.

0 commit comments

Comments
 (0)