Skip to content

Commit

Permalink
#7718: accessible url arguments from notebook (#7773)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete authored and LeeTZ committed Aug 29, 2018
1 parent b4d2c68 commit 5f6f726
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 86 deletions.
26 changes: 26 additions & 0 deletions js/notebook/src/extension/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare global {
}

export const BEAKER_GETCODECELLS = 'beakerx.getcodecells';
export const BEAKER_GET_URL_ARG = 'beakerx.geturlarg';
export const BEAKER_AUTOTRANSLATION = 'beakerx.autotranslation';
export const BEAKER_TAG_RUN = 'beakerx.tag.run';

Expand All @@ -39,6 +40,12 @@ const msgHandlers = {
msgHandlers[BEAKER_AUTOTRANSLATION](msg);
},

[BEAKER_GET_URL_ARG]: (msg) => {
if (msg.content.data.state.name == "URL_ARG") {
sendArgUrl(msg.content.data.url,msg.content.data.state.arg_name);
}
},

[BEAKER_AUTOTRANSLATION]: (msg) => {
window.beakerx['LOCK_PROXY'] = true;
window.beakerx[msg.content.data.state.name] = JSON.parse(msg.content.data.state.value);
Expand Down Expand Up @@ -87,6 +94,10 @@ export const registerCommTargets = (kernel: any): void => {
comm.on_msg(msgHandlers[BEAKER_TAG_RUN]);
});

kernel.comm_manager.register_target(BEAKER_GET_URL_ARG, function (comm) {
comm.on_msg(msgHandlers[BEAKER_GET_URL_ARG]);
});

