-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:twosigma/beakerx into lukasz/7204…
…_do_not_relly_on_bootstrap_css
- Loading branch information
Showing
85 changed files
with
3,368 additions
and
700 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ | |
from .kotlin_magic import * | ||
from .scala_magic import * | ||
from .sql_magic import * | ||
from .jvm_kernel_magic import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from py4j.clientserver import ClientServer, JavaParameters, PythonParameters | ||
from queue import Empty | ||
from jupyter_client.manager import KernelManager | ||
from jupyter_client.kernelspec import NoSuchKernel | ||
import json | ||
import sys | ||
|
||
|
||
class JVMKernelMagic: | ||
|
||
def __init__(self, kernel_name): | ||
self.km = None | ||
self.kc = None | ||
self.comms = [] | ||
self.kernel_name = kernel_name | ||
self.start() | ||
|
||
def start(self): | ||
self.km = KernelManager() | ||
self.km.kernel_name = self.kernel_name | ||
self.km.start_kernel() | ||
self.kc = self.km.client() | ||
self.kc.start_channels() | ||
self.kc.wait_for_ready() | ||
|
||
def stop_kernel(self): | ||
self.kc.stop_channels() | ||
self.km.shutdown_kernel(now=True) | ||
|
||
def run_cell(self, code): | ||
if not self.km: | ||
self.start() | ||
self.kc.execute(code, allow_stdin=True) | ||
|
||
def get_shell_msg(self): | ||
return self.kc.get_shell_msg() | ||
|
||
def get_iopub_msg(self): | ||
try: | ||
msg = self.kc.get_iopub_msg(timeout=1) | ||
return msg | ||
except Empty: | ||
return None | ||
|
||
def pass_msg(self, msg_raw): | ||
msg_json = json.loads(msg_raw) | ||
content = msg_json['content'] | ||
msg_type = msg_json['header']['msg_type'] | ||
msg = self.kc.session.msg(msg_type, content) | ||
self.kc.shell_channel.send(msg) | ||
return None | ||
|
||
|
||
class PythonEntryPoint(object): | ||
|
||
def __init__(self, kernel_name): | ||
self.pm = JVMKernelMagic(kernel_name) | ||
|
||
def evaluate(self, code): | ||
print('code for evaluate {}'.format(code)) | ||
self.pm.run_cell(code) | ||
return None | ||
|
||
def getShellMsg(self): | ||
shellMsg = self.pm.get_shell_msg() | ||
return json.dumps(shellMsg, default=str) | ||
|
||
def getIopubMsg(self): | ||
iopubMsg = self.pm.get_iopub_msg() | ||
return json.dumps(iopubMsg, default=str) | ||
|
||
def shutdownKernel(self): | ||
self.pm.stop_kernel() | ||
return None | ||
|
||
def sendMessage(self, msg_raw): | ||
self.pm.pass_msg(msg_raw) | ||
return None | ||
|
||
class Java: | ||
implements = ["com.twosigma.beakerx.kernel.PythonEntryPoint"] | ||
|
||
|
||
class Py4JServer: | ||
def __init__(self, port, pyport, kernel_name): | ||
try: | ||
pep = PythonEntryPoint(kernel_name) | ||
except NoSuchKernel: | ||
sys.exit(2) | ||
ClientServer( | ||
java_parameters=JavaParameters(port=int(port)), | ||
python_parameters=PythonParameters(port=int(pyport)), | ||
python_server_entry_point=pep) | ||
print('Py4j server is running') | ||
|
||
|
||
if __name__ == '__main__': | ||
Py4JServer(sys.argv[1], sys.argv[2], sys.argv[3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Polyglot Magics\n", | ||
"\n", | ||
"The BeakerX kernels have magics to run other kernels, including `%%python`. There are magics for each BeakerX langage: `%%groovy`, `%%java`, `%%scala`, `%%sql`, `%%clojure`, and `%%kotlin`. There is also a `%%kernel` magic to access any kernel available to Jupyter. Widgets work through these magics.\n", | ||
"\n", | ||
"## Python Magic\n", | ||
"\n", | ||
"For example, even though this is a Groovy notebook, you can run Python code:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%python\n", | ||
"print(\"regular\")\n", | ||
"{\"output\": \"works\"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Widgets Work" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%python\n", | ||
"import ipywidgets as widgets\n", | ||
"w = widgets.IntSlider()\n", | ||
"w" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%python\n", | ||
"w.value" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Scala Table" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%scala\n", | ||
"new TableDisplay(Seq(Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 3),\n", | ||
" Map(\"a\" -> 4, \"b\" -> 5, \"c\" -> 6),\n", | ||
" Map(\"a\" -> 7, \"b\" -> 8, \"c\" -> 8)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Kernel Magic\n", | ||
"\n", | ||
"Access any kernel available to Jupyter by name:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%kernel ir\n", | ||
"x <- rnorm(1000)\n", | ||
"plot(x)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%kernel julia-0.6\n", | ||
"(1 + 1im) * (1 - 1im)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Groovy", | ||
"language": "groovy", | ||
"name": "groovy" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "groovy", | ||
"file_extension": ".groovy", | ||
"mimetype": "", | ||
"name": "Groovy", | ||
"nbconverter_exporter": "", | ||
"version": "2.4.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.