Search in sources :

Example 16 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class IRInterpreterTest method testZShow.

@Test
public void testZShow() throws InterpreterException, IOException {
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret("df=data.frame(country=c(\"US\", \"GB\", \"BR\"),\n" + "val1=c(10,13,14),\n" + "val2=c(23,12,32))", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    result = interpreter.interpret("z.show(df)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(resultMessages.toString(), InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
    assertEquals("country\tval1\tval2\n" + "3\t10\t23\n" + "2\t13\t12\n" + "1\t14\t32\n", resultMessages.get(0).getData());
    context = getInterpreterContext();
    result = interpreter.interpret("z.show(df, maxRows=1)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(2, resultMessages.size());
    assertEquals(resultMessages.toString(), InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
    assertEquals("country\tval1\tval2\n" + "3\t10\t23\n", resultMessages.get(0).getData());
    assertEquals(resultMessages.toString(), InterpreterResult.Type.HTML, resultMessages.get(1).getType());
    assertEquals("<font color=red>Results are limited by 1 rows.</font>\n", resultMessages.get(1).getData());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test) IRKernelTest(org.apache.zeppelin.jupyter.IRKernelTest)

Example 17 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class ShinyInterpreterTest method testShinyApp.

@Test
public void testShinyApp() throws IOException, InterpreterException, InterruptedException, UnirestException {
    /**
     **************** Launch Shiny app with default app name ****************************
     */
    InterpreterContext context = getInterpreterContext();
    context.getLocalProperties().put("type", "ui");
    InterpreterResult result = interpreter.interpret(IOUtils.toString(getClass().getResource("/ui.R"), StandardCharsets.UTF_8), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    context.getLocalProperties().put("type", "server");
    result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R"), StandardCharsets.UTF_8), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    final InterpreterContext context2 = getInterpreterContext();
    context2.getLocalProperties().put("type", "run");
    Thread thread = new Thread(() -> {
        try {
            interpreter.interpret("", context2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    thread.start();
    // wait for the shiny app start
    Thread.sleep(5 * 1000);
    // extract shiny url
    List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage();
    assertEquals(resultMessages.toString(), 1, resultMessages.size());
    assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType());
    String resultMessageData = resultMessages.get(0).getData();
    assertTrue(resultMessageData, resultMessageData.contains("<iframe"));
    Pattern urlPattern = Pattern.compile(".*src=\"(http\\S*)\".*", Pattern.DOTALL);
    Matcher matcher = urlPattern.matcher(resultMessageData);
    if (!matcher.matches()) {
        fail("Unable to extract url: " + resultMessageData);
    }
    String shinyURL = matcher.group(1);
    // verify shiny app via calling its rest api
    HttpResponse<String> response = Unirest.get(shinyURL).asString();
    assertEquals(200, response.getStatus());
    assertTrue(response.getBody(), response.getBody().contains("Shiny Text"));
    /**
     ********************** Launch another shiny app (app2) ****************************
     */
    context = getInterpreterContext();
    context.getLocalProperties().put("type", "ui");
    context.getLocalProperties().put("app", "app2");
    result = interpreter.interpret(IOUtils.toString(getClass().getResource("/ui.R"), StandardCharsets.UTF_8), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    context.getLocalProperties().put("type", "server");
    context.getLocalProperties().put("app", "app2");
    result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R"), StandardCharsets.UTF_8), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    final InterpreterContext context3 = getInterpreterContext();
    context3.getLocalProperties().put("type", "run");
    context3.getLocalProperties().put("app", "app2");
    thread = new Thread(() -> {
        try {
            interpreter.interpret("", context3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    thread.start();
    // wait for the shiny app start
    Thread.sleep(5 * 1000);
    // extract shiny url
    resultMessages = context3.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType());
    resultMessageData = resultMessages.get(0).getData();
    assertTrue(resultMessageData, resultMessageData.contains("<iframe"));
    matcher = urlPattern.matcher(resultMessageData);
    if (!matcher.matches()) {
        fail("Unable to extract url: " + resultMessageData);
    }
    String shinyURL2 = matcher.group(1);
    // verify shiny app via calling its rest api
    response = Unirest.get(shinyURL2).asString();
    assertEquals(200, response.getStatus());
    assertTrue(response.getBody(), response.getBody().contains("Shiny Text"));
    // cancel paragraph to stop the first shiny app
    interpreter.cancel(getInterpreterContext());
    // wait for shiny app to be stopped
    Thread.sleep(1000);
    try {
        Unirest.get(shinyURL).asString();
        fail("Should fail to connect to shiny app");
    } catch (Exception e) {
        assertTrue(e.getMessage(), e.getMessage().contains("Connection refused"));
    }
    // the second shiny app still works
    response = Unirest.get(shinyURL2).asString();
    assertEquals(200, response.getStatus());
    assertTrue(response.getBody(), response.getBody().contains("Shiny Text"));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class IPythonInterpreterTest method testIpythonKernelCrash_shouldNotHangExecution.

@Test
public void testIpythonKernelCrash_shouldNotHangExecution() throws InterpreterException, IOException {
    // The goal of this test is to ensure that we handle case when the kernel die.
    // In order to do so, we will kill the kernel process from the python code.
    // A real example of that could be a out of memory by the code we execute.
    String codeDep = "!pip install psutil";
    String codeFindPID = "from os import getpid\n" + "import psutil\n" + "pids = psutil.pids()\n" + "my_pid = getpid()\n" + "pidToKill = []\n" + "for pid in pids:\n" + "    try:\n" + "        p = psutil.Process(pid)\n" + "        cmd = p.cmdline()\n" + "        for arg in cmd:\n" + "            if arg.count('ipykernel'):\n" + "                pidToKill.append(pid)\n" + "    except:\n" + "        continue\n" + "len(pidToKill)";
    String codeKillKernel = "from os import kill\n" + "import signal\n" + "for pid in pidToKill:\n" + "    kill(pid, signal.SIGKILL)";
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret(codeDep, context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    result = interpreter.interpret(codeFindPID, context);
    assertEquals(Code.SUCCESS, result.code());
    InterpreterResultMessage output = context.out.toInterpreterResultMessage().get(0);
    int numberOfPID = Integer.parseInt(output.getData());
    assertTrue(numberOfPID > 0);
    context = getInterpreterContext();
    result = interpreter.interpret(codeKillKernel, context);
    assertEquals(Code.ERROR, result.code());
    output = context.out.toInterpreterResultMessage().get(0);
    assertTrue(output.getData(), output.getData().contains("Ipython kernel has been stopped. Please check logs. " + "It might be because of an out of memory issue."));
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Example 19 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class IPythonInterpreterTest method setUp.

@Override
public void setUp() throws InterpreterException {
    Properties properties = initIntpProperties();
    startInterpreter(properties);
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret("import sys\nsys.version_info.major", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    try {
        List<InterpreterResultMessage> messages = context.out.toInterpreterResultMessage();
        if (messages.get(0).getData().equals("2")) {
            isPython2 = true;
        } else {
            isPython2 = false;
        }
    } catch (IOException e) {
        throw new InterpreterException(e);
    }
}
Also used : InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IOException(java.io.IOException) Properties(java.util.Properties) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage)

Example 20 with InterpreterResultMessage

use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.

the class IPythonInterpreterTest method testIPythonPlotting.

@Test
public void testIPythonPlotting() throws InterpreterException, InterruptedException, IOException {
    // matplotlib
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret("%matplotlib inline\n" + "import matplotlib.pyplot as plt\ndata=[1,1,2,3,4]\nplt.figure()\nplt.plot(data)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
    // the order of IMAGE and TEXT is not determined
    // check there must be one IMAGE output
    boolean hasImageOutput = false;
    boolean hasLineText = false;
    for (InterpreterResultMessage msg : interpreterResultMessages) {
        if (msg.getType() == InterpreterResult.Type.IMG) {
            hasImageOutput = true;
        }
        if (msg.getType() == InterpreterResult.Type.TEXT && msg.getData().contains("matplotlib.lines.Line2D")) {
            hasLineText = true;
        }
    }
    assertTrue("No Image Output", hasImageOutput);
    assertTrue("No Line Text", hasLineText);
    if (!enableBokehTest) {
        LOGGER.info("Bokeh test is skipped");
        return;
    }
    // bokeh
    // bokeh initialization
    context = getInterpreterContext();
    result = interpreter.interpret("from bokeh.io import output_notebook, show\n" + "from bokeh.plotting import figure\n" + "output_notebook()", context);
    assertEquals(context.out.toString(), InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    if (interpreterResultMessages.size() == 3) {
        // the first InterpreterResultMessage is empty text for python3 or spark 1.6
        assertEquals(3, interpreterResultMessages.size());
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
        assertTrue(interpreterResultMessages.get(1).getData().contains("Loading BokehJS"));
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(2).getType());
        assertTrue(interpreterResultMessages.get(2).getData().contains("BokehJS is being loaded"));
    } else {
        // the size of interpreterResultMessage is 3 in other cases
        assertEquals(2, interpreterResultMessages.size());
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(0).getType());
        assertTrue(interpreterResultMessages.get(0).getData().contains("Loading BokehJS"));
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
        assertTrue(interpreterResultMessages.get(1).getData().contains("BokehJS is being loaded"));
    }
    // bokeh plotting
    context = getInterpreterContext();
    result = interpreter.interpret("from bokeh.plotting import figure, output_file, show\n" + "x = [1, 2, 3, 4, 5]\n" + "y = [6, 7, 2, 4, 5]\n" + "p = figure(title=\"simple line example\", x_axis_label='x', y_axis_label='y')\n" + "p.line(x, y, legend=\"Temp.\", line_width=2)\n" + "show(p)", context);
    assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    if (interpreterResultMessages.size() == 3) {
        // the first InterpreterResultMessage is empty text for python3 or spark 1.6
        assertEquals(3, interpreterResultMessages.size());
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(2).getType());
        // docs_json is the source data of plotting which bokeh would use to render the plotting.
        assertTrue(interpreterResultMessages.get(2).getData().contains("docs_json"));
    } else {
        // the size of interpreterResultMessage is 3 in other cases
        assertEquals(2, interpreterResultMessages.size());
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(0).getType());
        assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
        // docs_json is the source data of plotting which bokeh would use to render the plotting.
        assertTrue(interpreterResultMessages.get(1).getData().contains("docs_json"));
    }
    // TODO(zjffdu) ggplot is broken https://github.com/yhat/ggpy/issues/662
    // ggplot
    // context = getInterpreterContext();
    // result = interpreter.interpret("from ggplot import *\n" +
    // "ggplot(diamonds, aes(x='price', fill='cut')) +\\\n" +
    // "    geom_density(alpha=0.25) +\\\n" +
    // "    facet_wrap(\"clarity\")", context);
    // assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // interpreterResultMessages = context.out.toInterpreterResultMessage();
    // // the order of IMAGE and TEXT is not determined
    // // check there must be one IMAGE output
    // hasImageOutput = false;
    // for (InterpreterResultMessage msg : interpreterResultMessages) {
    // if (msg.getType() == InterpreterResult.Type.IMG) {
    // hasImageOutput = true;
    // }
    // }
    // assertTrue("No Image Output", hasImageOutput);
    // hvplot
    context = getInterpreterContext();
    result = interpreter.interpret("import pandas as pd, numpy as np\n" + "idx = pd.date_range('1/1/2000', periods=1000)\n" + "df = pd.DataFrame(np.random.randn(1000, 4), index=idx, columns=list('ABCD')).cumsum()\n" + "import hvplot.pandas\n" + "df.hvplot()", context);
    assertEquals(context.out.toInterpreterResultMessage().get(0).getData(), InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertEquals(interpreterResultMessages.size() + ":" + context.out.toString(), 3, interpreterResultMessages.size());
    // the first message is the warning text message.
    assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(0).getType());
    assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
    assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(2).getType());
    // docs_json is the source data of plotting which bokeh would use to render the plotting.
    assertTrue(interpreterResultMessages.get(2).getData().contains("docs_json"));
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Aggregations

InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)80 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)78 Test (org.junit.Test)60 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)55 Properties (java.util.Properties)17 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)15 IOException (java.io.IOException)13 File (java.io.File)8 TimeoutException (java.util.concurrent.TimeoutException)7 Waiter (net.jodah.concurrentunit.Waiter)7 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)6 Test (org.junit.jupiter.api.Test)6 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)3 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Map (java.util.Map)2 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)2 CheckBox (org.apache.zeppelin.display.ui.CheckBox)2 Select (org.apache.zeppelin.display.ui.Select)2