{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Magics to Access the JVM Kernels from Python\n", "\n", "BeakerX has magics for Python so you can run cells in the other languages.\n", "The first few cells below show how complete the implementation is with Groovy, then we have just one cell in each other language.\n", "\n", "There are also [Polyglot Magics](../groovy/PolyglotMagic.ipynb) magics for accessing Python from the JVM.\n", "\n", "Fleshing out the polyglot support in BeakerX with communication between languages ([#5039](https://github.com/twosigma/beakerx/issues/5039)) is on the agenda.\n", "\n", "## Groovy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%groovy\n", "println(\"stdout works\")\n", "f = {it + \" work\"}\n", "f(\"results\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%groovy\n", "new Plot(title:\"plots work\", initHeight: 200)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%groovy\n", "[a:\"tables\", b:\"work\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%groovy\n", "\"errors work\"/1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%groovy\n", "HTML(\"

HTML works

\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%groovy\n", "def p = new Plot(title : 'Plots Work', xLabel: 'Horizontal', yLabel: 'Vertical');\n", "p << new Line(x: [0, 1, 2, 3, 4, 5], y: [0, 1, 6, 5, 2, 8])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Java" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%java\n", "import java.util.List;\n", "import com.twosigma.beakerx.chart.xychart.Plot;\n", "import java.util.Arrays;\n", "\n", "Plot p = new Plot();\n", "\n", "p.setTitle(\"Java Works\");\n", "p.setXLabel(\"Horizontal\");\n", "p.setYLabel(\"Vertical\");\n", "\n", "Bars b = new Bars();\n", "\n", "List x = Arrays.asList(0, 1, 2, 3, 4, 5);\n", "List y = Arrays.asList(0, 1, 6, 5, 2, 8);\n", "Line line = new Line();\n", "line.setX(x);\n", "line.setY(y);\n", "p.add(line);\n", " \n", "return p;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scala" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%scala\n", "val plot = new Plot { title = \"Scala Works\"; xLabel=\"Horizontal\"; yLabel=\"Vertical\" }\n", "val line = new Line {x = Seq(0, 1, 2, 3, 4, 5); y = Seq(0, 1, 6, 5, 2, 8)}\n", "plot.add(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kotlin" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%kotlin\n", "val x: MutableList = mutableListOf(0, 1, 2, 3, 4, 5)\n", "val y: MutableList = mutableListOf(0, 1, 6, 5, 2, 8)\n", "val line = Line()\n", "line.setX(x)\n", "line.setY(y)\n", "\n", "val plot = Plot()\n", "plot.setTitle(\"Kotlin Works\")\n", "plot.setXLabel(\"Horizontal\")\n", "plot.setYLabel(\"Vertical\")\n", "plot.add(line)\n", "plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clojure" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%clojure\n", "(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]))))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SQL" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sql\n", "%defaultDatasource jdbc:h2:mem:db\n", "DROP TABLE IF EXISTS cities;\n", "CREATE 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\n", "FROM CSVREAD('../resources/data/UScity.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sql\n", "SELECT * FROM cities WHERE state = 'NY'" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }