Search in sources :

Example 6 with TextBox

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

the class ZeppelinSparkClusterTest method testPythonNoteDynamicForms.

@Test
public void testPythonNoteDynamicForms() 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.pyspark 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.pyspark z.noteSelect('language', [('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.pyspark z.noteCheckbox('languages', [('java', 'JAVA'), ('scala', 'SCALA')], ['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)

Example 7 with TextBox

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

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

the class NoteTest method testNoteJson.

public void testNoteJson() throws IOException {
    Note note = new Note("test", "", interpreterFactory, interpreterSettingManager, paragraphJobListener, credentials, noteEventListener);
    note.setName("/test_note");
    note.getConfig().put("config_1", "value_1");
    note.getInfo().put("info_1", "value_1");
    String pText = "%spark sc.version";
    Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
    p.setText(pText);
    p.setResult(new InterpreterResult(InterpreterResult.Code.SUCCESS, "1.6.2"));
    p.settings.getForms().put("textbox_1", new TextBox("name", "default_name"));
    p.settings.getParams().put("textbox_1", "my_name");
    note.getAngularObjects().put("ao_1", Arrays.asList(new AngularObject("name_1", "value_1", note.getId(), p.getId(), null)));
    // test Paragraph Json
    Paragraph p2 = Paragraph.fromJson(p.toJson());
    assertEquals(p2.settings, p.settings);
    assertEquals(p2, p);
    // test Note Json
    Note note2 = Note.fromJson(null, note.toJson());
    assertEquals(note2, note);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) AngularObject(org.apache.zeppelin.display.AngularObject) TextBox(org.apache.zeppelin.display.ui.TextBox)

Example 9 with TextBox

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

the class ZeppelinSparkClusterTest method testRNoteDynamicForms.

@Test
public void testRNoteDynamicForms() 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.r 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"));
            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) TextBox(org.apache.zeppelin.display.ui.TextBox) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 10 with TextBox

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

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