Search in sources :

Example 11 with TextBox

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

the class GUI method textbox.

public Object textbox(String id, String defaultValue) {
    // first find values from client and then use default
    Object value = params.get(id);
    if (value == null) {
        value = defaultValue;
    }
    forms.put(id, new TextBox(id, defaultValue));
    params.put(id, value);
    return value;
}
Also used : TextBox(org.apache.zeppelin.display.ui.TextBox)

Example 12 with TextBox

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

the class FlinkInterpreterTest method testScalaBasic.

@Test
public void testScalaBasic() throws InterpreterException, IOException {
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = interpreter.interpret("val a=\"hello world\"", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
    assertEquals("a: String = hello world\n", resultMessages.get(0).getData());
    context = getInterpreterContext();
    result = interpreter.interpret("print(a)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
    assertEquals("hello world", resultMessages.get(0).getData());
    // java stdout
    context = getInterpreterContext();
    result = interpreter.interpret("System.out.print(a)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
    assertEquals("hello world", resultMessages.get(0).getData());
    // java stderr
    context = getInterpreterContext();
    result = interpreter.interpret("System.err.print(a)", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
    assertEquals("hello world", resultMessages.get(0).getData());
    // incomplete
    result = interpreter.interpret("println(a", getInterpreterContext());
    assertEquals(InterpreterResult.Code.INCOMPLETE, result.code());
    // syntax error
    context = getInterpreterContext();
    result = interpreter.interpret("println(b)", context);
    assertEquals(InterpreterResult.Code.ERROR, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
    assertTrue(resultMessages.get(0).getData(), resultMessages.get(0).getData().contains("not found: value b"));
    // multiple line
    context = getInterpreterContext();
    result = interpreter.interpret("\"123\".\ntoInt", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // single line comment
    context = getInterpreterContext();
    result = interpreter.interpret("/*comment here*/", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    result = interpreter.interpret("/*comment here*/\nprint(\"hello world\")", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // multiple line comment
    context = getInterpreterContext();
    result = interpreter.interpret("/*line 1 \n line 2*/", context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // test function
    context = getInterpreterContext();
    result = interpreter.interpret("def add(x:Int, y:Int)\n{ return x+y }", context);
    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());
    // companion object
    result = interpreter.interpret("class Counter {\n " + "var value: Long = 0} \n" + "object Counter {\n def apply(x: Long) = new Counter()\n}", getInterpreterContext());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // case class
    // context = getInterpreterContext();result = interpreter.interpret(
    // "case class WC(word: String, count: Int)\n" +
    // "val wordCounts = benv.fromElements(\n" +
    // "WC(\"hello\", 1),\n" +
    // "WC(\"world\", 2),\n" +
    // "WC(\"world\", 8))\n" +
    // "wordCounts.collect()",
    // context);
    // assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    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.checkbox(\"checkbox_1\", " + "Seq(\"value_2\"), Seq((\"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(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_2\"), " + "Seq((\"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());
    // 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());
}
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) 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