#!/usr/bin/env python # coding: utf-8 # # 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](../groovy/PolyglotMagic.ipynb) magics for accessing Python from the JVM. # # Fleshing out the polyglot support in BeakerX with communication between languages ([#5039](https://github.com/twosigma/beakerx/issues/5039)) is on the agenda. # # ## Groovy # In[ ]: get_ipython().run_cell_magic('groovy', '', 'println("stdout works")\nf = {it + " work"}\nf("results")\n') # In[ ]: get_ipython().run_cell_magic('groovy', '', 'new Plot(title:"plots work", initHeight: 200)\n') # In[ ]: get_ipython().run_cell_magic('groovy', '', '[a:"tables", b:"work"]\n') # In[ ]: get_ipython().run_cell_magic('groovy', '', '"errors work"/1\n') # In[ ]: get_ipython().run_cell_magic('groovy', '', 'HTML("

HTML works

")\n') # In[ ]: get_ipython().run_cell_magic('groovy', '', "def p = new Plot(title : 'Plots Work', xLabel: 'Horizontal', yLabel: 'Vertical');\np << new Line(x: [0, 1, 2, 3, 4, 5], y: [0, 1, 6, 5, 2, 8])\n") # ## Java # In[ ]: get_ipython().run_cell_magic('java', '', 'import java.util.List;\nimport com.twosigma.beakerx.chart.xychart.Plot;\nimport java.util.Arrays;\n\nPlot p = new Plot();\n\np.setTitle("Java Works");\np.setXLabel("Horizontal");\np.setYLabel("Vertical");\n\nBars b = new Bars();\n\nList x = Arrays.asList(0, 1, 2, 3, 4, 5);\nList y = Arrays.asList(0, 1, 6, 5, 2, 8);\nLine line = new Line();\nline.setX(x);\nline.setY(y);\np.add(line);\n \nreturn p;\n') # ## Scala # In[ ]: get_ipython().run_cell_magic('scala', '', 'val plot = new Plot { title = "Scala Works"; xLabel="Horizontal"; yLabel="Vertical" }\nval line = new Line {x = Seq(0, 1, 2, 3, 4, 5); y = Seq(0, 1, 6, 5, 2, 8)}\nplot.add(line)\n') # ## Kotlin # In[ ]: get_ipython().run_cell_magic('kotlin', '', 'val x: MutableList = mutableListOf(0, 1, 2, 3, 4, 5)\nval y: MutableList = mutableListOf(0, 1, 6, 5, 2, 8)\nval line = Line()\nline.setX(x)\nline.setY(y)\n\nval plot = Plot()\nplot.setTitle("Kotlin Works")\nplot.setXLabel("Horizontal")\nplot.setYLabel("Vertical")\nplot.add(line)\nplot\n') # ## Clojure # In[ ]: get_ipython().run_cell_magic('clojure', '', '(import \'[com.twosigma.beakerx.chart.xychart Plot]\n \'[com.twosigma.beakerx.chart.xychart.plotitem Line])\n(doto (Plot.)\n (.setTitle "Clojure Works")\n (.setXLabel "Horizontal")\n (.setYLabel "Vertical")\n (.add (doto (Line.)\n (.setX [0, 1, 2, 3, 4, 5])\n (.setY [0, 1, 6, 5, 2, 8]))))\n') # ## SQL # In[ ]: get_ipython().run_cell_magic('sql', '', "%defaultDatasource jdbc:h2:mem:db\nDROP TABLE IF EXISTS cities;\nCREATE TABLE cities(\n zip_code varchar(5),\n latitude float,\n longitude float,\n city varchar(100),\n state varchar(2),\n county varchar(100),\n PRIMARY KEY (zip_code),\n) AS SELECT\n zip_code,\n latitude,\n longitude,\n city,\n state,\n county\nFROM CSVREAD('../resources/data/UScity.csv')\n") # In[ ]: get_ipython().run_cell_magic('sql', '', "SELECT * FROM cities WHERE state = 'NY'\n")