如何在 Zeppelin 的 javascript 中将变量放入 z ZeppelinContext 中? [英] How to put a variable into z ZeppelinContext in javascript in Zeppelin?

查看:18
本文介绍了如何在 Zeppelin 的 javascript 中将变量放入 z ZeppelinContext 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala 和 Python 中:

z.put("varname", 变量)

但在 javascript 中我得到(在控制台中)

Uncaught ReferenceError: z is not defined

我真正想做的是在 Zeppelin 中使用 z.angular("varname") 从 Scala 代码访问 javascript 变量,但我没有运气:(

在一个单元格中完全需要类似的东西

%angular<脚本>var myVar = "你好世界";//这里有一些神奇的代码!

然后在另一个单元格中

println(z.angular("myVar"))

更新:

这就是我到目前为止所拥有的,我完全是在黑暗中刺伤,因为我更像是一个后端/数据科学类的人.所以提前为我的前端绝望道歉.

单元格 1:

z.angularBind("myVar", "myVar")z.angularBind("msg", "msg")

注意我不知道在第二个参数中放什么.

单元格 2:

%angular<div ng-app><div id="outer" ng-controller="MsgCtrl">你是{{msg}}

<div onclick="change()">点击我</div>

<脚本>var myVar = "你好世界";函数 MsgCtrl($scope){$scope.msg = "foo";//还试验了 $scope.msg = myVar;}功能变化(){var scope = angular.element($("#outer")).scope();范围.$应用(函数(){scope.msg = '超级英雄';})}

单元格 3:

z.angular("msg")z.angular("myVar")

无论我做什么,我都会得到 null 或 var 名称.

我没有看到按钮或任何可以点击"的东西.

解决方案

在 Angular 解释器中.我们可以

  • 在 html 内容中使用

    {{name}}

  • 绑定

    ng-model='name'

  • 使用或设置 ng-click 或其他角度指令.

    ng-click="name='Rockie'"

<块引用>

根据许多 SO,在 angular 控制器之外修改范围变量不是一个好习惯.我也没有让它在 Zeppelin 中工作.不过有一个普通的js小提琴示例.

并在 Spark 解释器中.我们可以

  • 绑定

    z.angularBind("name", "Knock Data")

  • 解除绑定

    z.angularUnbind("name")

  • 取值时需要先绑定变量,否则会得到null.

    z.angular("name")

  • 观察变量

    z.angularWatch("name", (oldValue: Object, newValue: Object) => {println(s"值从 $oldValue 变为 $newValue"})

在角度绑定旁边.动态表单是为最终用户提供交互的好方法

  • 输入

    z.input("number", 1).toString.toInt

  • 选择不带默认值

    z.select("Day", Seq(("1", "Mon"), ("2", "Tue")))

  • 选择默认值,

    z.select("Day", "1", Seq(("1", "Mon"), ("2", "Tue")))

  • 检查

    z.checkbox("good applications", Seq(("1", "Zeppelin"), ("2", "Highcharts"), ("3", "Spark")))

In Scala and Python it's:

z.put("varname", variable)

But in javascript I get (in the console)

Uncaught ReferenceError: z is not defined

What I really want to do is access a javascript variable from Scala code using z.angular("varname") in Zeppelin, but I'm having no luck :(

In full need in one cell something like

%angular
<script>
var myVar = "hello world";

// some magic code here!
</script>

Then in another cell

println(z.angular("myVar"))

UPDATE:

This is what I have so far, I'm completely stabbing in the dark, since I'm more of a back end / data science kind of guy. So apologies in advance for my front end hopelessness.

Cell 1:

z.angularBind("myVar", "myVar")
z.angularBind("msg", "msg")

Note I have no idea what to put in the second argument.

Cell 2:

%angular

<div ng-app>
    <div id="outer" ng-controller="MsgCtrl">
        You are {{msg}}
    </div>
    <div onclick="change()">click me</div>
</div>

<script>
var myVar = "hello world";

function MsgCtrl($scope) 
{
    $scope.msg = "foo";
    // also experimented with $scope.msg = myVar;
}

function change() {
    var scope = angular.element($("#outer")).scope();
    scope.$apply(function(){
        scope.msg = 'Superhero';
    })
}
</script>

Cell 3:

z.angular("msg")
z.angular("myVar")

And no matter what I do I just either get null or the var name.

I don't see a button either or anything to "click".

解决方案

In Angular interpreter. we can

  • Use within html content

    {{name}}

  • Bind with

    ng-model='name'

  • Use or set with ng-click or other angular directives.

    ng-click="name='Rockie'"

Modify scope variable outside of angular's controller is not a good practise according to many SOs. I also have not make it work in Zeppelin. There is a normal js fiddle example though.

And in Spark interpreter. We can

  • Bind with

    z.angularBind("name", "Knock Data")

  • Unbind with

    z.angularUnbind("name")

  • Get the value with, the variable need be bind first, otherwise it will get null.

    z.angular("name")

  • Watch a variable with

    z.angularWatch("name", (oldValue: Object, newValue: Object) => { println(s"value changed from $oldValue to $newValue" })

Beside the angular bind. The Dynamic Form is a good way to give interactive for end users

  • Input

    z.input("number", 1).toString.toInt

  • Select without default value

    z.select("Day", Seq(("1", "Mon"), ("2", "Tue")))

  • Select with default value,

    z.select("Day", "1", Seq(("1", "Mon"), ("2", "Tue")))

  • Check

    z.checkbox("good applications", Seq(("1", "Zeppelin"), ("2", "Highcharts"), ("3", "Spark")))

这篇关于如何在 Zeppelin 的 javascript 中将变量放入 z ZeppelinContext 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