Skip to content

Commit 14aa37a

Browse files
Return some application/vnd.plotly.v1+json data too
So that plotly-scala works from nteract
1 parent 19fe832 commit 14aa37a

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

almond/src/main/scala/plotly/Almond.scala

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package plotly
22

33
import java.lang.{Boolean => JBoolean, Double => JDouble, Integer => JInt}
44

5-
import almond.interpreter.api.OutputHandler
5+
import almond.interpreter.api.{DisplayData, OutputHandler}
66

77
import scala.util.Random
88
import plotly.element._
@@ -50,25 +50,48 @@ object Almond {
5050
</script>
5151
"""
5252

53+
Internal.initialized = true
54+
5355
publish.html(html)
5456
}
5557

5658
def plotJs(
57-
div: String,
5859
data: Seq[Trace],
59-
layout: Layout
60+
layout: Layout,
61+
div: String = ""
6062
)(implicit
6163
publish: OutputHandler
62-
): Unit = {
64+
): String = {
65+
66+
val (div0, divPart) =
67+
if (div.isEmpty) {
68+
val d = randomDiv()
69+
(d, s"""<div class="chart" id="$d"></div>""")
70+
} else
71+
(div, "")
6372

64-
val baseJs = Plotly.jsSnippet(div, data, layout)
73+
val baseJs = Plotly.jsSnippet(div0, data, layout)
74+
val json = Plotly.jsonSnippet(data, layout)
6575

6676
val js =
67-
s"""requirejs(["plotly"], function(Plotly) {
77+
s"""require('plotly', function(Plotly) {
6878
| $baseJs
6979
|});
7080
""".stripMargin
71-
publish.js(js)
81+
82+
val data0 = DisplayData(
83+
data = Map(
84+
"text/html" ->
85+
s"""$divPart
86+
|<script>$js</script>
87+
""".stripMargin,
88+
"application/vnd.plotly.v1+json" -> json
89+
)
90+
)
91+
92+
publish.display(data0)
93+
94+
div0
7295
}
7396

7497
def randomDiv() = "plot-" + math.abs(Random.nextInt().toLong)
@@ -89,18 +112,7 @@ object Almond {
89112
}
90113
}
91114

92-
val div0 =
93-
if (div.isEmpty)
94-
randomDiv()
95-
else
96-
div
97-
98-
if (div.isEmpty)
99-
publish.html(s"""<div class="chart" id="$div0"></div>""")
100-
101-
plotJs(div0, data, layout)
102-
103-
div0
115+
plotJs(data, layout)
104116
}
105117

106118
implicit class DataOps(val data: Trace) extends AnyVal {

render/jvm/src/main/scala/plotly/Plotly.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.lang.{Boolean => JBoolean, Double => JDouble, Integer => JInt}
99
import java.nio.file.Files
1010

1111
import argonaut.Argonaut._
12-
import argonaut.PrettyParams
12+
import argonaut.{Json, PrettyParams}
1313
import plotly.internals.{BetterPrinter, Properties}
1414

1515
import scala.annotation.tailrec
@@ -18,6 +18,16 @@ object Plotly {
1818

1919
private val printer = BetterPrinter(PrettyParams.nospace.copy(dropNullKeys = true))
2020

21+
def jsonSnippet(data: Seq[Trace], layout: Layout): String = {
22+
23+
val json = Json.obj(
24+
"data" -> data.toList.asJson,
25+
"layout" -> layout.asJson
26+
)
27+
28+
printer.render(json)
29+
}
30+
2131
def jsSnippet(div: String, data: Seq[Trace], layout: Layout): String = {
2232

2333
val b = new StringBuilder

0 commit comments

Comments
 (0)