use of org.apache.zeppelin.interpreter.InterpreterResultMessageOutput in project zeppelin by apache.
the class KotlinInterpreterTest method getInterpreterContext.
private static InterpreterContext getInterpreterContext() {
output = "";
InterpreterContext context = InterpreterContext.builder().setInterpreterOut(new InterpreterOutput()).build();
context.out = new InterpreterOutput(new InterpreterOutputListener() {
@Override
public void onUpdateAll(InterpreterOutput out) {
}
@Override
public void onAppend(int index, InterpreterResultMessageOutput out, byte[] line) {
try {
output = out.toInterpreterResultMessage().getData();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onUpdate(int index, InterpreterResultMessageOutput out) {
}
});
return context;
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessageOutput in project zeppelin by apache.
the class JupyterKernelClient method stream_execute.
// execute the code and make the output as streaming by writing it to InterpreterOutputStream
// one by one.
public ExecuteResponse stream_execute(ExecuteRequest request, final InterpreterOutputStream interpreterOutput) {
final ExecuteResponse.Builder finalResponseBuilder = ExecuteResponse.newBuilder().setStatus(ExecuteStatus.SUCCESS);
final AtomicBoolean completedFlag = new AtomicBoolean(false);
maybeKernelFailed = false;
LOGGER.debug("stream_execute code:\n" + request.getCode());
asyncStub.execute(request, new StreamObserver<ExecuteResponse>() {
OutputType lastOutputType = null;
@Override
public void onNext(ExecuteResponse executeResponse) {
LOGGER.debug("Interpreter Streaming Output: " + executeResponse.getType() + "\t" + executeResponse.getOutput());
switch(executeResponse.getType()) {
case TEXT:
try {
if (checkForShinyApp(executeResponse.getOutput())) {
break;
}
if (executeResponse.getOutput().startsWith("%")) {
// the output from jupyter kernel maybe specify format already.
interpreterOutput.write((executeResponse.getOutput()).getBytes());
} else {
// only add %text when the previous output type is not TEXT & HTML.
// Reason :
// 1. if no `%text`, it will be treated as previous output type.
// 2. Always prepend `%text `, there will be an extra line separator,
// because `%text ` appends line separator first.
InterpreterResultMessageOutput curOutput = interpreterOutput.getInterpreterOutput().getCurrentOutput();
if (curOutput != null && curOutput.getType() != InterpreterResult.Type.HTML && curOutput.getType() != InterpreterResult.Type.TEXT) {
interpreterOutput.write("%text ".getBytes());
}
// R packages doesn't work. e.g. googlevis
if (kernel.equals("ir") && executeResponse.getOutput().contains("<script type=\"text/javascript\">")) {
interpreterOutput.write("\n%html ".getBytes());
}
interpreterOutput.write(executeResponse.getOutput().getBytes());
}
interpreterOutput.getInterpreterOutput().flush();
} catch (IOException e) {
LOGGER.error("Unexpected IOException", e);
}
break;
case PNG:
case JPEG:
try {
interpreterOutput.write(("\n%img " + executeResponse.getOutput()).getBytes());
interpreterOutput.getInterpreterOutput().flush();
} catch (IOException e) {
LOGGER.error("Unexpected IOException", e);
}
break;
case HTML:
try {
interpreterOutput.write(("\n%html " + executeResponse.getOutput()).getBytes());
interpreterOutput.getInterpreterOutput().flush();
} catch (IOException e) {
LOGGER.error("Unexpected IOException", e);
}
break;
case CLEAR:
interpreterOutput.getInterpreterOutput().clear();
break;
default:
LOGGER.error("Unrecognized type:" + executeResponse.getType());
}
lastOutputType = executeResponse.getType();
if (executeResponse.getStatus() == ExecuteStatus.ERROR) {
// set the finalResponse to ERROR if any ERROR happens, otherwise the finalResponse would
// be SUCCESS.
finalResponseBuilder.setStatus(ExecuteStatus.ERROR);
}
}
@Override
public void onError(Throwable throwable) {
try {
// only output the extra error when no error message is displayed before.
if (finalResponseBuilder.getStatus() != null && finalResponseBuilder.getStatus() != ExecuteStatus.ERROR) {
interpreterOutput.getInterpreterOutput().write("\n%text " + ExceptionUtils.getStackTrace(throwable));
interpreterOutput.getInterpreterOutput().flush();
}
} catch (IOException e) {
LOGGER.error("Unexpected IOException", e);
}
LOGGER.error("Fail to call IPython grpc", throwable);
finalResponseBuilder.setStatus(ExecuteStatus.ERROR);
maybeKernelFailed = true;
completedFlag.set(true);
synchronized (completedFlag) {
completedFlag.notify();
}
}
@Override
public void onCompleted() {
synchronized (completedFlag) {
try {
LOGGER.debug("stream_execute is completed");
interpreterOutput.getInterpreterOutput().flush();
} catch (IOException e) {
LOGGER.error("Unexpected IOException", e);
}
completedFlag.set(true);
completedFlag.notify();
}
}
});
synchronized (completedFlag) {
if (!completedFlag.get()) {
try {
completedFlag.wait();
} catch (InterruptedException e) {
LOGGER.error("Unexpected Interruption", e);
}
}
}
return finalResponseBuilder.build();
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessageOutput in project zeppelin by apache.
the class RemoteInterpreterServer method runApplication.
@Override
public RemoteApplicationResult runApplication(String applicationInstanceId) throws InterpreterRPCException, TException {
LOGGER.info("run application {}", applicationInstanceId);
RunningApplication runningApp = runningApplications.get(applicationInstanceId);
if (runningApp == null) {
LOGGER.error("Application instance {} not exists", applicationInstanceId);
return new RemoteApplicationResult(false, "Application instance does not exists");
} else {
ApplicationContext context = runningApp.app.context();
try {
context.out.clear();
context.out.setType(InterpreterResult.Type.ANGULAR);
ResourceSet resource = appLoader.findRequiredResourceSet(runningApp.pkg.getResources(), context.getNoteId(), context.getParagraphId());
for (Resource res : resource) {
System.err.println("Resource " + res.get());
}
runningApp.app.run(resource);
context.out.flush();
InterpreterResultMessageOutput out = context.out.getOutputAt(0);
intpEventClient.onAppOutputUpdate(context.getNoteId(), context.getParagraphId(), 0, applicationInstanceId, out.getType(), new String(out.toByteArray()));
return new RemoteApplicationResult(true, "");
} catch (ApplicationException | IOException e) {
return new RemoteApplicationResult(false, e.getMessage());
}
}
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessageOutput in project zeppelin by apache.
the class SubmarineUI method outputLog.
public void outputLog(String title, String message) {
try {
StringBuffer formatMsg = new StringBuffer();
InterpreterResultMessageOutput output = null;
output = intpContext.out.getOutputAt(1);
formatMsg.append("<div style=\"width:100%\">");
if (!StringUtils.isEmpty(title)) {
formatMsg.append(title);
}
formatMsg.append("<pre style=\"max-height:120px\">");
formatMsg.append(message);
formatMsg.append("</pre>");
formatMsg.append("</div>\n");
output.write(formatMsg.toString());
output.flush();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessageOutput in project zeppelin by apache.
the class SubmarineUI method createUsageUI.
public void createUsageUI() {
try {
List<CommandlineOption> commandlineOptions = getCommandlineOptions();
HashMap<String, Object> mapParams = new HashMap();
mapParams.put(unifyKey(SubmarineConstants.PARAGRAPH_ID), intpContext.getParagraphId());
mapParams.put(SubmarineConstants.COMMANDLINE_OPTIONS, commandlineOptions);
URL urlTemplate = Resources.getResource(SUBMARINE_USAGE_JINJA);
String template = Resources.toString(urlTemplate, Charsets.UTF_8);
Jinjava jinjava = new Jinjava();
String submarineUsage = jinjava.render(template, mapParams);
InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(0);
outputUI.clear();
outputUI.write(submarineUsage);
outputUI.flush();
// UI update, log needs to be cleaned at the same time
InterpreterResultMessageOutput outputLOG = intpContext.out.getOutputAt(1);
outputLOG.clear();
outputLOG.flush();
} catch (IOException e) {
LOGGER.error("Can't print usage", e);
}
}
Aggregations