Skip to content

Commit 51e9582

Browse files
authored
Merge pull request #174 from Kotlin/notebook-dark-theme
Add a way of explicit switching to dark color scheme.
2 parents 272da20 + cffe404 commit 51e9582

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ internal fun AnyFrame.toHtmlData(
154154
}
155155
val body = getResourceText("/table.html", "ID" to rootId)
156156
val script = scripts.joinToString("\n") + "\n" + getResourceText("/renderTable.js", "___ID___" to rootId)
157-
return HtmlData("", body, script)
157+
return HtmlData("", body, script, configuration.useDarkColorScheme)
158158
}
159159

160-
public data class HtmlData(val style: String, val body: String, val script: String) {
160+
public data class HtmlData(val style: String, val body: String, val script: String, val useDarkColorScheme: Boolean) {
161161
override fun toString(): String = """
162162
<html>
163163
<head>
@@ -166,27 +166,39 @@ public data class HtmlData(val style: String, val body: String, val script: Stri
166166
</style>
167167
</head>
168168
<body>
169-
$body
169+
<div${darkSchemeAttribute()}>
170+
$body
171+
</div>
170172
</body>
171173
<script>
172174
$script
173175
</script>
174176
</html>
175177
""".trimIndent()
176178

179+
private fun darkSchemeAttribute(): String {
180+
return if (useDarkColorScheme) " class=\"dataframe_dark\"" else ""
181+
}
182+
177183
public fun toJupyter(): MimeTypedResult = HTML(toString())
178184

179185
public operator fun plus(other: HtmlData): HtmlData =
180-
HtmlData(style + "\n" + other.style, body + "\n" + other.body, script + "\n" + other.script)
186+
HtmlData(
187+
style + "\n" + other.style,
188+
body + "\n" + other.body,
189+
script + "\n" + other.script,
190+
useDarkColorScheme || other.useDarkColorScheme
191+
)
181192
}
182193

183194
internal fun HtmlData.print() = println(this)
184195

185-
internal fun initHtml(includeJs: Boolean = true, includeCss: Boolean = true): HtmlData =
196+
internal fun initHtml(includeJs: Boolean = true, includeCss: Boolean = true, useDarkColorScheme: Boolean = false): HtmlData =
186197
HtmlData(
187198
style = if (includeCss) getResources("/table.css") else "",
188199
script = if (includeJs) getResourceText("/init.js") else "",
189-
body = ""
200+
body = "",
201+
useDarkColorScheme = useDarkColorScheme,
190202
)
191203

192204
public fun <T> DataFrame<T>.html(): String = toHTML(extraHtml = initHtml()).toString()
@@ -205,7 +217,7 @@ public fun <T> DataFrame<T>.toHTML(
205217
} else "<p>$footer</p>"
206218

207219
val tableHtml = toHtmlData(configuration, cellRenderer)
208-
val html = tableHtml + HtmlData("", bodyFooter, "")
220+
val html = tableHtml + HtmlData("", bodyFooter, "", configuration.useDarkColorScheme)
209221

210222
return if (extraHtml != null) extraHtml + html else html
211223
}
@@ -217,6 +229,7 @@ public data class DisplayConfiguration(
217229
var decimalFormat: RendererDecimalFormat = RendererDecimalFormat.DEFAULT,
218230
var isolatedOutputs: Boolean = flagFromEnv("LETS_PLOT_HTML_ISOLATED_FRAME"),
219231
internal val localTesting: Boolean = flagFromEnv("KOTLIN_DATAFRAME_LOCAL_TESTING"),
232+
var useDarkColorScheme: Boolean = false,
220233
) {
221234
public companion object {
222235
public val DEFAULT: DisplayConfiguration = DisplayConfiguration()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
2323
reifiedDisplayConfiguration,
2424
extraHtml = initHtml(
2525
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
26-
includeCss = true
26+
includeCss = true,
27+
useDarkColorScheme = reifiedDisplayConfiguration.useDarkColorScheme
2728
),
2829
contextRenderer
2930
) { footer }

core/src/main/resources/table.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
--link-hover: #1a466c;
1515
}
1616

17-
:root[theme="dark"], :root [data-jp-theme-light="false"]{
17+
:root[theme="dark"], :root [data-jp-theme-light="false"], .dataframe_dark{
1818
--background: #303030;
1919
--background-odd: #3c3c3c;
2020
--background-hover: #464646;

0 commit comments

Comments
 (0)