-
Notifications
You must be signed in to change notification settings - Fork 73
Plugin example #1241
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
Plugin example #1241
Changes from 5 commits
5f101e9
741f7bc
084f563
43dc1b4
c5fba7c
1ab848f
fbb39ce
55101c1
d99d3da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
# Kotlin DataFrame Compiler Plugin | ||
|
||
Kotlin DataFrame compiler plugin: available in Gradle projects, is coming to Kotlin Notebook and Maven projects soon. | ||
<web-summary> | ||
Explore the Kotlin DataFrame Compiler Plugin — | ||
a powerful tool for on-the-fly generating type-safe accessors in DataFrames. | ||
</web-summary> | ||
|
||
Check out this video that shows how expressions update the schema of a dataframe: | ||
<card-summary> | ||
Explore the Kotlin DataFrame Compiler Plugin — | ||
a powerful tool for on-the-fly generating type-safe accessors in DataFrames. | ||
</card-summary> | ||
|
||
<link-summary> | ||
Explore the Kotlin DataFrame Compiler Plugin — | ||
a powerful tool for on-the-fly generating type-safe accessors in DataFrames. | ||
</link-summary> | ||
|
||
|
||
> Now available in Gradle projects, is coming to Kotlin Notebook and Maven projects soon. | ||
AndreiKingsley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
**Kotlin DataFrame Compiler Plugin** is a Kotlin compiler plugin that automatically generates | ||
**[type-safe extension properties](extensionPropertiesApi.md)** for your DataFrame, | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
allowing you to access columns and rows in a type-safe way and avoid mistakes in column names. | ||
AndreiKingsley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Why use it? | ||
|
||
- Access columns as regular properties: `df.name` instead of `df["name"]`. | ||
- Get full IDE and compiler support: autocompletion, refactoring, and type checking. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uhm, maybe scrap "refactoring" for now. That does not work yet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But will be :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope so :) |
||
- Improve code readability and safety when working with DataFrame. | ||
|
||
Check out this video that shows how expressions update the schema of a dataframe: | ||
|
||
<video src="compiler_plugin.mp4" controls=""/> | ||
|
||
|
@@ -113,3 +138,9 @@ fun main() { | |
``` | ||
|
||
[Learn more](dataSchema.md) about data schema declarations | ||
|
||
## Examples | ||
|
||
* [Kotlin DataFrame in the IntelliJ IDEA project example](https://github.com/Kotlin/dataframe/blob/master/examples/kotlin-dataframe-plugin-example) | ||
— an IntelliJ IDEA project showcasing simple DataFrame expressions using the Compiler Plugin. | ||
* [](compilerPluginExamples.md) — few examples of Compiler Plugin usages. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Kotlin DataFrame Compiler Plugin Example | ||
|
||
An IntelliJ IDEA project demonstrating the use of the | ||
[Kotlin DataFrame Compiler Plugin](https://kotlin.github.io/dataframe/compiler-plugin.html). | ||
|
||
> **Note:** This project uses **dev versions** of the Kotlin and the Kotlin DataFrame plugin. | ||
> See [build.gradle.kts](build.gradle.kts) for details. | ||
|
||
For proper functionality in IntelliJ IDEA requires version 2025.2 or higher | ||
(Currently available only in [IntelliJ IDEA EAP](https://www.jetbrains.com/idea/nextversion/?_gl=1*1ojxffu*_gcl_au*MTk5NzUwODYzOS4xNzQ2NzkxMDMz*_ga*MTE0ODQ1MzY3OS4xNzM4OTY1NzM3*_ga_9J976DJZ68*czE3NDkyMTM4MzkkbzE5OCRnMSR0MTc0OTIxMzg0MSRqNTgkbDAkaDA.) | ||
). | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
kotlin("jvm") version "2.2.20-dev-3524" | ||
kotlin("plugin.dataframe") version "2.2.20-dev-3524" | ||
} | ||
|
||
group = "org.example" | ||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven("https://packages.jetbrains.team/maven/p/kt/dev/") | ||
mavenCentral() | ||
} | ||
|
||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
dependencies { | ||
implementation("org.jetbrains.kotlinx:dataframe:1.0.0-Beta2") | ||
testImplementation(kotlin("test")) | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
kotlin { | ||
jvmToolchain(11) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
kotlin.code.style=official | ||
kotlin.incremental=false | ||
AndreiKingsley marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pluginManagement { | ||
repositories { | ||
maven("https://packages.jetbrains.team/maven/p/kt/dev/") | ||
mavenCentral() | ||
gradlePluginPortal() | ||
} | ||
} | ||
plugins { | ||
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" | ||
} | ||
rootProject.name = "kdf_plugin_example" | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.jetbrains.kotlinx.dataframe.examples.plugin | ||
|
||
import org.jetbrains.kotlinx.dataframe.DataFrame | ||
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName | ||
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema | ||
import org.jetbrains.kotlinx.dataframe.api.* | ||
import org.jetbrains.kotlinx.dataframe.io.readCsv | ||
import org.jetbrains.kotlinx.dataframe.io.writeCsv | ||
import java.net.URL | ||
|
||
/* | ||
* Declare data schema for the DataFrame from jetbrains_repositories.csv. | ||
*/ | ||
@DataSchema | ||
data class Repositories( | ||
@ColumnName("full_name") | ||
val fullName: String, | ||
@ColumnName("html_url") | ||
val htmlUrl: URL, | ||
@ColumnName("stargazers_count") | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val stargazersCount: Int, | ||
val topics: String, | ||
val watchers: Int | ||
) | ||
|
||
/* | ||
* Define kinds of repositories: | ||
*/ | ||
enum class RepoKind { | ||
Kotlin, IntelliJ, Other | ||
} | ||
|
||
/* | ||
* A rule for determining the kind of repository based on its name and topics. | ||
*/ | ||
fun getKind(fullName: String, topics: List<String>): RepoKind { | ||
fun checkContains(name: String) = name in topics || fullName.lowercase().contains(name) | ||
|
||
return when { | ||
checkContains("kotlin") -> RepoKind.Kotlin | ||
checkContains("idea") || checkContains("intellij") -> RepoKind.IntelliJ | ||
else -> RepoKind.Other | ||
} | ||
} | ||
|
||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fun main() { | ||
val repos = DataFrame | ||
// Read DataFrame from the CSV file. | ||
.readCsv("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains_repositories.csv") | ||
// And convert it to match the `Repositories` schema. | ||
.convertTo<Repositories>() | ||
|
||
// Rename columns to CamelCase | ||
val reposUpdated = repos.renameToCamelCase() | ||
// Convert values in the "topic" column (which were `String` initially) | ||
// to the list of topics. | ||
.convert { topics }.with { | ||
val inner = it.removePrefix("[").removeSuffix("]") | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (inner.isEmpty()) emptyList() else inner.split(',').map(String::trim) | ||
} | ||
// Filter rows, keeping only repos with more than 150 stars. | ||
.filter { it.stargazersCount > 150 } | ||
// Add a new column with the number of topics. | ||
.add("topicCount") { topics.size } | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Add a new column with the kind of repository. | ||
.add("kind") { getKind(fullName, topics) } | ||
|
||
// Write an updated DataFrame to a CSV file. | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reposUpdated.writeCsv("jetbrains_repositories_new.csv") | ||
|
||
// TODO: Add Kandy Plot | ||
/*reposUpdated.groupBy { kind }.max { stargazersCount }.plot { | ||
bars { | ||
x(kind) | ||
y(stargazersCount) | ||
} | ||
}*/ | ||
} |
Uh oh!
There was an error while loading. Please reload this page.