Skip to content

Commit

Permalink
#7617 - fix for passing context json as process parameter on windows (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lmitusinski authored and scottdraves committed Jul 2, 2018
1 parent b723866 commit f3c1697
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion beakerx/beakerx/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,5 +619,6 @@ def get_context_session():
kernel = get_ipython().kernel
# if subkernel get session from extra start parameters
if len(kernel.parent.argv) == 3:
return json.loads(kernel.parent.argv[2])['contextId']
context_json = base64.b64decode(kernel.parent.argv[2]).decode('UTF-8')
return json.loads(context_json)['contextId']
return kernel.session.session
8 changes: 5 additions & 3 deletions beakerx/beakerx_magics/kernel_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from jupyter_client.manager import KernelManager
from ipykernel.zmqshell import ZMQInteractiveShell
import atexit
import base64
import json
import logging
import os
Expand All @@ -38,7 +39,7 @@ def __init__(self, shell):
def start(self, kernel_name):
self.km = KernelManager()
self.km.kernel_name = kernel_name
self.km.start_kernel(extra_arguments=[self._context_as_string()])
self.km.start_kernel(extra_arguments=[self._context_base64()])
atexit.register(self.stop_kernel)
self.kc = self.km.client()
self.kc.start_channels()
Expand Down Expand Up @@ -85,11 +86,12 @@ def pass_message(self, msg_raw):
# don't create the list of keys if debug messages aren't enabled
self.log.debug("Current comms: %s", list(self.comms.keys()))

def _context_as_string(self):
return json.dumps({
def _context_base64(self):
context_json = json.dumps({
'port': os.environ["BEAKERX_AUTOTRANSLATION_PORT"],
'contextId': get_ipython().kernel.session.session,
})
return base64.b64encode(context_json.encode('utf-8')).decode()


def comm_msg(stream, ident, msg):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.twosigma.beakerx.message.MessageSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import py4j.Base64;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Map;
Expand All @@ -44,7 +46,12 @@ public KernelConfigurationFile(final String[] args) {
}
this.config = getConfig(args[0]);
if (args.length == 2) {
String contextAsJson = args[1];
String contextAsJson = null;
try {
contextAsJson = new String(Base64.decode(args[1]), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.context = Optional.of(contextAsJson);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.message.Header;
import com.twosigma.beakerx.message.Message;
import org.apache.commons.codec.binary.Base64;
import py4j.ClientServer;
import py4j.GatewayServer;

Expand All @@ -28,6 +29,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -84,7 +86,7 @@ protected String[] getPy4jCommand() {
"--port", port == null ? DEFAULT_PORT : String.valueOf(port),
"--pyport", pythonPort == null ? DEFAULT_PYTHON_PORT : String.valueOf(pythonPort),
"--kernel", kernelName,
"--context", this.context
"--context", Base64.encodeBase64String(this.context.getBytes(StandardCharsets.UTF_8))
};
}

Expand Down

0 comments on commit f3c1697

Please sign in to comment.