Search in sources :

Example 1 with Input

use of org.apache.zeppelin.display.Input in project zeppelin by apache.

the class Note method addCloneParagraph.

/**
   * Clone paragraph and add it to note.
   *
   * @param srcParagraph source paragraph
   */
void addCloneParagraph(Paragraph srcParagraph) {
    // Keep paragraph original ID
    final Paragraph newParagraph = new Paragraph(srcParagraph.getId(), this, this, factory, interpreterSettingManager);
    Map<String, Object> config = new HashMap<>(srcParagraph.getConfig());
    Map<String, Object> param = new HashMap<>(srcParagraph.settings.getParams());
    Map<String, Input> form = new HashMap<>(srcParagraph.settings.getForms());
    newParagraph.setConfig(config);
    newParagraph.settings.setParams(param);
    newParagraph.settings.setForms(form);
    newParagraph.setText(srcParagraph.getText());
    newParagraph.setTitle(srcParagraph.getTitle());
    try {
        Gson gson = new Gson();
        String resultJson = gson.toJson(srcParagraph.getReturn());
        InterpreterResult result = gson.fromJson(resultJson, InterpreterResult.class);
        newParagraph.setReturn(result, null);
    } catch (Exception e) {
        // 'result' part of Note consists of exception, instead of actual interpreter results
        logger.warn("Paragraph " + srcParagraph.getId() + " has a result with exception. " + e.getMessage());
    }
    synchronized (paragraphs) {
        paragraphs.add(newParagraph);
    }
    if (noteEventListener != null) {
        noteEventListener.onParagraphCreate(newParagraph);
    }
}
Also used : Input(org.apache.zeppelin.display.Input) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException)

Example 2 with Input

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

Example 3 with Input

use of org.apache.zeppelin.display.Input 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 4 with Input

use of org.apache.zeppelin.display.Input in project zeppelin by apache.

the class ParagraphTest method should_extract_variable_from_angular_object_registry.

@Test
public void should_extract_variable_from_angular_object_registry() throws Exception {
    // Given
    final String noteId = "noteId";
    final AngularObjectRegistry registry = mock(AngularObjectRegistry.class);
    final Note note = mock(Note.class);
    final Map<String, Input> inputs = new HashMap<>();
    inputs.put("name", null);
    inputs.put("age", null);
    inputs.put("job", null);
    final String scriptBody = "My name is ${name} and I am ${age=20} years old. " + "My occupation is ${ job = engineer | developer | artists}";
    final Paragraph paragraph = new Paragraph(note, null);
    final String paragraphId = paragraph.getId();
    final AngularObject<String> nameAO = AngularObjectBuilder.build("name", "DuyHai DOAN", noteId, paragraphId);
    final AngularObject<Integer> ageAO = AngularObjectBuilder.build("age", 34, noteId, null);
    when(note.getId()).thenReturn(noteId);
    when(registry.get("name", noteId, paragraphId)).thenReturn(nameAO);
    when(registry.get("age", noteId, null)).thenReturn(ageAO);
    final String expected = "My name is DuyHai DOAN and I am 34 years old. " + "My occupation is ${ job = engineer | developer | artists}";
    // When
    final String actual = paragraph.extractVariablesFromAngularRegistry(scriptBody, inputs, registry);
    // Then
    verify(registry).get("name", noteId, paragraphId);
    verify(registry).get("age", noteId, null);
    assertEquals(actual, expected);
}
Also used : Input(org.apache.zeppelin.display.Input) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 5 with Input

use of org.apache.zeppelin.display.Input in project zeppelin by apache.

the class Paragraph method jobRun.

