Search in sources :

Example 1 with ExecuteResponse

use of org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse 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 2 with ExecuteResponse

use of org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse in project zeppelin by apache.

the class JupyterKernelInterpreter method internalInterpret.

@Override
public InterpreterResult internalInterpret(String st, InterpreterContext context) throws InterpreterException {
    z.setGui(context.getGui());
    z.setNoteGui(context.getNoteGui());
    z.setInterpreterContext(context);
    interpreterOutput.setInterpreterOutput(context.out);
    jupyterKernelClient.setInterpreterContext(context);
    try {
        ExecuteResponse response = jupyterKernelClient.stream_execute(ExecuteRequest.newBuilder().setCode(st).build(), interpreterOutput);
        interpreterOutput.getInterpreterOutput().flush();
        // if jupyter kernel is maybe terminated.
        if (jupyterKernelProcessLauncher.isRunning() && !jupyterKernelClient.isMaybeKernelFailed()) {
            return new InterpreterResult(InterpreterResult.Code.valueOf(response.getStatus().name()));
        } else {
            if (jupyterKernelClient.isMaybeKernelFailed()) {
                Thread.sleep(1000);
            }
            if (jupyterKernelProcessLauncher.isRunning()) {
                return new InterpreterResult(InterpreterResult.Code.valueOf(response.getStatus().name()));
            } else {
                return new InterpreterResult(InterpreterResult.Code.ERROR, "IPython kernel is abnormally exited, please check your code and log.");
            }
        }
    } catch (Exception e) {
        throw new InterpreterException("Fail to interpret python code", e);
    }
}
Also used : InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ExecuteResponse(org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException)

Example 3 with ExecuteResponse

use of org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse in project zeppelin by apache.

the class IRInterpreter method initIRKernel.

/**
 * Init IRKernel by execute R script zeppelin-isparkr.R
 * @throws IOException
 * @throws InterpreterException
 */
protected void initIRKernel() throws IOException, InterpreterException {
    String timeout = getProperty("spark.r.backendConnectionTimeout", "6000");
    InputStream input = getClass().getClassLoader().getResourceAsStream("R/zeppelin_isparkr.R");
    String code = IOUtils.toString(input, StandardCharsets.UTF_8).replace("${Port}", sparkRBackend.port() + "").replace("${version}", sparkVersion() + "").replace("${libPath}", "\"" + SparkRUtils.getSparkRLib(isSparkSupported()) + "\"").replace("${timeout}", timeout).replace("${isSparkSupported}", "\"" + isSparkSupported() + "\"").replace("${authSecret}", "\"" + sparkRBackend.socketSecret() + "\"");
    LOGGER.debug("Init IRKernel via script:\n{}", code);
    ExecuteResponse response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder().setCode(code).build());
    if (response.getStatus() != ExecuteStatus.SUCCESS) {
        throw new IOException("Fail to setup JVMGateway\n" + response.getOutput());
    }
}
Also used : InputStream(java.io.InputStream) ExecuteResponse(org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse) IOException(java.io.IOException)

Example 4 with ExecuteResponse

use of org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse in project zeppelin by apache.

the class IPythonInterpreter method initPythonInterpreter.

private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws IOException {
    InputStream input = getClass().getClassLoader().getResourceAsStream("python/zeppelin_ipython.py");
    List<String> lines = IOUtils.readLines(input, StandardCharsets.UTF_8);
    ExecuteResponse response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder().setCode(StringUtils.join(lines, System.lineSeparator()).replace("${JVM_GATEWAY_PORT}", gatewayPort + "").replace("${JVM_GATEWAY_ADDRESS}", gatewayHost)).build());
    if (response.getStatus() != ExecuteStatus.SUCCESS) {
        throw new IOException("Fail to setup JVMGateway\n" + response.getOutput());
    }
    input = getClass().getClassLoader().getResourceAsStream("python/zeppelin_context.py");
    lines = IOUtils.readLines(input, StandardCharsets.UTF_8);
    response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder().setCode(StringUtils.join(lines, System.lineSeparator())).build());
    if (response.getStatus() != ExecuteStatus.SUCCESS) {
        throw new IOException("Fail to import ZeppelinContext\n" + response.getOutput());
    }
    response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder().setCode("z = __zeppelin__ = PyZeppelinContext(intp.getZeppelinContext(), gateway)").build());
    if (response.getStatus() != ExecuteStatus.SUCCESS) {
        throw new IOException("Fail to setup ZeppelinContext\n" + response.getOutput());
    }
    if (additionalPythonInitFile != null) {
        input = getClass().getClassLoader().getResourceAsStream(additionalPythonInitFile);
        lines = IOUtils.readLines(input, StandardCharsets.UTF_8);
        response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder().setCode(StringUtils.join(lines, System.lineSeparator()).replace("${JVM_GATEWAY_PORT}", gatewayPort + "").replace("${JVM_GATEWAY_ADDRESS}", gatewayHost)).build());
        if (response.getStatus() != ExecuteStatus.SUCCESS) {
            LOGGER.error("Fail to run additional Python init file\n{}", response.getOutput());
            throw new IOException("Fail to run additional Python init file: " + additionalPythonInitFile + "\n" + response.getOutput());
        }
    }
}
Also used : InputStream(java.io.InputStream) ExecuteResponse(org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse) IOException(java.io.IOException)

Example 5 with ExecuteResponse

use of org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse in project zeppelin by apache.

the class JupyterKernelClient method main.

public static void main(String[] args) {
    JupyterKernelClient client = new JupyterKernelClient("localhost", 50053, "python");
    client.status(StatusRequest.newBuilder().build());
    ExecuteResponse response = client.block_execute(ExecuteRequest.newBuilder().setCode("abcd=2").build());
    System.out.println(response.getOutput());
}
Also used : ExecuteResponse(org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse)

Aggregations

ExecuteResponse (org.apache.zeppelin.interpreter.jupyter.proto.ExecuteResponse)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 InterpreterResultMessageOutput (org.apache.zeppelin.interpreter.InterpreterResultMessageOutput)1 OutputType (org.apache.zeppelin.interpreter.jupyter.proto.OutputType)1