Skip to content

Commit 8e596e6

Browse files
committed
Fix problem with big arrow
This commit fixes the following bug. Steps to reproduce: 1. Execute %use dataframe 2. Calculate and render some dataframe-like table which is rendered with small arrows. It will be rendered correctly 3. Re-execute cell with %use 4. Arrows in the table now become giant Reason: When you re-execute %use command, it effectively clears all the output including CSS-s. Disappearing of styles leads to different visual problems including this one. Fix: Include CSS to the output for every dataframe-like object
1 parent c44a8b8 commit 8e596e6

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,18 @@ public data class HtmlData(val style: String, val body: String, val script: Stri
182182

183183
internal fun HtmlData.print() = println(this)
184184

185-
internal fun initHtml(): HtmlData =
186-
HtmlData(style = getResources("/table.css"), script = getResourceText("/init.js"), body = "")
185+
internal fun initHtml(includeJs: Boolean = true, includeCss: Boolean = true): HtmlData =
186+
HtmlData(
187+
style = if (includeCss) getResources("/table.css") else "",
188+
script = if (includeJs) getResourceText("/init.js") else "",
189+
body = ""
190+
)
187191

188-
public fun <T> DataFrame<T>.html(): String = toHTML(includeInit = true).toString()
192+
public fun <T> DataFrame<T>.html(): String = toHTML(extraHtml = initHtml()).toString()
189193

190194
public fun <T> DataFrame<T>.toHTML(
191195
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
192-
includeInit: Boolean = false,
196+
extraHtml: HtmlData? = null,
193197
cellRenderer: CellRenderer = org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer,
194198
getFooter: (DataFrame<T>) -> String = { "DataFrame [${it.size}]" }
195199
): HtmlData {
@@ -203,7 +207,7 @@ public fun <T> DataFrame<T>.toHTML(
203207
val tableHtml = toHtmlData(configuration, cellRenderer)
204208
val html = tableHtml + HtmlData("", bodyFooter, "")
205209

206-
return if (includeInit) initHtml() + html else html
210+
return if (extraHtml != null) extraHtml + html else html
207211
}
208212

209213
public data class DisplayConfiguration(

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe.jupyter
22

33
import org.jetbrains.kotlinx.dataframe.DataFrame
44
import org.jetbrains.kotlinx.dataframe.io.DisplayConfiguration
5+
import org.jetbrains.kotlinx.dataframe.io.initHtml
56
import org.jetbrains.kotlinx.dataframe.io.toHTML
67
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
78

@@ -18,5 +19,13 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
1819
val contextRenderer = JupyterCellRenderer(this.notebook, host)
1920
val reifiedDisplayConfiguration = value.modifyConfig(display)
2021
val footer = getFooter(value)
21-
getDf(value).toHTML(reifiedDisplayConfiguration, includeInit = reifiedDisplayConfiguration.isolatedOutputs, contextRenderer, { footer }).toJupyter()
22+
getDf(value).toHTML(
23+
reifiedDisplayConfiguration,
24+
extraHtml = initHtml(
25+
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
26+
includeCss = true
27+
),
28+
contextRenderer
29+
) { footer }
30+
.toJupyter()
2231
}

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/rendering/RenderingTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class RenderingTests {
5959
@Test
6060
fun `long text is trimmed without escaping`() {
6161
val df = dataFrameOf("text")("asdfkjasdlkjfhasljkddasdasdasdasdasdasdhf")
62-
val html = df.toHTML(includeInit = false).toString()
62+
val html = df.toHTML().toString()
6363
html shouldNotContain "\\\\"
6464
html shouldNotContain "&#34;"
6565
}
@@ -68,7 +68,7 @@ class RenderingTests {
6868
fun `non ascii text`() {
6969
val value = "Шёл Шива по шоссе, сокрушая сущее"
7070
val df = dataFrameOf("text")(value)
71-
val script = df.toHTML(includeInit = false).script
71+
val script = df.toHTML().script
7272
script shouldContain value.escapeHTML()
7373
}
7474

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/rendering/html/Utils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.jetbrains.kotlinx.dataframe.rendering.html
22

33
import org.jetbrains.kotlinx.dataframe.AnyFrame
4+
import org.jetbrains.kotlinx.dataframe.io.initHtml
45
import org.jetbrains.kotlinx.dataframe.io.toHTML
56
import java.awt.Desktop
67
import java.io.File
78

89
fun AnyFrame.browse() {
910
val file = File("temp.html") // File.createTempFile("df_rendering", ".html")
10-
file.writeText(toHTML(includeInit = true).toString())
11+
file.writeText(toHTML(extraHtml = initHtml()).toString())
1112
val uri = file.toURI()
1213
val desktop = Desktop.getDesktop()
1314
desktop.browse(uri)

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/HtmlRenderingTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jetbrains.kotlinx.dataframe.api.group
88
import org.jetbrains.kotlinx.dataframe.api.into
99
import org.jetbrains.kotlinx.dataframe.api.parse
1010
import org.jetbrains.kotlinx.dataframe.io.html
11+
import org.jetbrains.kotlinx.dataframe.io.initHtml
1112
import org.jetbrains.kotlinx.dataframe.io.toHTML
1213
import org.jetbrains.kotlinx.jupyter.findNthSubstring
1314
import org.junit.Ignore
@@ -19,7 +20,7 @@ class HtmlRenderingTests : BaseTest() {
1920

2021
fun AnyFrame.browse() {
2122
val file = File("temp.html") // File.createTempFile("df_rendering", ".html")
22-
file.writeText(toHTML(includeInit = true).toString())
23+
file.writeText(toHTML(extraHtml = initHtml()).toString())
2324
val uri = file.toURI()
2425
val desktop = Desktop.getDesktop()
2526
desktop.browse(uri)

0 commit comments

Comments
 (0)