kernel.comm_info(
null, (msg) => {
assignMsgHandlersToExistingComms(msg.content.comms, kernel);
Expand All @@ -111,6 +122,21 @@ const sendJupyterCodeCells = (filter: string, url: string) => {
service.post(data)
};

const sendArgUrl = (url: string, argName:string) => {

const data: { url: string,argName:string, argValue:string } =
{
argName: argName,
argValue: "",
url : url
};

let parsedUrl = new URL(window.location.href);
data.argValue = parsedUrl.searchParams.get(argName);
let service = new BeakerxRestHandler();
service.post(data)
};

class BeakerxRestHandler {

private api: BeakerXApi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import java.util.concurrent.SynchronousQueue;

public interface BeakerXClient {

String CODE_CELL_PATH = "codecell";

String URL_ARG = "urlarg";

void showProgressUpdate(String message, int progress);

void delBeaker();
Expand All @@ -38,4 +41,6 @@ public interface BeakerXClient {
void runByTag(String tag);

String getContext();

String urlArg(String argName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class NamespaceClient implements BeakerXClient {
private AutotranslationService autotranslationService;
private BeakerXJsonSerializer beakerXJsonSerializer;
private CommRepository commRepository;
private Comm urlArgComm;

public NamespaceClient(AutotranslationService autotranslationService, BeakerXJsonSerializer beakerXJsonSerializer, CommRepository commRepository) {
this.autotranslationService = autotranslationService;
Expand Down Expand Up @@ -192,4 +193,32 @@ public static NamespaceClient create(String id,
return new NamespaceClient(AutotranslationServiceImpl.createAsMainKernel(id), serializer, commRepository);
}
}

@Override
public String urlArg(String argName) {
try {
Comm c = getUrlArgComm();
HashMap<String, Serializable> data = new HashMap<>();
HashMap<String, Serializable> state = new HashMap<>();
state.put("name", "URL_ARG");
state.put("arg_name", argName);
data.put("url", KernelManager.get().getBeakerXServer().getURL() + URL_ARG);
data.put("state", state);
data.put("buffer_paths", new HashMap<>());
c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data));
// block
Object argNameValue = getMessageQueue(URL_ARG).take();
return (String) argNameValue;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private Comm getUrlArgComm() {
if (urlArgComm == null) {
urlArgComm = new Comm(TargetNamesEnum.BEAKER_GET_URL_ARG);
urlArgComm.open();
}
return urlArgComm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum TargetNamesEnum {

BEAKER_TAG_RUN("beakerx.tag.run"),
BEAKER_GETCODECELLS("beakerx.getcodecells"),
BEAKER_GET_URL_ARG("beakerx.geturlarg"),
BEAKER_AUTOTRANSLATION("beakerx.autotranslation"),
JUPYTER_WIDGET("jupyter.widget"),
JUPYTER_WIDGET_VERSION("jupyter.widget.version"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jetbrains.annotations.NotNull;

import static com.twosigma.beakerx.BeakerXClient.CODE_CELL_PATH;
import static com.twosigma.beakerx.BeakerXClient.URL_ARG;
import static com.twosigma.beakerx.KernelInfoHandler.INTERRUPT_KERNEL;
import static com.twosigma.beakerx.kernel.comm.GetCodeCellsHandler.INSTANCE;
import static com.twosigma.beakerx.kernel.magic.command.functionality.AsyncMagicCommand.CANCEL_EXECUTION;
Expand All @@ -33,6 +34,11 @@ public abstract class BeakerXServerJavalin implements BeakerXServer {

private Javalin app = null;
private Integer freePort;
private GetUrlArgHandler urlArgHandler;

public BeakerXServerJavalin(GetUrlArgHandler urlArgHandler) {
this.urlArgHandler = urlArgHandler;
}

@Override
public synchronized BeakerXServer get(KernelFunctionality kernel) {
Expand Down Expand Up @@ -73,6 +79,11 @@ private void mappingsForAllKernels(Javalin server, KernelFunctionality kernel) {
server.post(CANCEL_EXECUTION+"/:groupname", ctx -> {
kernel.cancelExecution(GroupName.of(ctx.param("groupname")));
});

server.post(URL_ARG, ctx -> {
String body = ctx.body();
urlArgHandler.handle(body);
});
}

public abstract void createMapping(Javalin app, KernelFunctionality kernel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.
*/
package com.twosigma.beakerx.kernel.restserver.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.BeakerXClient;

import java.io.IOException;

import static com.twosigma.beakerx.BeakerXClient.URL_ARG;

public class GetUrlArgHandler {


private ObjectMapper objectMapper;
private BeakerXClient beakerXClient;

public GetUrlArgHandler(BeakerXClient beakerXClient) {
this.beakerXClient = beakerXClient;
objectMapper = new ObjectMapper();
}

public void handle(String data) {
try {
String value = getBeakerArgValue(data);
this.beakerXClient.getMessageQueue(URL_ARG).put((value == null) ? "" : value);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

private String getBeakerArgValue(String json) {
try {
UrlArgBody value = objectMapper.readValue(json, UrlArgBody.class);
return value.argValue;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static class UrlArgBody {
public String argName;
public String argValue;
public String url;

public UrlArgBody() {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public String getContext() {
return null;
}

@Override
public String urlArg(String argName) {
return null;
}

public String getLastRunByTag() {
return lastRunByTag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
package com.twosigma.beakerx.clojure.kernel;

import static com.twosigma.beakerx.DefaultJVMVariables.IMPORTS;
import static com.twosigma.beakerx.kernel.Utils.uuid;
import static java.util.Arrays.stream;

import clojure.lang.LazySeq;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.BeakerXCommRepository;
Expand All @@ -30,31 +26,24 @@
import com.twosigma.beakerx.clojure.handlers.ClojureKernelInfoHandler;
import com.twosigma.beakerx.evaluator.Evaluator;
import com.twosigma.beakerx.handler.KernelHandler;
import com.twosigma.beakerx.kernel.restserver.BeakerXServer;
import com.twosigma.beakerx.kernel.CacheFolderFactory;
import com.twosigma.beakerx.kernel.CloseKernelAction;
import com.twosigma.beakerx.kernel.CustomMagicCommandsEmptyImpl;
import com.twosigma.beakerx.kernel.Kernel;
import com.twosigma.beakerx.kernel.KernelConfigurationFile;
import com.twosigma.beakerx.kernel.EvaluatorParameters;
import com.twosigma.beakerx.kernel.KernelRunner;
import com.twosigma.beakerx.kernel.KernelSocketsFactory;
import com.twosigma.beakerx.kernel.KernelSocketsFactoryImpl;
import com.twosigma.beakerx.kernel.*;
import com.twosigma.beakerx.kernel.handler.CommOpenHandler;
import com.twosigma.beakerx.kernel.magic.command.MagicCommandTypesFactory;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand;
import com.twosigma.beakerx.kernel.restserver.BeakerXServer;
import com.twosigma.beakerx.kernel.restserver.impl.GetUrlArgHandler;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.mimetype.MIMEContainer;
import jupyter.Displayer;
import jupyter.Displayers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

import static com.twosigma.beakerx.DefaultJVMVariables.IMPORTS;
import static com.twosigma.beakerx.kernel.Utils.uuid;
import static java.util.Arrays.stream;

public class Clojure extends Kernel {

private Clojure(String sessionId,
Expand All @@ -72,7 +61,14 @@ public Clojure(String sessionId,
CacheFolderFactory cacheFolderFactory,
CommRepository commRepository,
BeakerXServer beakerXServer) {
super(sessionId, evaluator, kernelSocketsFactory, closeKernelAction, cacheFolderFactory, new CustomMagicCommandsEmptyImpl(), commRepository, beakerXServer);
super(sessionId,
evaluator,
kernelSocketsFactory,
closeKernelAction,
cacheFolderFactory,
new CustomMagicCommandsEmptyImpl(),
commRepository,
beakerXServer);
}

@Override
Expand All @@ -95,7 +91,7 @@ public static void main(final String[] args) {
NamespaceClient namespaceClient = NamespaceClient.create(id, configurationFile, new ClojureBeakerXJsonSerializer(), beakerXCommRepository);
ClojureEvaluator evaluator = new ClojureEvaluator(id, id, getKernelParameters(), namespaceClient);

return new Clojure(id, evaluator, kernelSocketsFactory, beakerXCommRepository, new ClojureBeakerXServer());
return new Clojure(id, evaluator, kernelSocketsFactory, beakerXCommRepository, new ClojureBeakerXServer(new GetUrlArgHandler(namespaceClient)));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

import com.twosigma.beakerx.kernel.restserver.impl.BeakerXServerJavalin;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.kernel.restserver.impl.GetUrlArgHandler;
import io.javalin.Javalin;

public class ClojureBeakerXServer extends BeakerXServerJavalin {

public ClojureBeakerXServer(GetUrlArgHandler urlArgHandler) {
super(urlArgHandler);
}

@Override
public void createMapping(Javalin app, KernelFunctionality kernel) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package com.twosigma.beakerx.groovy.kernel;

import static com.twosigma.beakerx.DefaultJVMVariables.IMPORTS;
import static com.twosigma.beakerx.kernel.Utils.uuid;

import com.twosigma.beakerx.BeakerXCommRepository;
import com.twosigma.beakerx.CommRepository;
import com.twosigma.beakerx.NamespaceClient;
Expand All @@ -26,21 +23,17 @@
import com.twosigma.beakerx.groovy.evaluator.GroovyEvaluator;
import com.twosigma.beakerx.groovy.handler.GroovyKernelInfoHandler;
import com.twosigma.beakerx.handler.KernelHandler;
import com.twosigma.beakerx.kernel.restserver.BeakerXServer;
import com.twosigma.beakerx.kernel.CacheFolderFactory;
import com.twosigma.beakerx.kernel.CloseKernelAction;
import com.twosigma.beakerx.kernel.CustomMagicCommandsEmptyImpl;
import com.twosigma.beakerx.kernel.Kernel;
import com.twosigma.beakerx.kernel.KernelConfigurationFile;
import com.twosigma.beakerx.kernel.EvaluatorParameters;
import com.twosigma.beakerx.kernel.KernelRunner;
import com.twosigma.beakerx.kernel.KernelSocketsFactory;
import com.twosigma.beakerx.kernel.KernelSocketsFactoryImpl;
import com.twosigma.beakerx.kernel.*;
import com.twosigma.beakerx.kernel.handler.CommOpenHandler;
import com.twosigma.beakerx.kernel.restserver.BeakerXServer;
import com.twosigma.beakerx.kernel.restserver.impl.GetUrlArgHandler;
import com.twosigma.beakerx.message.Message;

import java.util.HashMap;

import static com.twosigma.beakerx.DefaultJVMVariables.IMPORTS;
import static com.twosigma.beakerx.kernel.Utils.uuid;

public class Groovy extends Kernel {

private Groovy(final String id,
Expand Down Expand Up @@ -86,11 +79,12 @@ public static void main(final String[] args) {
configurationFile);

BeakerXCommRepository beakerXCommRepository = new BeakerXCommRepository();
NamespaceClient namespaceClient = NamespaceClient.create(id, configurationFile, beakerXCommRepository);
GroovyEvaluator evaluator = new GroovyEvaluator(id,
id,
getEvaluatorParameters(),
NamespaceClient.create(id, configurationFile, beakerXCommRepository));
return new Groovy(id, evaluator, kernelSocketsFactory, beakerXCommRepository, new GroovyBeakerXServer());
namespaceClient);
return new Groovy(id, evaluator, kernelSocketsFactory, beakerXCommRepository, new GroovyBeakerXServer(new GetUrlArgHandler(namespaceClient)));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

import com.twosigma.beakerx.kernel.restserver.impl.BeakerXServerJavalin;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.kernel.restserver.impl.GetUrlArgHandler;
import io.javalin.Javalin;

public class GroovyBeakerXServer extends BeakerXServerJavalin {

public GroovyBeakerXServer(GetUrlArgHandler urlArgHandler) {
super(urlArgHandler);
}

@Override
public void createMapping(Javalin app, KernelFunctionality kernel) {
}
Expand Down
Loading

0 comments on commit 5f6f726

Please sign in to comment.