mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
ZEPPELIN-2390. Improve returnType for z.checkbox
This commit is contained in:
parent
540ceb87d8
commit
75e3afcf2f
4 changed files with 61 additions and 16 deletions
|
|
@ -136,7 +136,7 @@ public class ZeppelinContext {
|
|||
}
|
||||
|
||||
@ZeppelinApi
|
||||
public scala.collection.Iterable<Object> checkbox(String name,
|
||||
public scala.collection.Seq<Object> checkbox(String name,
|
||||
scala.collection.Iterable<Tuple2<Object, String>> options) {
|
||||
List<Object> allChecked = new LinkedList<>();
|
||||
for (Tuple2<Object, String> option : asJavaIterable(options)) {
|
||||
|
|
@ -146,11 +146,12 @@ public class ZeppelinContext {
|
|||
}
|
||||
|
||||
@ZeppelinApi
|
||||
public scala.collection.Iterable<Object> checkbox(String name,
|
||||
public scala.collection.Seq<Object> checkbox(String name,
|
||||
scala.collection.Iterable<Object> defaultChecked,
|
||||
scala.collection.Iterable<Tuple2<Object, String>> options) {
|
||||
return collectionAsScalaIterable(gui.checkbox(name, asJavaCollection(defaultChecked),
|
||||
tuplesToParamOptions(options)));
|
||||
return scala.collection.JavaConversions.asScalaBuffer(
|
||||
gui.checkbox(name, asJavaCollection(defaultChecked),
|
||||
tuplesToParamOptions(options))).toSeq();
|
||||
}
|
||||
|
||||
private ParamOption[] tuplesToParamOptions(
|
||||
|
|
|
|||
|
|
@ -97,13 +97,15 @@ class PyZeppelinContext(dict):
|
|||
|
||||
def checkbox(self, name, options, defaultChecked=None):
|
||||
if defaultChecked is None:
|
||||
defaultChecked = list(map(lambda items: items[0], options))
|
||||
defaultChecked = []
|
||||
optionTuples = list(map(lambda items: self.__tupleToScalaTuple2(items), options))
|
||||
optionIterables = gateway.jvm.scala.collection.JavaConversions.collectionAsScalaIterable(optionTuples)
|
||||
defaultCheckedIterables = gateway.jvm.scala.collection.JavaConversions.collectionAsScalaIterable(defaultChecked)
|
||||
|
||||
checkedIterables = self.z.checkbox(name, defaultCheckedIterables, optionIterables)
|
||||
return gateway.jvm.scala.collection.JavaConversions.asJavaCollection(checkedIterables)
|
||||
checkedItems = gateway.jvm.scala.collection.JavaConversions.seqAsJavaList(self.z.checkbox(name, defaultCheckedIterables, optionIterables))
|
||||
result = []
|
||||
for checkedItem in checkedItems:
|
||||
result.append(checkedItem)
|
||||
return result;
|
||||
|
||||
def registerHook(self, event, cmd, replName=None):
|
||||
if replName is None:
|
||||
|
|
|
|||
|
|
@ -75,14 +75,14 @@ public class GUI implements Serializable {
|
|||
return value;
|
||||
}
|
||||
|
||||
public Collection<Object> checkbox(String id, Collection<Object> defaultChecked,
|
||||
public List<Object> checkbox(String id, Collection<Object> defaultChecked,
|
||||
ParamOption[] options) {
|
||||
Collection<Object> checked = (Collection<Object>) params.get(id);
|
||||
if (checked == null) {
|
||||
checked = defaultChecked;
|
||||
}
|
||||
forms.put(id, new Input(id, defaultChecked, "checkbox", options));
|
||||
Collection<Object> filtered = new LinkedList<>();
|
||||
List<Object> filtered = new LinkedList<>();
|
||||
for (Object o : checked) {
|
||||
if (isValidOption(o, options)) {
|
||||
filtered.add(o);
|
||||
|
|
|
|||
|
|
@ -483,18 +483,19 @@ public class ZeppelinSparkClusterTest extends AbstractTestRestApi {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testZeppelinContextDynamicForms() throws IOException {
|
||||
public void testSparkZeppelinContextDynamicForms() throws IOException {
|
||||
Note note = ZeppelinServer.notebook.createNote(anonymous);
|
||||
Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
|
||||
note.setName("note");
|
||||
Map config = p.getConfig();
|
||||
config.put("enabled", true);
|
||||
p.setConfig(config);
|
||||
String code = "%spark.spark z.input(\"my_input\", \"default_name\")\n" +
|
||||
"z.select(\"my_select\", \"select_2\"," +
|
||||
"Seq((\"1\", \"select_1\"), (\"2\", \"select_2\")))\n" +
|
||||
"z.checkbox(\"my_checkbox\", Seq(\"check_1\"), " +
|
||||
"Seq((\"1\", \"check_1\"), (\"2\", \"check_2\")))";
|
||||
String code = "%spark.spark println(z.input(\"my_input\", \"default_name\"))\n" +
|
||||
"println(z.select(\"my_select\", \"1\"," +
|
||||
"Seq((\"1\", \"select_1\"), (\"2\", \"select_2\"))))\n" +
|
||||
"val items=z.checkbox(\"my_checkbox\", Seq(\"2\"), " +
|
||||
"Seq((\"1\", \"check_1\"), (\"2\", \"check_2\")))\n" +
|
||||
"println(items(0))";
|
||||
p.setText(code);
|
||||
p.setAuthenticationInfo(anonymous);
|
||||
note.run(p.getId());
|
||||
|
|
@ -505,5 +506,46 @@ public class ZeppelinSparkClusterTest extends AbstractTestRestApi {
|
|||
assert(formIter.next().equals("my_input"));
|
||||
assert(formIter.next().equals("my_select"));
|
||||
assert(formIter.next().equals("my_checkbox"));
|
||||
|
||||
// check dynamic forms values
|
||||
String[] result = p.getResult().message().get(0).getData().split("\n");
|
||||
assertEquals(4, result.length);
|
||||
assertEquals("default_name", result[0]);
|
||||
assertEquals("1", result[1]);
|
||||
assertEquals("items: Seq[Object] = Buffer(2)", result[2]);
|
||||
assertEquals("2", result[3]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPySparkZeppelinContextDynamicForms() throws IOException {
|
||||
Note note = ZeppelinServer.notebook.createNote(anonymous);
|
||||
Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
|
||||
note.setName("note");
|
||||
Map config = p.getConfig();
|
||||
config.put("enabled", true);
|
||||
p.setConfig(config);
|
||||
String code = "%spark.pyspark print(z.input('my_input', 'default_name'))\n" +
|
||||
"print(z.select('my_select', " +
|
||||
"[('1', 'select_1'), ('2', 'select_2')], defaultValue='1'))\n" +
|
||||
"items=z.checkbox('my_checkbox', " +
|
||||
"[('1', 'check_1'), ('2', 'check_2')], defaultChecked=['2'])\n" +
|
||||
"print(items[0])";
|
||||
p.setText(code);
|
||||
p.setAuthenticationInfo(anonymous);
|
||||
note.run(p.getId());
|
||||
waitForFinish(p);
|
||||
|
||||
assertEquals(Status.FINISHED, p.getStatus());
|
||||
Iterator<String> formIter = p.settings.getForms().keySet().iterator();
|
||||
assert(formIter.next().equals("my_input"));
|
||||
assert(formIter.next().equals("my_select"));
|
||||
assert(formIter.next().equals("my_checkbox"));
|
||||
|
||||
// check dynamic forms values
|
||||
String[] result = p.getResult().message().get(0).getData().split("\n");
|
||||
assertEquals(3, result.length);
|
||||
assertEquals("default_name", result[0]);
|
||||
assertEquals("1", result[1]);
|
||||
assertEquals("2", result[2]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue