Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/displaysystem/angular.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -96,3 +96,28 @@ z.angularBind("run", 0) will initialize 'run' to zero. And then register watcher
After clicked button, you'll see both 'run' and numWatched are increased by 1

<img src="/assets/themes/zeppelin/img/screenshots/display_angular3.png" width=600px />



<br />
#### Bind/Unbind function

Through ZeppelinContext, you can bind/unbind function to AngularJS view.

Currently it only works in Spark Interpreter (scala).

```
// bind function in current notebook.
z.angularBindFunction(String name, { args: Seq[Object] => ... })

// bind function in all notebooks related to current interpreter.
z.angularBindGlobal(String name, Object object)

// unbind function in current notebook.
z.angularUnbind(String name)

// unbind function in all notebooks related to current interpreter.
z.angularUnbindGlobal(String name)

```

Original file line numberDiff line numberDiff line change
Expand Up@@ -24,21 +24,14 @@
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

import org.apache.spark.SparkContext;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.SQLContext.QueryExecution;
import org.apache.spark.sql.catalyst.expressions.Attribute;
import org.apache.spark.sql.hive.HiveContext;
import org.apache.zeppelin.display.AngularObject;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.display.AngularObjectWatcher;
import org.apache.zeppelin.display.GUI;
import org.apache.zeppelin.display.*;
import org.apache.zeppelin.display.Input.ParamOption;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterContextRunner;
Expand All@@ -48,6 +41,9 @@
import scala.Tuple2;
import scala.Unit;
import scala.collection.Iterable;
import scala.collection.JavaConversions;
import scala.collection.Seq;
import scala.collection.mutable.Set;

/**
* Spark context for zeppelin.
Expand DownExpand Up@@ -749,4 +745,55 @@ private void angularUnbind(String name, String noteId) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
registry.remove(name, noteId);
}


public void angularBindFunction(String name, final scala.Function1<Seq<Object>, Unit> func) {
String noteId = interpreterContext.getNoteId();
angularBindFunction(name, noteId, new AngularFunctionRunnable() {
@Override
public void run(Object... args) {
Set<Object> sets = JavaConversions.asScalaSet(new HashSet<Object>(Arrays.asList(args)));
func.apply(sets.toSeq());
}
});
}

public void angularBindFunction(String name, AngularFunctionRunnable func) {
String noteId = interpreterContext.getNoteId();
angularBindFunction(name, noteId, func);
}

public void angularUnbindFunction(String name) {
String noteId = interpreterContext.getNoteId();
angularUnbindFunction(name, noteId);
}

public void angularBindFunctionGlobal(String name,
final scala.Function1<Seq<Object>, Unit> func) {
angularBindFunction(name, null, new AngularFunctionRunnable() {
@Override
public void run(Object... args) {
Set<Object> sets = JavaConversions.asScalaSet(new HashSet<Object>(Arrays.asList(args)));
func.apply(sets.toSeq());
}
});
}

public void angularBindFunctionGlobal(String name, AngularFunctionRunnable func) {
angularBindFunction(name, null, func);
}

public void angularUnbindFunctionGlobal(String name) {
angularUnbindFunction(name, null);
}

private void angularBindFunction(String name, String noteId, AngularFunctionRunnable func) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
registry.createAngularFunction(name, noteId, func);
}

private void angularUnbindFunction(String name, String noteId) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
registry.removeAngularFunction(name, noteId);
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zeppelin.display;

import com.google.gson.Gson;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
* AngularFunction provides proxy object in front-end side.
* Calling front-end side proxy will invoke AngularFunctionRunnable of this AngularFunction
*/
public class AngularFunction extends AngularObjectWatcher {
Logger logger = LoggerFactory.getLogger(AngularFunction.class);
private static final String ANGULAR_FUNCTION_OBJECT_NAME_PREFIX = "_Z_ANGULAR_FUNC_";
private final AngularObjectRegistry registry;
private final String name;
private final String noteId;
private final AngularFunctionRunnable runnable;

// arguments of invocation from fron-end proxy function
AngularObject angularObject;

protected AngularFunction(AngularObjectRegistry registry,
String name, String noteId,
AngularFunctionRunnable runnable) {
super(null);
this.name = name;
this.noteId = noteId;
this.runnable = runnable;
this.registry = registry;

remove();

angularObject = registry.add(getFuncName(name), "", noteId);
angularObject.addWatcher(this);
}


static String getFuncName(String name) {
return ANGULAR_FUNCTION_OBJECT_NAME_PREFIX + name;
}

@Override
public void watch(Object oldObject, Object newObject, InterpreterContext context) {
if (runnable == null) {
return;
}

Object argumentList = angularObject.get();
if (argumentList instanceof Object[]) {
runnable.run((Object[]) angularObject.get());
} else {
runnable.run(angularObject.get());
}
}


void remove() {
registry.remove(getFuncName(name), noteId);
}

public String getNoteId() {
return noteId;
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zeppelin.display;

/**
* User provided function body for AngularFunction
*/
public interface AngularFunctionRunnable {
public void run (Object ... args);
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@
* - localRegistry: AngularObject is valid only inside of a single notebook
*/
public class AngularObjectRegistry {
Map<String, Map<String, AngularObject>> registry =
Map<String, Map<String, AngularObject>> registry =
new HashMap<String, Map<String, AngularObject>>();
private final String GLOBAL_KEY = "_GLOBAL_";
private AngularObjectRegistryListener listener;
Expand DownExpand Up@@ -69,6 +69,15 @@ public AngularObject add(String name, Object o, String noteId) {
return add(name, o, noteId, true);
}

public AngularFunction createAngularFunction(String name, String noteId,
AngularFunctionRunnable func) {
return new AngularFunction(this, name, noteId, func);
}

public void removeAngularFunction(String name, String noteId) {
remove(AngularFunction.getFuncName(name), noteId);
}

private String getRegistryKey(String noteId) {
if (noteId == null) {
return GLOBAL_KEY;
Expand All@@ -87,7 +96,7 @@ private Map<String, AngularObject> getRegistryForKey(String noteId) {
return registry.get(key);
}
}

public AngularObject add(String name, Object o, String noteId, boolean emit) {
AngularObject ao = createNewAngularObject(name, o, noteId);

Expand Down
Loading