Search in sources :

Example 6 with CheckBox

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

the class GUI method convertFromOldInput.

private Input convertFromOldInput(OldInput oldInput) {
    Input convertedInput = null;
    if (oldInput.options == null || oldInput instanceof OldInput.OldTextBox) {
        convertedInput = new TextBox(oldInput.name, oldInput.defaultValue.toString());
    } else if (oldInput instanceof OldInput.OldCheckBox) {
        convertedInput = new CheckBox(oldInput.name, (List) oldInput.defaultValue, oldInput.options);
    } else if (oldInput instanceof OldInput && oldInput.options != null) {
        convertedInput = new Select(oldInput.name, oldInput.defaultValue, oldInput.options);
    } else {
        throw new RuntimeException("Can not convert this OldInput.");
    }
    convertedInput.setDisplayName(oldInput.getDisplayName());
    convertedInput.setHidden(oldInput.isHidden());
    convertedInput.setArgument(oldInput.getArgument());
    return convertedInput;
}
Also used : CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) TextBox(org.apache.zeppelin.display.ui.TextBox)

Example 7 with CheckBox

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

the class Input method getInputForm.

private static Input getInputForm(Matcher match) {
    String hiddenPart = match.group(1);
    boolean hidden = false;
    if ("_".equals(hiddenPart)) {
        hidden = true;
    }
    String m = match.group(2);
    String namePart;
    String valuePart;
    int p = m.indexOf('=');
    if (p > 0) {
        namePart = m.substring(0, p);
        valuePart = m.substring(p + 1);
    } else {
        namePart = m;
        valuePart = null;
    }
    String varName;
    String displayName = null;
    String type = null;
    String arg = null;
    Object defaultValue = null;
    ParamOption[] paramOptions = null;
    // get var name type
    String varNamePart;
    String[] typeArray = getType(namePart);
    if (typeArray != null) {
        type = typeArray[0];
        arg = typeArray[1];
        varNamePart = typeArray[2];
    } else {
        varNamePart = namePart;
    }
    // get var name and displayname
    String[] varNameArray = getNameAndDisplayName(varNamePart);
    if (varNameArray != null) {
        varName = varNameArray[0];
        displayName = varNameArray[1];
    } else {
        varName = varNamePart.trim();
    }
    // get defaultValue
    if (valuePart != null) {
        // find default value
        int optionP = valuePart.indexOf(",");
        if (optionP >= 0) {
            // option available
            defaultValue = valuePart.substring(0, optionP);
            if (type != null && type.equals("checkbox")) {
                // checkbox may contain multiple default checks
                defaultValue = Input.splitPipe((String) defaultValue);
            }
            String optionPart = valuePart.substring(optionP + 1);
            String[] options = Input.splitPipe(optionPart);
            paramOptions = new ParamOption[options.length];
            for (int i = 0; i < options.length; i++) {
                String[] optNameArray = getNameAndDisplayName(options[i]);
                if (optNameArray != null) {
                    paramOptions[i] = new ParamOption(optNameArray[0], optNameArray[1]);
                } else {
                    paramOptions[i] = new ParamOption(options[i], null);
                }
            }
        } else {
            // no option
            defaultValue = valuePart;
        }
    }
    Input input = null;
    if (type == null) {
        if (paramOptions == null) {
            input = new TextBox(varName, (String) defaultValue);
        } else {
            input = new Select(varName, defaultValue, paramOptions);
        }
    } else if (type.equals("checkbox")) {
        input = new CheckBox(varName, (Object[]) defaultValue, paramOptions);
    } else if (type.equals("password")) {
        input = new Password(varName);
    } else {
        throw new RuntimeException("Could not recognize dynamic form with type: " + type);
    }
    input.setArgument(arg);
    if (!StringUtils.isBlank(displayName)) {
        // only set displayName when it is not empty (user explicitly specify it)
        // e.g. ${name(display_name)=value)
        input.setDisplayName(displayName);
    }
    input.setHidden(hidden);
    return input;
}
Also used : TextBox(org.apache.zeppelin.display.ui.TextBox) ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption) OptionInput(org.apache.zeppelin.display.ui.OptionInput) CheckBox(org.apache.zeppelin.display.ui.CheckBox) Select(org.apache.zeppelin.display.ui.Select) Password(org.apache.zeppelin.display.ui.Password)

Example 8 with CheckBox

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

the class GUI method checkbox.

public List<Object> checkbox(String id, ParamOption[] options, Collection<Object> defaultChecked) {
    Collection<Object> checked = (Collection<Object>) params.get(id);
    if (checked == null) {
        checked = defaultChecked;
    }
    forms.put(id, new CheckBox(id, defaultChecked, options));
    List<Object> filtered = new LinkedList<>();
    for (Object o : checked) {
        if (isValidOption(o, options)) {
            filtered.add(o);
        }
    }
    params.put(id, filtered);
    return filtered;
}
Also used : CheckBox(org.apache.zeppelin.display.ui.CheckBox) Collection(java.util.Collection) LinkedList(java.util.LinkedList)

Example 9 with CheckBox

use of org.apache.zeppelin.display.ui.CheckBox 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

CheckBox (org.apache.zeppelin.display.ui.CheckBox)9 Select (org.apache.zeppelin.display.ui.Select)8 TextBox (org.apache.zeppelin.display.ui.TextBox)8 Test (org.junit.Test)6 Password (org.apache.zeppelin.display.ui.Password)4 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)3 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)3 Input (org.apache.zeppelin.display.Input)2 ParamOption (org.apache.zeppelin.display.ui.OptionInput.ParamOption)2 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)2 Notebook (org.apache.zeppelin.notebook.Notebook)2 Paragraph (org.apache.zeppelin.notebook.Paragraph)2 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)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