@Override
protected InterpreterResult jobRun() throws Throwable {
    try {
        if (localProperties.getOrDefault("isRecover", "false").equals("false")) {
            this.runtimeInfos.clear();
        }
        this.interpreter = getBindedInterpreter();
        if (this.interpreter == null) {
            LOGGER.error("Can not find interpreter name " + intpText);
            throw new RuntimeException("Can not find interpreter for " + intpText);
        }
        LOGGER.info("Run paragraph [paragraph_id: {}, interpreter: {}, note_id: {}, user: {}]", getId(), this.interpreter.getClassName(), note.getId(), subject.getUser());
        InterpreterSetting interpreterSetting = ((ManagedInterpreterGroup) interpreter.getInterpreterGroup()).getInterpreterSetting();
        if (interpreterSetting.getStatus() != InterpreterSetting.Status.READY) {
            String message = String.format("Interpreter Setting '%s' is not ready, its status is %s", interpreterSetting.getName(), interpreterSetting.getStatus());
            LOGGER.error(message);
            throw new RuntimeException(message);
        }
        if (this.user != null) {
            if (subject != null && !interpreterSetting.isUserAuthorized(subject.getUsersAndRoles())) {
                String msg = String.format("%s has no permission for %s", subject.getUser(), intpText);
                LOGGER.error(msg);
                return new InterpreterResult(Code.ERROR, msg);
            }
        }
        for (Paragraph p : userParagraphMap.values()) {
            p.setText(getText());
        }
        // inject form
        String script = this.scriptText;
        String form = localProperties.getOrDefault("form", interpreter.getFormType().name());
        if (form.equalsIgnoreCase("simple")) {
            // inputs will be built from script body
            LinkedHashMap<String, Input> inputs = Input.extractSimpleQueryForm(script, false);
            LinkedHashMap<String, Input> noteInputs = Input.extractSimpleQueryForm(script, true);
            final AngularObjectRegistry angularRegistry = interpreter.getInterpreterGroup().getAngularObjectRegistry();
            String scriptBody = extractVariablesFromAngularRegistry(script, inputs, angularRegistry);
            settings.setForms(inputs);
            if (!noteInputs.isEmpty()) {
                if (!note.getNoteForms().isEmpty()) {
                    Map<String, Input> currentNoteForms = note.getNoteForms();
                    for (String s : noteInputs.keySet()) {
                        if (!currentNoteForms.containsKey(s)) {
                            currentNoteForms.put(s, noteInputs.get(s));
                        }
                    }
                } else {
                    note.setNoteForms(noteInputs);
                }
            }
            script = Input.getSimpleQuery(note.getNoteParams(), scriptBody, true);
            script = Input.getSimpleQuery(settings.getParams(), script, false);
        } else {
            settings.clear();
        }
        LOGGER.debug("RUN : " + script);
        try {
            InterpreterContext context = getInterpreterContext();
            InterpreterContext.set(context);
            // Inject credentials
            String injectPropStr = interpreter.getProperty(Constants.INJECT_CREDENTIALS, "false");
            injectPropStr = context.getStringLocalProperty(Constants.INJECT_CREDENTIALS, injectPropStr);
            boolean shouldInjectCredentials = Boolean.parseBoolean(injectPropStr);
            InterpreterResult ret = null;
            if (shouldInjectCredentials) {
                UserCredentials creds = context.getAuthenticationInfo().getUserCredentials();
                CredentialInjector credinjector = new CredentialInjector(creds);
                String code = credinjector.replaceCredentials(script);
                ret = interpreter.interpret(code, context);
                ret = credinjector.hidePasswords(ret);
            } else {
                ret = interpreter.interpret(script, context);
            }
            if (interpreter.getFormType() == FormType.NATIVE) {
                note.setNoteParams(context.getNoteGui().getParams());
                note.setNoteForms(context.getNoteGui().getForms());
            }
            if (Code.KEEP_PREVIOUS_RESULT == ret.code()) {
                return getReturn();
            }
            Paragraph p = getUserParagraph(getUser());
            if (null != p) {
                p.setResult(ret);
                p.settings.setParams(settings.getParams());
            }
            return ret;
        } finally {
            InterpreterContext.remove();
        }
    } catch (Exception e) {
        return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
    } finally {
        localProperties.remove("isRecover");
    }
}
Also used : InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) Input(org.apache.zeppelin.display.Input) UserCredentials(org.apache.zeppelin.user.UserCredentials) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Aggregations

Input (org.apache.zeppelin.display.Input)10 Test (org.junit.Test)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 AngularObject (org.apache.zeppelin.display.AngularObject)4 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)3 TextBox (org.apache.zeppelin.display.ui.TextBox)3 Notebook (org.apache.zeppelin.notebook.Notebook)3 Paragraph (org.apache.zeppelin.notebook.Paragraph)3 Gson (com.google.gson.Gson)2 LinkedHashMap (java.util.LinkedHashMap)2 CheckBox (org.apache.zeppelin.display.ui.CheckBox)2 Select (org.apache.zeppelin.display.ui.Select)2 AbstractInterpreterTest (org.apache.zeppelin.interpreter.AbstractInterpreterTest)2 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)2 InterpreterNotFoundException (org.apache.zeppelin.interpreter.InterpreterNotFoundException)2 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ArrayList (java.util.ArrayList)1 GUI (org.apache.zeppelin.display.GUI)1