Skip to content

Commit d299abf

Browse files
Merge pull request #36 from alexarchambault/develop
Various things
2 parents 6a8f629 + 14aa37a commit d299abf

File tree

8 files changed

+265
-29
lines changed

8 files changed

+265
-29
lines changed

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

Lines changed: 32 additions & 20 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._
@@ -23,7 +23,7 @@ object Almond {
2323
s"""define('plotly', function(require, exports, module) {
2424
| ${Plotly.plotlyMinJs}
2525
|});
26-
"""
26+
""".stripMargin
2727
else
2828
"""require.config({
2929
| paths: {
@@ -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 = {
6365

64-
val baseJs = Plotly.jsSnippet(div, data, layout)
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, "")
72+
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 {

build.sbt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,33 @@ lazy val render = crossProject(JVMPlatform, JSPlatform)
4444
libraryDependencies += Deps.argonautShapeless.value
4545
)
4646
.jvmSettings(
47-
libraryDependencies += WebDeps.plotlyJs
47+
libraryDependencies ++= Seq(
48+
WebDeps.plotlyJs,
49+
Deps.scalaTest % "test"
50+
),
51+
resourceGenerators.in(Compile) += Def.task {
52+
import sys.process._
53+
54+
val dir = classDirectory.in(Compile).value / "plotly"
55+
val ver = version.value
56+
57+
val f = dir / "plotly-scala.properties"
58+
dir.mkdirs()
59+
60+
val p = new java.util.Properties
61+
62+
p.setProperty("plotly-js-version", WebDeps.Versions.plotlyJs)
63+
p.setProperty("version", ver)
64+
p.setProperty("commit-hash", Seq("git", "rev-parse", "HEAD").!!.trim)
65+
66+
val w = new java.io.FileOutputStream(f)
67+
p.store(w, "plotly-scala properties")
68+
w.close()
69+
70+
state.value.log.info(s"Wrote $f")
71+
72+
Seq(f)
73+
}
4874
)
4975
.jsSettings(
5076
libraryDependencies += Deps.scalajsDom.value

project/WebDeps.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import sbt._
33

44
object WebDeps {
55

6+
object Versions {
7+
def plotlyJs = "1.41.3"
8+
}
9+
610
def bootstrap = "org.webjars.bower" % "bootstrap" % "3.3.6"
711
def jquery = "org.webjars.bower" % "jquery" % "2.2.4"
8-
def plotlyJs = "org.webjars.bower" % "plotly.js" % "1.41.3"
12+
def plotlyJs = "org.webjars.bower" % "plotly.js" % Versions.plotlyJs
913
def prism = "org.webjars.bower" % "prism" % "1.5.0"
1014

1115
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import argonaut.Argonaut._
66
import argonaut.{Json, PrettyParams}
77
import plotly.Codecs._
88
import plotly.element.Color
9+
import plotly.internals.BetterPrinter
910
import plotly.layout._
1011

1112
import scala.scalajs.js
@@ -14,10 +15,10 @@ import scala.scalajs.js.JSON
1415

1516
object Plotly {
1617

17-
private val printer = PrettyParams.nospace.copy(dropNullKeys = true)
18+
private val printer = BetterPrinter(PrettyParams.nospace.copy(dropNullKeys = true))
1819
private def stripNulls(json: Json): js.Any = {
1920
// Remove empty objects
20-
JSON.parse(printer.pretty(json))
21+
JSON.parse(printer.render(json))
2122
}
2223

2324
def plot(div: String, data: Seq[Trace], layout: Layout): Unit = {

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ 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}
13+
import plotly.internals.{BetterPrinter, Properties}
1314

1415
import scala.annotation.tailrec
1516

1617
object Plotly {
1718

18-
private val printer = PrettyParams.nospace.copy(dropNullKeys = true)
19+
private val printer = BetterPrinter(PrettyParams.nospace.copy(dropNullKeys = true))
20+
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+
}
1930

2031
def jsSnippet(div: String, data: Seq[Trace], layout: Layout): String = {
2132

@@ -25,15 +36,15 @@ object Plotly {
2536

2637
for ((d, idx) <- data.zipWithIndex) {
2738
b ++= s" var data$idx = "
28-
b ++= printer.pretty(d.asJson)
39+
b ++= printer.render(d.asJson)
2940
b ++= ";\n"
3041
}
3142

3243
b ++= "\n "
3344
b ++= data.indices.map(idx => s"data$idx").mkString("var data = [", ", ", "];")
3445
b ++= "\n"
3546
b ++= " var layout = "
36-
b ++= printer.pretty(layout.asJson)
47+
b ++= printer.render(layout.asJson)
3748
b ++= ";\n\n Plotly.plot('"
3849
b ++= div.replaceAll("'", "\\'")
3950
b ++= "', data, layout);\n"
@@ -57,7 +68,8 @@ object Plotly {
5768
buffer.toByteArray
5869
}
5970

60-
val plotlyVersion = "1.12.0" // FIXME Get from build.sbt
71+
def plotlyVersion: String =
72+
Properties.plotlyJsVersion
6173

6274
def plotlyMinJs: String = {
6375
var is: InputStream = null
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package plotly.internals
2+
3+
import java.util.{Properties => JProperties}
4+
5+
object Properties {
6+
7+
private lazy val props = {
8+
val p = new JProperties
9+
try {
10+
p.load(
11+
getClass
12+
.getClassLoader
13+
.getResourceAsStream("plotly/plotly-scala.properties")
14+
)
15+
}
16+
catch {
17+
case _: NullPointerException =>
18+
}
19+
p
20+
}
21+
22+
lazy val plotlyJsVersion = props.getProperty("plotly-js-version")
23+
lazy val version = props.getProperty("version")
24+
lazy val commitHash = props.getProperty("commit-hash")
25+
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package plotly
2+
3+
import org.scalatest.PropSpec
4+
5+
class ResourceTests extends PropSpec {
6+
7+
property("plotly.min.js must be found in resources") {
8+
assert(Plotly.plotlyMinJs.nonEmpty)
9+
}
10+
11+
}

0 commit comments

Comments
 (0)