mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
Python: refactoring to z.show()
This commit is contained in:
parent
41a7302d81
commit
15646a1cc4
5 changed files with 70 additions and 58 deletions
File diff suppressed because one or more lines are too long
|
|
@ -193,12 +193,13 @@ public class PythonInterpreter extends Interpreter {
|
|||
|
||||
private String sendCommandToPython(String cmd) {
|
||||
String output = "";
|
||||
logger.info("Sending : \n" + (cmd.length() > 200 ? cmd.substring(0, 120) + "..." : cmd));
|
||||
logger.info("Sending : \n" + (cmd.length() > 200 ? cmd.substring(0, 200) + "..." : cmd));
|
||||
try {
|
||||
output = process.sendAndGetResult(cmd);
|
||||
} catch (IOException e) {
|
||||
logger.error("Error when sending commands to python process", e);
|
||||
}
|
||||
//logger.info("Got : \n" + output);
|
||||
return output;
|
||||
}
|
||||
|
||||
|
|
|
|||
0
python/src/main/resources/__init__.py
Normal file
0
python/src/main/resources/__init__.py
Normal file
|
|
@ -71,33 +71,41 @@ plt.close()
|
|||
print('''<pre>zeppelin_show(plt,width='50px')
|
||||
zeppelin_show(plt,height='150px') </pre></div>''')
|
||||
|
||||
# Matplotlib show function
|
||||
def zeppelin_show(p, width="0", height="0"):
|
||||
img = io.StringIO()
|
||||
p.savefig(img, format='svg')
|
||||
img.seek(0)
|
||||
style = ""
|
||||
if(width != "0"):
|
||||
style += 'width:'+width
|
||||
if(height != "0"):
|
||||
if(len(style) != 0):
|
||||
style += ","
|
||||
style += 'height:'+height
|
||||
print("%html <div style='" + style + "'>" + img.read() + "<div>")
|
||||
|
||||
# If py4j is detected, these class will be override
|
||||
# with the implementation in bootstrap_input.py
|
||||
class PyZeppelinContext():
|
||||
class PyZeppelinContext(object):
|
||||
""" If py4j is detected, these class will be override
|
||||
with the implementation in bootstrap_input.py
|
||||
"""
|
||||
errorMsg = "You must install py4j Python module " \
|
||||
"(pip install py4j) to use Zeppelin dynamic forms features"
|
||||
|
||||
def __init__(self, zc):
|
||||
self.z = zc
|
||||
|
||||
def input(self, name, defaultValue=""):
|
||||
print (self.errorMsg)
|
||||
|
||||
def select(self, name, options, defaultValue=""):
|
||||
print (self.errorMsg)
|
||||
|
||||
def checkbox(self, name, options, defaultChecked=[]):
|
||||
print (self.errorMsg)
|
||||
|
||||
def show(self, p, width="0", height="0"):
|
||||
"""Matplotlib show function
|
||||
"""
|
||||
img = io.StringIO()
|
||||
p.savefig(img, format='svg')
|
||||
img.seek(0)
|
||||
style = ""
|
||||
if (width != "0"):
|
||||
style += 'width:' + width
|
||||
if (height != "0"):
|
||||
if (len(style) != 0):
|
||||
style += ","
|
||||
style += 'height:' + height
|
||||
print("%html <div style='" + style + "'>" + img.read() + "<div>")
|
||||
|
||||
|
||||
z = PyZeppelinContext("")
|
||||
|
||||
|
|
|
|||
|
|
@ -16,20 +16,23 @@
|
|||
from py4j.java_gateway import JavaGateway
|
||||
from py4j.java_gateway import java_import, JavaGateway, GatewayClient
|
||||
|
||||
|
||||
client = GatewayClient(port=%PORT%)
|
||||
gateway = JavaGateway(client)
|
||||
java_import(gateway.jvm, "org.apache.zeppelin.display.Input")
|
||||
|
||||
class PyZeppelinContext():
|
||||
paramOption = gateway.jvm.org.apache.zeppelin.display.Input.ParamOption
|
||||
javaList = gateway.jvm.java.util.ArrayList
|
||||
|
||||
class Py4jZeppelinContext(PyZeppelinContext):
|
||||
"""A context impl that uses Py4j to communicate to JVM
|
||||
"""
|
||||
def __init__(self, zc):
|
||||
self.z = zc
|
||||
|
||||
super(Py4jZeppelinContext, self).__init__(zc)
|
||||
self.paramOption = gateway.jvm.org.apache.zeppelin.display.Input.ParamOption
|
||||
self.javaList = gateway.jvm.java.util.ArrayList
|
||||
|
||||
def input(self, name, defaultValue=""):
|
||||
return self.z.getGui().input(name, defaultValue)
|
||||
|
||||
|
||||
def select(self, name, options, defaultValue=""):
|
||||
javaOptions = gateway.new_array(self.paramOption, len(options))
|
||||
i = 0
|
||||
|
|
@ -37,7 +40,7 @@ class PyZeppelinContext():
|
|||
javaOptions[i] = self.paramOption(tuple[0], tuple[1])
|
||||
i += 1
|
||||
return self.z.getGui().select(name, defaultValue, javaOptions)
|
||||
|
||||
|
||||
def checkbox(self, name, options, defaultChecked=[]):
|
||||
javaOptions = gateway.new_array(self.paramOption, len(options))
|
||||
i = 0
|
||||
|
|
@ -49,4 +52,5 @@ class PyZeppelinContext():
|
|||
javaDefaultCheck.append(check)
|
||||
return self.z.getGui().checkbox(name, javaDefaultCheck, javaOptions)
|
||||
|
||||
z = PyZeppelinContext(gateway.entry_point)
|
||||
|
||||
z = Py4jZeppelinContext(gateway.entry_point)
|
||||
|
|
|
|||
Loading…
Reference in a new issue