Search in sources :

Example 6 with InterpreterResultMessageOutput

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;
}
Also used : InterpreterOutputListener(org.apache.zeppelin.interpreter.InterpreterOutputListener) InterpreterResultMessageOutput(org.apache.zeppelin.interpreter.InterpreterResultMessageOutput) InterpreterOutput(org.apache.zeppelin.interpreter.InterpreterOutput) IOException(java.io.IOException) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext)

Example 7 with InterpreterResultMessageOutput

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterpreterResultMessageOutput(org.apache.zeppelin.interpreter.InterpreterResultMessageOutput) ExecuteResponse(org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse) IOException(java.io.IOException) OutputType(org.apache.zeppelin.interpreter.jupyter.proto.OutputType)

Example 8 with InterpreterResultMessageOutput

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());
        }
    }
}
Also used : ApplicationContext(org.apache.zeppelin.helium.ApplicationContext) ApplicationException(org.apache.zeppelin.helium.ApplicationException) Resource(org.apache.zeppelin.resource.Resource) InterpreterResultMessageOutput(org.apache.zeppelin.interpreter.InterpreterResultMessageOutput) ResourceSet(org.apache.zeppelin.resource.ResourceSet) IOException(java.io.IOException) RemoteApplicationResult(org.apache.zeppelin.interpreter.thrift.RemoteApplicationResult)

Example 9 with InterpreterResultMessageOutput

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);
    }
}
Also used : InterpreterResultMessageOutput(org.apache.zeppelin.interpreter.InterpreterResultMessageOutput) IOException(java.io.IOException)

Example 10 with InterpreterResultMessageOutput

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);
    }
}
Also used : HashMap(java.util.HashMap) Jinjava(com.hubspot.jinjava.Jinjava) InterpreterResultMessageOutput(org.apache.zeppelin.interpreter.InterpreterResultMessageOutput) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

IOException (java.io.IOException)11 InterpreterResultMessageOutput (org.apache.zeppelin.interpreter.InterpreterResultMessageOutput)11 Jinjava (com.hubspot.jinjava.Jinjava)5 URL (java.net.URL)5 HashMap (java.util.HashMap)5 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)3 InterpreterOutputListener (org.apache.zeppelin.interpreter.InterpreterOutputListener)3 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)2 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)2 InetAddress (java.net.InetAddress)1 Date (java.util.Date)1 Properties (java.util.Properties)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ApplicationContext (org.apache.zeppelin.helium.ApplicationContext)1 ApplicationException (org.apache.zeppelin.helium.ApplicationException)1 Interpreter (org.apache.zeppelin.interpreter.Interpreter)1 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)1 ExecuteResponse (org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse)1 OutputType (org.apache.zeppelin.interpreter.jupyter.proto.OutputType)1 RemoteInterpreterEventClient (org.apache.zeppelin.interpreter.remote.RemoteInterpreterEventClient)1