Magics to Access the JVM Kernels from Python

BeakerX has magics for Python so you can run cells in the other languages. The first few cells below show how complete the implementation is with Groovy, then we have just one cell in each other language.

There are also Polyglot Magics magics for accessing Python from the JVM.

You can communicate between languages with Autotranslation.

Groovy


In [ ]:
%%groovy
println("stdout works")
f = {it + " work"}
f("results")

In [ ]:
%%groovy
new Plot(title:"plots work", initHeight: 200)

In [ ]:
%%groovy
[a:"tables", b:"work"]

In [ ]:
%%groovy
"errors work"/1

In [ ]:
%%groovy
HTML("<h1>HTML works</h1>")

In [ ]:
%%groovy
def p = new Plot(title : 'Plots Work', xLabel: 'Horizontal', yLabel: 'Vertical');
p << new Line(x: [0, 1, 2, 3, 4, 5], y: [0, 1, 6, 5, 2, 8])

Java


In [ ]:
%%java
import java.util.List;
import com.twosigma.beakerx.chart.xychart.Plot;
import java.util.Arrays;

Plot p = new Plot();

p.setTitle("Java Works");
p.setXLabel("Horizontal");
p.setYLabel("Vertical");

Bars b = new Bars();

List<Object> x = Arrays.asList(0, 1, 2, 3, 4, 5);
List<Number> y = Arrays.asList(0, 1, 6, 5, 2, 8);
Line line = new Line();
line.setX(x);
line.setY(y);
p.add(line);
  
return p;

Scala


In [ ]:
%%scala
val plot = new Plot { title = "Scala Works"; xLabel="Horizontal"; yLabel="Vertical" }
val line = new Line {x = Seq(0, 1, 2, 3, 4, 5); y = Seq(0, 1, 6, 5, 2, 8)}
plot.add(line)

Kotlin


In [ ]:
%%kotlin
val x: MutableList<Any> = mutableListOf(0, 1, 2, 3, 4, 5)
val y: MutableList<Number> = mutableListOf(0, 1, 6, 5, 2, 8)
val line = Line()
line.setX(x)
line.setY(y)

val plot = Plot()
plot.setTitle("Kotlin Works")
plot.setXLabel("Horizontal")
plot.setYLabel("Vertical")
plot.add(line)
plot

Clojure


In [ ]:
%%clojure
(import '[com.twosigma.beakerx.chart.xychart Plot]
        '[com.twosigma.beakerx.chart.xychart.plotitem Line])
(doto (Plot.)
            (.setTitle "Clojure Works")
            (.setXLabel "Horizontal")
            (.setYLabel "Vertical")
            (.add (doto (Line.)
                        (.setX [0, 1, 2, 3, 4, 5])
                        (.setY [0, 1, 6, 5, 2, 8]))))

SQL


In [ ]:
%%sql
%defaultDatasource jdbc:h2:mem:db
DROP TABLE IF EXISTS cities;
CREATE TABLE cities(
    zip_code varchar(5),
    latitude float,
    longitude float,
    city varchar(100),
    state varchar(2),
    county varchar(100),
    PRIMARY KEY (zip_code),
) AS SELECT
    zip_code,
    latitude,
    longitude,
    city,
    state,
    county
FROM CSVREAD('../resources/data/UScity.csv')

In [ ]:
%%sql
SELECT * FROM cities WHERE state = 'NY'