Skip to content

Commit

Permalink
[HOTFIX] Dynamic form in python interpreter
Browse files Browse the repository at this point in the history
### What is this PR for?
#2106 rewrote python interpreter. But dynamic form feature is not rewritten correctly.

### What type of PR is it?
Hot Fix

### Todos
* [x] - Bring dynamic form back

### What is the Jira issue?
#2106

### How should this be tested?
run

```
%python
print("Hello "+z.input("name", "sun"))
```

```
%python
print("Hello "+z.select("day", [("1","mon"),
                                ("2","tue"),
                                ("3","wed"),
                                ("4","thurs"),
                                ("5","fri"),
                                ("6","sat"),
                                ("7","sun")]))
```

```
%python
options = [("apple","Apple"), ("banana","Banana"), ("orange","Orange")]
print("Hello "+ " and ".join(z.checkbox("fruit", options, ["apple"])))
```

### Questions:
* Does the licenses files need update? no
* Is there breaking changes for older versions? no
* Does this needs documentation? no

Author: Lee moon soo <moon@apache.org>

Closes #2155 from Leemoonsoo/python_get_interpreter_context and squashes the following commits:

c5e584a [Lee moon soo] fix matplotlib display error on python 3.4
3e6603b [Lee moon soo] correctly handle zeppelin.python property.
5be8db4 [Lee moon soo] Expose a method to get InterpreterOutput, so user can call InterpreterOutput.clear()
a405a93 [Lee moon soo] implement dynamic form
  • Loading branch information
Leemoonsoo committed Mar 20, 2017
1 parent 632815c commit 1972a58
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
6 changes: 5 additions & 1 deletion interpreter/lib/python/backend_zinline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from __future__ import print_function

import sys
import uuid
import warnings
import base64
Expand Down Expand Up @@ -94,7 +95,10 @@ def get_bytes(self, **kwargs):
buf = BytesIO()
self.print_figure(buf, **kwargs)
fmt = fmt.encode()
byte_str = b"data:image/%s;base64," %fmt
if sys.version_info >= (3, 4) and sys.version_info < (3, 5):
byte_str = bytes("data:image/%s;base64," %fmt, "utf-8")
else:
byte_str = b"data:image/%s;base64," %fmt
byte_str += base64.b64encode(buf.getvalue())

# Python3 forces all strings to default to unicode, but for raster image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public InterpreterResult interpret(String st, InterpreterContext context) {
mountPythonScript +
mountPy4j +
"-e PYTHONPATH=\"" + pythonPath + "\" " +
image +
" python /_zeppelin_tmp/" + pythonScript.getName());
image + " " +
getPythonInterpreter().getPythonBindPath() + " " +
"/_zeppelin_tmp/" + pythonScript.getName());
restartPythonProcess();
out.clear();
return new InterpreterResult(InterpreterResult.Code.SUCCESS, "\"" + image + "\" activated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ public InterpreterResult interpret(String cmd, InterpreterContext contextInterpr
}
}

public InterpreterContext getCurrentInterpreterContext() {
return context;
}

public void interrupt() throws IOException {
if (pythonPid > -1) {
logger.info("Sending SIGINT signal to PID : " + pythonPid);
Expand Down Expand Up @@ -440,14 +444,23 @@ public void setPythonCommand(String cmd) {
pythonCommand = cmd;
}

public String getPythonCommand() {
private String getPythonCommand() {
if (pythonCommand == null) {
return DEFAULT_ZEPPELIN_PYTHON;
return getPythonBindPath();
} else {
return pythonCommand;
}
}

public String getPythonBindPath() {
String path = getProperty("zeppelin.python");
if (path == null) {
return DEFAULT_ZEPPELIN_PYTHON;
} else {
return path;
}
}

private Job getRunningJob(String paragraphId) {
Job foundJob = null;
Collection<Job> jobsRunning = getScheduler().getJobsRunning();
Expand Down
35 changes: 26 additions & 9 deletions python/src/main/resources/python/zeppelin_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,41 @@ def flush(self):


class PyZeppelinContext(object):
""" If py4j is detected, these class will be override
with the implementation in bootstrap_input.py
""" A context impl that uses Py4j to communicate to JVM
"""
errorMsg = "You must install py4j Python module " \
"(pip install py4j) to use Zeppelin dynamic forms features"

def __init__(self):
def __init__(self, z):
self.z = z
self.paramOption = gateway.jvm.org.apache.zeppelin.display.Input.ParamOption
self.javaList = gateway.jvm.java.util.ArrayList
self.max_result = 1000
self._displayhook = lambda *args: None
self._setup_matplotlib()

def getInterpreterContext(self):
return self.z.getCurrentInterpreterContext()

def input(self, name, defaultValue=""):
print(self.errorMsg)
return self.z.getGui().input(name, defaultValue)

def select(self, name, options, defaultValue=""):
print(self.errorMsg)
javaOptions = gateway.new_array(self.paramOption, len(options))
i = 0
for tuple in options:
javaOptions[i] = self.paramOption(tuple[0], tuple[1])
i += 1
return self.z.getGui().select(name, defaultValue, javaOptions)

def checkbox(self, name, options, defaultChecked=[]):
print(self.errorMsg)
javaOptions = gateway.new_array(self.paramOption, len(options))
i = 0
for tuple in options:
javaOptions[i] = self.paramOption(tuple[0], tuple[1])
i += 1
javaDefaultCheck = self.javaList()
for check in defaultChecked:
javaDefaultCheck.append(check)
return self.z.getGui().checkbox(name, javaDefaultCheck, javaOptions)

def show(self, p, **kwargs):
if hasattr(p, '__name__') and p.__name__ == "matplotlib.pyplot":
Expand Down Expand Up @@ -187,7 +203,8 @@ def handler_stop_signals(sig, frame):
intp = gateway.entry_point
intp.onPythonScriptInitialized(os.getpid())

z = PyZeppelinContext()
java_import(gateway.jvm, "org.apache.zeppelin.display.Input")
z = PyZeppelinContext(intp)
z._setup_matplotlib()

output = Logger()
Expand Down
3 changes: 3 additions & 0 deletions spark/src/main/resources/python/zeppelin_pyspark.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def put(self, key, value):
def get(self, key):
return self.__getitem__(key)

def getInterpreterContext(self):
return self.z.getInterpreterContext()

def input(self, name, defaultValue=""):
return self.z.input(name, defaultValue)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ public RemoteWorksController getRemoteWorksController() {
public void setRemoteWorksController(RemoteWorksController remoteWorksController) {
this.remoteWorksController = remoteWorksController;
}

public InterpreterOutput out() {
return out;
}
}

0 comments on commit 1972a58

Please sign in to comment.