{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The `scijava-jupyter-kernel` is polyglot which means you can use multiple languages in the same notebook. Available languages are : \n", "\n", "- Groovy\n", "- Python\n", "- BeanShell\n", "- Clojure\n", "- JavaScript\n", "- Scala\n", "\n", "All those script languages are executed by the [Scijava Scripting Mechanism](https://imagej.net/Scripting#Supported_languages). It means that any languages added to Scijava Script will work on `scijava-jupyter-kernel`.\n", "\n", "You can specify a language per cell with the following shebang-like syntax : `#!groovy`. It needs to be on the **first line** of the cell. If nothing is specified, Groovy is the default language or the last selected language is used. \n", "\n", "---\n", "\n", "Now let's see some of the specific feature of the scripting languages." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Groovy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Groovy version is 2.4.10\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!groovy\n", "println \"The Groovy version is \" + GroovySystem.version" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Groovy!\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class HelloWorld {\n", " def greet( name ){\n", " \"Hello ${name}!\"\n", " }\n", "}\n", "\n", "def hm = new HelloWorld()\n", "println hm.greet(\"Groovy\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n", "10\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def pickEven(n, block) {\n", " for (int i=2; i <= n; i += 2) {\n", " block(i)\n", " }\n", "}\n", "\n", "pickEven(10) {\n", " println it\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11) \n", "[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)]\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!python\n", "import sys\n", "print(sys.version)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Rocket altitude:', 1)\n", "('Rocket altitude:', 0)\n", "('Rocket altitude:', 0)\n", "('Rocket altitude:', 0)\n", "('Rocket altitude:', 0)\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Code is from http://introtopython.org/classes.html\n", "\n", "class Rocket(object):\n", " # Rocket simulates a rocket ship for a game,\n", " # or a physics simulation.\n", " \n", " def __init__(self):\n", " # Each rocket has an (x,y) position.\n", " self.x = 0\n", " self.y = 0\n", " \n", " def move_up(self):\n", " # Increment the y-position of the rocket.\n", " self.y += 1\n", " \n", "# Create a fleet of 5 rockets, and store them in a list.\n", "my_rockets = [Rocket() for x in range(0,5)]\n", "\n", "# Move the first rocket up.\n", "my_rockets[0].move_up()\n", "\n", "# Show that only the first rocket has moved.\n", "for rocket in my_rockets:\n", " print(\"Rocket altitude:\", rocket.y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Beanshell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#!Beanshell\n", "printBanner();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hashtable hashtable = new Hashtable();\n", "Date date = new Date();\n", "hashtable.put( \"today\", date );\n", "\n", "// Print the current clock value\n", "print( System.currentTimeMillis() );\n", "\n", "// Loop\n", "for (int i=0; i<5; i++)\n", " print(i);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clojure" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{:major 1, :minor 8, :incremental 0, :qualifier nil}\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!clojure\n", "(println *clojure-version*)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 -1 2 1/2]\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(let [fn-list [+ - * /]]\n", " (println ((apply juxt fn-list) 1 2)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Javascript" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World :-)\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!javascript\n", "print(\"Hello World :-)\");" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good day\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time = 18\n", "\n", "if (time < 10) {\n", " greeting = \"Good morning\";\n", "} else if (time < 20) {\n", " greeting = \"Good day\";\n", "} else {\n", " greeting = \"Good evening\";\n", "}\n", "\n", "print(greeting)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BMW\n", "Volvo\n", "Saab\n", "Ford\n", "\n" ] }, { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var cars = [\"BMW\", \"Volvo\", \"Saab\", \"Ford\"];\n", "var text = \"\";\n", "\n", "for (var i = 0; i < cars.length; i++) {\n", " text += cars[i] + \"\\n\";\n", "}\n", "\n", "print(text);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scala" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Scala library version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc." ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#!scala\n", "scala.util.Properties.versionMsg" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "No Outputs" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point(val xc: Int, val yc: Int) {\n", " var x: Int = xc\n", " var y: Int = yc\n", " \n", " def move(dx: Int, dy: Int) {\n", " x = x + dx\n", " y = y + dy\n", " println(\"Point x location : \" + x);\n", " println(\"Point y location : \" + y);\n", " }\n", "}\n", "\n", "val pt = new Point(10, 20);\n", "// Move to a new location\n", "pt.move(10, 10);" ] } ], "metadata": { "kernelspec": { "display_name": "SciJava", "language": "groovy", "name": "scijava" }, "language_info": { "codemirror_mode": "groovy", "file_extension": "", "mimetype": "", "name": "scijava", "nbconverter_exporter": "", "pygments_lexer": "groovy", "version": "1.0" } }, "nbformat": 4, "nbformat_minor": 2 }