Skip to content

Commit

Permalink
jarek/7718: url arg accessible from notebook python (#7800)
Browse files Browse the repository at this point in the history
* #7718: getting url args for python

* #7718: solve problem with killing the app by CTRL+C

* #7718: add type to UrlArgBody
  • Loading branch information
jaroslawmalekcodete authored and LeeTZ committed Sep 26, 2018
1 parent f68329b commit 345b26e
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 89 deletions.
43 changes: 43 additions & 0 deletions beakerx/beakerx/beakerx_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2018 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.

import socket
import zmq
import threading


class BeakerxZMQServer:

def __init__(self, beakerXQueue):
self.queue = beakerXQueue
self.url = "tcp://127.0.0.1:" + BeakerxZMQServer.get_free_tcp_port()
thread = threading.Thread(target=self.threaded_function, daemon=True)
thread.start()

def threaded_function(self):
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind(self.url)
while True:
message = socket.recv()
self.queue.put(message)
socket.send_string("Ok")

@staticmethod
def get_free_tcp_port():
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.bind(('localhost', 0))
addr, port = tcp.getsockname()
tcp.close()
return str(port)
43 changes: 31 additions & 12 deletions beakerx/beakerx/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from notebook.utils import url_path_join
from notebook.base.handlers import APIHandler, IPythonHandler

import json
from tornado import web
from .environment import *
import beakerx
import tornado
import json
import os
import tornado
import zmq
from notebook.base.handlers import APIHandler, IPythonHandler
from notebook.utils import url_path_join
from tornado import web

from .beakerx_autotranslation_server import start_autotranslation_server
from .environment import *


class BeakerxRestHandler(APIHandler):

def data_received(self, chunk):
pass

@web.authenticated
@tornado.web.asynchronous
def post(self):

def handle_response(response):
self.finish(response.body)

data = tornado.escape.json_decode(self.request.body)
content = json.dumps(data)
params = json.loads(content)

type = params['type']
url = params['url']
if type == "python":
self.handle_python(content, url)
else:
self.handle_rest(self.handle_response, url)

def handle_rest(self, handle_response, url):
req = tornado.httpclient.HTTPRequest(
url=url,
method=self.request.method,
Expand All @@ -48,7 +54,6 @@ def handle_response(response):
follow_redirects=False,
allow_nonstandard_methods=True
)

client = tornado.httpclient.AsyncHTTPClient()
try:
client.fetch(req, handle_response)
Expand All @@ -60,6 +65,19 @@ def handle_response(response):
self.write('Internal server error:\n' + str(e))
self.finish()

def handle_python(self, content, url):
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(url)
socket.send_string(content)
response = socket.recv()
self.finish(response)
socket.close()
context.destroy()

def handle_response(self, response):
self.finish(response.body)


class SparkMetricsExecutorsHandler(APIHandler):
def data_received(self, chunk):
Expand Down Expand Up @@ -145,7 +163,8 @@ def load_jupyter_server_extension(nbapp):
web_app = nbapp.web_app
host_pattern = '.*$'
settings_route_pattern = url_path_join(web_app.settings['base_url'], '/beakerx', '/settings')
spark_metrics_executors_route_pattern = url_path_join(web_app.settings['base_url'], '/beakerx', '/sparkmetrics/executors')
spark_metrics_executors_route_pattern = url_path_join(web_app.settings['base_url'], '/beakerx',
'/sparkmetrics/executors')
version_route_pattern = url_path_join(web_app.settings['base_url'], '/beakerx', '/version')
javadoc_route_pattern = url_path_join(web_app.settings['base_url'], '/static', '/javadoc/(.*)')
javadoc_lab_route_pattern = url_path_join(web_app.settings['base_url'], '/javadoc/(.*)')
Expand Down
Loading

0 comments on commit 345b26e

Please sign in to comment.