Search in sources :

Example 1 with TextBox

use of org.apache.zeppelin.display.ui.TextBox in project zeppelin by apache.

the class KotlinSparkInterpreterTest method zeppelinContextTest.

@Test
public void zeppelinContextTest() throws Exception {
    InterpreterResult result = interpreter.interpret("z.input(\"name\", \"default_name\")", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("name") instanceof TextBox);
    TextBox textBox = (TextBox) context.getGui().getForms().get("name");
    assertEquals("name", textBox.getName());
    assertEquals("default_name", textBox.getDefaultValue());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) TextBox(org.apache.zeppelin.display.ui.TextBox) Test(org.junit.Test)

Example 2 with TextBox

use of org.apache.zeppelin.display.ui.TextBox in project zeppelin by apache.

the class SparkInterpreterTest method testSparkInterpreter.

@Test
public void testSparkInterpreter() throws IOException, InterruptedException, InterpreterException {
    Properties properties = new Properties();
    properties.setProperty(SparkStringConstants.MASTER_PROP_NAME, "local");
    properties.setProperty(SparkStringConstants.APP_NAME_PROP_NAME, "test");
    properties.setProperty("zeppelin.spark.maxResult", "100");
    properties.setProperty("zeppelin.spark.uiWebUrl", "fake_spark_weburl/{{applicationId}}");
    // disable color output for easy testing
    properties.setProperty("zeppelin.spark.scala.color", "false");
    properties.setProperty("zeppelin.spark.deprecatedMsg.show", "false");
    InterpreterContext context = InterpreterContext.builder().setInterpreterOut(new InterpreterOutput()).setIntpEventClient(mockRemoteEventClient).setAngularObjectRegistry(new AngularObjectRegistry("spark", null)).build();
    InterpreterContext.set(context);
    interpreter = new SparkInterpreter(properties);
    interpreter.setInterpreterGroup(mock(InterpreterGroup.class));
    interpreter.open();
    InterpreterResult result = interpreter.interpret("val a=\"hello world\"", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("a: String = hello world\n", output);
    result = interpreter.interpret("print(a)", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("hello world", output);
    // java stdout
    result = interpreter.interpret("System.out.print(a)", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("hello world", output);
    // incomplete
    result = interpreter.interpret("println(a", getInterpreterContext());
    assertEquals(InterpreterResult.Code.INCOMPLETE, result.code());
    // syntax error
    result = interpreter.interpret("println(b)", getInterpreterContext());
    assertEquals(InterpreterResult.Code.ERROR, result.code());
    assertTrue(output.contains("not found: value b"));
    // multiple line
    result = interpreter.interpret("\"123\".\ntoInt", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // single line comment
    result = interpreter.interpret("print(\"hello world\")/*comment here*/", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("hello world", output);
    result = interpreter.interpret("/*comment here*/\nprint(\"hello world\")", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // multiple line comment
    result = interpreter.interpret("/*line 1 \n line 2*/", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // test function
    result = interpreter.interpret("def add(x:Int, y:Int)\n{ return x+y }", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    result = interpreter.interpret("print(add(1,2))", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    result = interpreter.interpret("/*line 1 \n line 2*/print(\"hello world\")", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    result = interpreter.interpret("$intp", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // Companion object with case class
    result = interpreter.interpret("import scala.math._\n" + "object Circle {\n" + "  private def calculateArea(radius: Double): Double = Pi * pow(radius, 2.0)\n" + "}\n" + "case class Circle(radius: Double) {\n" + "  import Circle._\n" + "  def area: Double = calculateArea(radius)\n" + "}\n" + "\n" + "val circle1 = new Circle(5.0)", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // class extend
    result = interpreter.interpret("import java.util.ArrayList", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    result = interpreter.interpret("class MyArrayList extends ArrayList{}", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // spark rdd operation
    context = getInterpreterContext();
    context.setParagraphId("pid_1");
    result = interpreter.interpret("sc\n.range(1, 10)\n.sum", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertTrue(output.contains("45"));
    ArgumentCaptor<Map> captorEvent = ArgumentCaptor.forClass(Map.class);
    verify(mockRemoteEventClient).onParaInfosReceived(captorEvent.capture());
    assertEquals("pid_1", captorEvent.getValue().get("paraId"));
    reset(mockRemoteEventClient);
    context = getInterpreterContext();
    context.setParagraphId("pid_2");
    result = interpreter.interpret("sc\n.range(1, 10)\n.sum", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertTrue(output.contains("45"));
    captorEvent = ArgumentCaptor.forClass(Map.class);
    verify(mockRemoteEventClient).onParaInfosReceived(captorEvent.capture());
    assertEquals("pid_2", captorEvent.getValue().get("paraId"));
    // spark job url is sent
    ArgumentCaptor<Map> onParaInfosReceivedArg = ArgumentCaptor.forClass(Map.class);
    verify(mockRemoteEventClient).onParaInfosReceived(onParaInfosReceivedArg.capture());
    assertTrue(((String) onParaInfosReceivedArg.getValue().get("jobUrl")).startsWith("fake_spark_weburl/" + interpreter.getJavaSparkContext().sc().applicationId()));
    // case class
    result = interpreter.interpret("val bankText = sc.textFile(\"bank.csv\")", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    result = interpreter.interpret("case class Bank(age:Integer, job:String, marital : String, education : String, balance : Integer)\n" + "val bank = bankText.map(s=>s.split(\";\")).filter(s => s(0)!=\"\\\"age\\\"\").map(\n" + "    s => Bank(s(0).toInt, \n" + "            s(1).replaceAll(\"\\\"\", \"\"),\n" + "            s(2).replaceAll(\"\\\"\", \"\"),\n" + "            s(3).replaceAll(\"\\\"\", \"\"),\n" + "            s(5).replaceAll(\"\\\"\", \"\").toInt\n" + "        )\n" + ").toDF()", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // spark version
    result = interpreter.interpret("sc.version", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // spark sql test
    String version = output.trim();
    if (version.contains("String = 1.")) {
        result = interpreter.interpret("sqlContext", getInterpreterContext());
        assertEquals(InterpreterResult.Code.SUCCESS, result.code());
        result = interpreter.interpret("val df = sqlContext.createDataFrame(Seq((1,\"a\"),(2, null)))\n" + "df.show()", getInterpreterContext());
        assertEquals(InterpreterResult.Code.SUCCESS, result.code());
        assertTrue(output.contains("+---+----+\n" + "| _1|  _2|\n" + "+---+----+\n" + "|  1|   a|\n" + "|  2|null|\n" + "+---+----+"));
    } else {
        // create dataset from case class
        context = getInterpreterContext();
        result = interpreter.interpret("case class Person(id:Int, name:String, age:Int, country:String)\n" + "val df2 = spark.createDataFrame(Seq(Person(1, \"andy\", 20, \"USA\"), " + "Person(2, \"jeff\", 23, \"China\"), Person(3, \"james\", 18, \"USA\")))\n" + "df2.printSchema\n" + "df2.show() ", context);
        assertEquals(InterpreterResult.Code.SUCCESS, result.code());
        result = interpreter.interpret("spark", getInterpreterContext());
        assertEquals(InterpreterResult.Code.SUCCESS, result.code());
        result = interpreter.interpret("val df = spark.createDataFrame(Seq((1,\"a\"),(2, null)))\n" + "df.show()", getInterpreterContext());
        assertEquals(InterpreterResult.Code.SUCCESS, result.code());
        assertTrue(output.contains("+---+----+\n" + "| _1|  _2|\n" + "+---+----+\n" + "|  1|   a|\n" + "|  2|null|\n" + "+---+----+"));
    }
    // ZeppelinContext
    context = getInterpreterContext();
    result = interpreter.interpret("z.show(df)", context);
    assertEquals(context.out.toString(), InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(InterpreterResult.Type.TABLE, messageOutput.getType());
    messageOutput.flush();
    assertEquals("_1\t_2\n1\ta\n2\tnull\n", messageOutput.toInterpreterResultMessage().getData());
    context = getInterpreterContext();
    result = interpreter.interpret("z.input(\"name\", \"default_name\")", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("name") instanceof TextBox);
    TextBox textBox = (TextBox) context.getGui().getForms().get("name");
    assertEquals("name", textBox.getName());
    assertEquals("default_name", textBox.getDefaultValue());
    context = getInterpreterContext();
    result = interpreter.interpret("z.password(\"pwd\")", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("pwd") instanceof Password);
    Password pwd = (Password) context.getGui().getForms().get("pwd");
    assertEquals("pwd", pwd.getName());
    context = getInterpreterContext();
    result = interpreter.interpret("z.checkbox(\"checkbox_1\", Seq((\"value_1\", \"name_1\"), (\"value_2\", \"name_2\")), Seq(\"value_2\"))", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("checkbox_1") instanceof CheckBox);
    CheckBox checkBox = (CheckBox) context.getGui().getForms().get("checkbox_1");
    assertEquals("checkbox_1", checkBox.getName());
    assertEquals(1, checkBox.getDefaultValue().length);
    assertEquals("value_2", checkBox.getDefaultValue()[0]);
    assertEquals(2, checkBox.getOptions().length);
    assertEquals("value_1", checkBox.getOptions()[0].getValue());
    assertEquals("name_1", checkBox.getOptions()[0].getDisplayName());
    assertEquals("value_2", checkBox.getOptions()[1].getValue());
    assertEquals("name_2", checkBox.getOptions()[1].getDisplayName());
    context = getInterpreterContext();
    result = interpreter.interpret("z.select(\"select_1\", Seq((\"value_1\", \"name_1\"), (\"value_2\", \"name_2\")), Seq(\"value_2\"))", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("select_1") instanceof Select);
    Select select = (Select) context.getGui().getForms().get("select_1");
    assertEquals("select_1", select.getName());
    // TODO(zjffdu) it seems a bug of GUI, the default value should be 'value_2', but it is List(value_2)
    // assertEquals("value_2", select.getDefaultValue());
    assertEquals(2, select.getOptions().length);
    assertEquals("value_1", select.getOptions()[0].getValue());
    assertEquals("name_1", select.getOptions()[0].getDisplayName());
    assertEquals("value_2", select.getOptions()[1].getValue());
    assertEquals("name_2", select.getOptions()[1].getDisplayName());
    // completions
    List<InterpreterCompletion> completions = interpreter.completion("a.", 2, getInterpreterContext());
    assertTrue(completions.size() > 0);
    completions = interpreter.completion("a.isEm", 6, getInterpreterContext());
    assertEquals(1, completions.size());
    assertEquals("isEmpty", completions.get(0).name);
    completions = interpreter.completion("sc.ra", 5, getInterpreterContext());
    assertEquals(1, completions.size());
    assertEquals("range", completions.get(0).name);
    // cursor in middle of code
    completions = interpreter.completion("sc.ra\n1+1", 5, getInterpreterContext());
    assertEquals(1, completions.size());
    assertEquals("range", completions.get(0).name);
    // Zeppelin-Display
    result = interpreter.interpret("import org.apache.zeppelin.display.angular.notebookscope._\n" + "import AngularElem._", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    result = interpreter.interpret("<div style=\"color:blue\">\n" + "<h4>Hello Angular Display System</h4>\n" + "</div>.display", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(InterpreterResult.Type.ANGULAR, messageOutput.getType());
    assertTrue(messageOutput.toInterpreterResultMessage().getData().contains("Hello Angular Display System"));
    result = interpreter.interpret("<div class=\"btn btn-success\">\n" + "  Click me\n" + "</div>.onClick{() =>\n" + "  println(\"hello world\")\n" + "}.display", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(InterpreterResult.Type.ANGULAR, messageOutput.getType());
    assertTrue(messageOutput.toInterpreterResultMessage().getData().contains("Click me"));
    // getProgress
    final InterpreterContext context2 = getInterpreterContext();
    Thread interpretThread = new Thread() {

        @Override
        public void run() {
            InterpreterResult result = null;
            try {
                result = interpreter.interpret("val df = sc.parallelize(1 to 10, 5).foreach(e=>Thread.sleep(1000))", context2);
            } catch (InterpreterException e) {
                e.printStackTrace();
            }
            assertEquals(InterpreterResult.Code.SUCCESS, result.code());
        }
    };
    interpretThread.start();
    boolean nonZeroProgress = false;
    int progress = 0;
    while (interpretThread.isAlive()) {
        progress = interpreter.getProgress(context2);
        assertTrue(progress >= 0);
        if (progress != 0 && progress != 100) {
            nonZeroProgress = true;
        }
        Thread.sleep(100);
    }
    assertTrue(nonZeroProgress);
    // cancel
    final InterpreterContext context3 = getInterpreterContext();
    interpretThread = new Thread() {

        @Override
        public void run() {
            InterpreterResult result = null;
            try {
                result = interpreter.interpret("val df = sc.parallelize(1 to 10, 2).foreach(e=>Thread.sleep(1000))", context3);
            } catch (InterpreterException e) {
                e.printStackTrace();
            }
            assertEquals(InterpreterResult.Code.ERROR, result.code());
            assertTrue(output.contains("cancelled"));
        }
    };
    interpretThread.start();
    // sleep 1 second to wait for the spark job start
    Thread.sleep(1000);
    interpreter.cancel(context3);
    interpretThread.join();
}
Also used : InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) TextBox(org.apache.zeppelin.display.ui.TextBox) Properties(java.util.Properties) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) InterpreterOutput(org.apache.zeppelin.interpreter.InterpreterOutput) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) Map(java.util.Map) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Password(org.apache.zeppelin.display.ui.Password) Test(org.junit.Test)

Example 3 with TextBox

use of org.apache.zeppelin.display.ui.TextBox in project zeppelin by apache.

the class BasePythonInterpreterTest method testZeppelinContext.

@Test
public void testZeppelinContext() throws InterpreterException, InterruptedException, IOException {
    // TextBox
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret("z.input(name='text_1', defaultValue='value_1')", context);
    Thread.sleep(100);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertTrue(interpreterResultMessages.get(0).getData().contains("'value_1'"));
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("text_1") instanceof TextBox);
    TextBox textbox = (TextBox) context.getGui().getForms().get("text_1");
    assertEquals("text_1", textbox.getName());
    assertEquals("value_1", textbox.getDefaultValue());
    // Password
    context = getInterpreterContext();
    result = interpreter.interpret("z.password(name='pwd_1')", context);
    Thread.sleep(100);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertTrue(context.getGui().getForms().get("pwd_1") instanceof Password);
    Password password = (Password) context.getGui().getForms().get("pwd_1");
    assertEquals("pwd_1", password.getName());
    // Select
    context = getInterpreterContext();
    result = interpreter.interpret("z.select(name='select_1'," + " options=[('value_1', 'name_1'), ('value_2', 'name_2')])", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("select_1") instanceof Select);
    Select select = (Select) context.getGui().getForms().get("select_1");
    assertEquals("select_1", select.getName());
    assertEquals(2, select.getOptions().length);
    assertEquals("name_1", select.getOptions()[0].getDisplayName());
    assertEquals("value_1", select.getOptions()[0].getValue());
    // CheckBox
    context = getInterpreterContext();
    result = interpreter.interpret("z.checkbox(name='checkbox_1'," + "options=[('value_1', 'name_1'), ('value_2', 'name_2')])", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, context.getGui().getForms().size());
    assertTrue(context.getGui().getForms().get("checkbox_1") instanceof CheckBox);
    CheckBox checkbox = (CheckBox) context.getGui().getForms().get("checkbox_1");
    assertEquals("checkbox_1", checkbox.getName());
    assertEquals(2, checkbox.getOptions().length);
    assertEquals("name_1", checkbox.getOptions()[0].getDisplayName());
    assertEquals("value_1", checkbox.getOptions()[0].getValue());
    // Pandas DataFrame
    context = getInterpreterContext();
    result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3], 'name':['a\ta','b\\nb','c\\r\\nc']})\nz.show(df)", context);
    assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, interpreterResultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
    assertEquals("id\tname\n1\ta a\n2\tb b\n3\tc c\n", interpreterResultMessages.get(0).getData());
    context = getInterpreterContext();
    result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3,4], 'name':['a','b','c', 'd']})\nz.show(df)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertEquals(2, interpreterResultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
    assertEquals("id\tname\n1\ta\n2\tb\n3\tc\n", interpreterResultMessages.get(0).getData());
    assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
    assertEquals("<font color=red>Results are limited by 3.</font>\n", interpreterResultMessages.get(1).getData());
    // z.show(df, show_index=True)
    context = getInterpreterContext();
    result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3], 'name':['a','b','c']})\n" + "z.show(df, show_index=True)", context);
    assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, interpreterResultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
    assertEquals("\tid\tname\n" + "%html <strong>0</strong>\t1\ta\n" + "%html <strong>1</strong>\t2\tb\n" + "%html <strong>2</strong>\t3\tc\n", interpreterResultMessages.get(0).getData());
    // z.show(matplotlib)
    context = getInterpreterContext();
    result = interpreter.interpret("import matplotlib.pyplot as plt\n" + "data=[1,1,2,3,4]\nplt.figure()\nplt.plot(data)\nz.show(plt)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    interpreterResultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, interpreterResultMessages.size());
    assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(0).getType());
    // clear output
    context = getInterpreterContext();
    result = interpreter.interpret("import time\nprint(\"Hello\")\n" + "time.sleep(0.5)\nz.getInterpreterContext().out().clear()\nprint(\"world\")\n", context);
    assertEquals("%text world\n", context.out.getCurrentOutput().toString());
}
Also used : CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) TextBox(org.apache.zeppelin.display.ui.TextBox) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Password(org.apache.zeppelin.display.ui.Password) Test(org.junit.Test)

Example 4 with TextBox

use of org.apache.zeppelin.display.ui.TextBox in project zeppelin by apache.

the class InputTest method testFormExtraction.

@Test
public void testFormExtraction() {
    // textbox form
    String script = "${input_form=}";
    Map<String, Input> forms = Input.extractSimpleQueryForm(script, false);
    assertEquals(1, forms.size());
    Input form = forms.get("input_form");
    assertEquals("input_form", form.name);
    assertEquals("input_form", form.displayName);
    assertEquals("", form.defaultValue);
    assertTrue(form instanceof TextBox);
    // textbox form with display name & default value
    script = "${input_form(Input Form)=xxx}";
    forms = Input.extractSimpleQueryForm(script, false);
    form = forms.get("input_form");
    assertEquals("xxx", form.defaultValue);
    assertTrue(form instanceof TextBox);
    assertEquals("Input Form", form.getDisplayName());
    // password form with display name
    script = "${password:my_pwd(My Password)}";
    forms = Input.extractSimpleQueryForm(script, false);
    form = forms.get("my_pwd");
    assertTrue(form instanceof Password);
    assertEquals("My Password", form.getDisplayName());
    // selection form
    script = "${select_form(Selection Form)=op1,op1|op2(Option 2)|op3}";
    form = Input.extractSimpleQueryForm(script, false).get("select_form");
    assertEquals("select_form", form.name);
    assertEquals("op1", form.defaultValue);
    assertEquals("Selection Form", form.getDisplayName());
    assertTrue(form instanceof Select);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", null), new ParamOption("op2", "Option 2"), new ParamOption("op3", null) }, ((Select) form).getOptions());
    // checkbox form
    script = "${checkbox:checkbox_form=op1,op1|op2|op3}";
    form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
    assertEquals("checkbox_form", form.name);
    assertEquals("checkbox_form", form.displayName);
    assertTrue(form instanceof CheckBox);
    assertArrayEquals(new Object[] { "op1" }, (Object[]) form.defaultValue);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", null), new ParamOption("op2", null), new ParamOption("op3", null) }, ((CheckBox) form).getOptions());
    // checkbox form with multiple default checks
    script = "${checkbox:checkbox_form(Checkbox Form)=op1|op3,op1(Option 1)|op2|op3}";
    form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
    assertEquals("checkbox_form", form.name);
    assertEquals("Checkbox Form", form.displayName);
    assertTrue(form instanceof CheckBox);
    assertArrayEquals(new Object[] { "op1", "op3" }, (Object[]) form.defaultValue);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", "Option 1"), new ParamOption("op2", null), new ParamOption("op3", null) }, ((CheckBox) form).getOptions());
    // checkbox form with no default check
    script = "${checkbox:checkbox_form(Checkbox Form)=,op1(Option 1)|op2(Option 2)|op3(Option 3)}";
    form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
    assertEquals("checkbox_form", form.name);
    assertEquals("Checkbox Form", form.displayName);
    assertTrue(form instanceof CheckBox);
    assertArrayEquals(new Object[] {}, (Object[]) form.defaultValue);
    assertArrayEquals(new ParamOption[] { new ParamOption("op1", "Option 1"), new ParamOption("op2", "Option 2"), new ParamOption("op3", "Option 3") }, ((CheckBox) form).getOptions());
}
Also used : CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) TextBox(org.apache.zeppelin.display.ui.TextBox) Password(org.apache.zeppelin.display.ui.Password) ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption) Test(org.junit.Test)

Example 5 with TextBox

use of org.apache.zeppelin.display.ui.TextBox in project zeppelin by apache.

the class ZeppelinSparkClusterTest method testScalaNoteDynamicForms.

@Test
public void testScalaNoteDynamicForms() throws IOException {
    assumeTrue("Hadoop version mismatch, skip test", isHadoopVersionMatch());
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            Paragraph p1 = note.addNewParagraph(anonymous);
            // create TextBox
            p1.setText("%spark z.noteTextbox(\"name\", \"world\")");
            note.run(p1.getId(), true);
            assertEquals(Status.FINISHED, p1.getStatus());
            Input input = p1.getNote().getNoteForms().get("name");
            assertTrue(input instanceof TextBox);
            TextBox inputTextBox = (TextBox) input;
            assertEquals("name", inputTextBox.getDisplayName());
            assertEquals("world", inputTextBox.getDefaultValue());
            assertEquals("world", p1.getNote().getNoteParams().get("name"));
            Paragraph p2 = note.addNewParagraph(anonymous);
            p2.setText("%md hello $${name}");
            note.run(p2.getId(), true);
            assertEquals(Status.FINISHED, p2.getStatus());
            assertTrue(p2.getReturn().toString(), p2.getReturn().toString().contains("hello world"));
            // create Select
            p1.setText("%spark z.noteSelect(\"language\", Seq((\"java\" -> \"JAVA\"), (\"scala\" -> \"SCALA\")), \"java\")");
            note.run(p1.getId(), true);
            assertEquals(Status.FINISHED, p1.getStatus());
            input = p1.getNote().getNoteForms().get("language");
            assertTrue(input instanceof Select);
            Select select = (Select) input;
            assertEquals("language", select.getDisplayName());
            assertEquals("java", select.getDefaultValue());
            assertEquals("java", p1.getNote().getNoteParams().get("language"));
            p2 = note.addNewParagraph(anonymous);
            p2.setText("%md hello $${language}");
            note.run(p2.getId(), true);
            assertEquals(Status.FINISHED, p2.getStatus());
            assertTrue(p2.getReturn().toString(), p2.getReturn().toString().contains("hello java"));
            // create Checkbox
            p1.setText("%spark z.noteCheckbox(\"languages\", Seq((\"java\" -> \"JAVA\"), (\"scala\" -> \"SCALA\")), Seq(\"java\", \"scala\"))");
            note.run(p1.getId(), true);
            assertEquals(Status.FINISHED, p1.getStatus());
            input = p1.getNote().getNoteForms().get("languages");
            assertTrue(input instanceof CheckBox);
            CheckBox checkbox = (CheckBox) input;
            assertEquals("languages", checkbox.getDisplayName());
            assertArrayEquals(new Object[] { "java", "scala" }, checkbox.getDefaultValue());
            assertEquals(Arrays.asList("java", "scala"), p1.getNote().getNoteParams().get("languages"));
            p2 = note.addNewParagraph(anonymous);
            p2.setText("%md hello $${checkbox:languages}");
            note.run(p2.getId(), true);
            assertEquals(Status.FINISHED, p2.getStatus());
            assertTrue(p2.getReturn().toString(), p2.getReturn().toString().contains("hello java,scala"));
            return null;
        });
    } finally {
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : Input(org.apache.zeppelin.display.Input) Notebook(org.apache.zeppelin.notebook.Notebook) CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) TextBox(org.apache.zeppelin.display.ui.TextBox) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Aggregations

TextBox (org.apache.zeppelin.display.ui.TextBox)12 CheckBox (org.apache.zeppelin.display.ui.CheckBox)8 Select (org.apache.zeppelin.display.ui.Select)8 Test (org.junit.Test)8 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)5 Password (org.apache.zeppelin.display.ui.Password)4 Input (org.apache.zeppelin.display.Input)3 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)3 Notebook (org.apache.zeppelin.notebook.Notebook)3 Paragraph (org.apache.zeppelin.notebook.Paragraph)3 ParamOption (org.apache.zeppelin.display.ui.OptionInput.ParamOption)2 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)2 Map (java.util.Map)1 Properties (java.util.Properties)1 AngularObject (org.apache.zeppelin.display.AngularObject)1 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)1 OptionInput (org.apache.zeppelin.display.ui.OptionInput)1 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)1 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)1 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)1