【问题标题】:Using a Beakerx Custom Magic使用 Beakerx 自定义魔法
【发布时间】:2018-08-01 04:33:54
【问题描述】:

我创建了一个自定义 Magic 命令,旨在以编程方式生成 spark 查询。这是我的类中实现 MagicCommandFunctionality 的相关部分:

MagicCommandOutcomeItem execute(MagicCommandExecutionParam magicCommandExecutionParam) {    

    // get the string that was entered:
    String input = magicCommandExecutionParam.command.substring(MAGIC.length())

    // use the input to generate a query
    String generatedQuery =  Interpreter.interpret(input)

    MIMEContainer result = Text(generatedQuery);
    return new MagicCommandOutput(MagicCommandOutcomeItem.Status.OK, result.getData().toString());
}

这很好用。它返回我生成的命令。 (作为文本)

我的问题是——我如何强制笔记本评估单元格中的值?我的猜测是涉及到 SimpleEvaluationObject 和 TryResult,但我可以'找不到任何使用示例

我可能希望内核为我创建一个,而不是创建 MagicCommandOutput。我看到 KernelMagicCommand 有一个执行方法可以做到这一点。有人有什么想法吗?

【问题讨论】:

    标签: beaker beaker-notebook


    【解决方案1】:

    好的,我找到了一种方法。这是我的解决方案:
    您可以向当前的 kernelManager 询问您感兴趣的内核,
    然后调用 PythonEntryPoint.evaluate。它似乎完成了这项工作!

    @覆盖 MagicCommandOutcomeItem 执行(MagicCommandExecutionParam magicCommandExecutionParam) {

    String input = magicCommandExecutionParam.command.substring(MAGIC.length() + 1)
    // this is the Scala code I want to evaluate:
    String codeToExecute = <your code here>
    
    KernelFunctionality kernel = KernelManager.get()
    PythonEntryPoint pep = kernel.getPythonEntryPoint(SCALA_KERNEL)
    pep.evaluate(codeToExecute)
    pep.getShellMsg()
    
    List<Message> messages = new ArrayList<>()
    //until there are messages on iopub channel available collect them into response
        while (true) {
            String iopubMsg = pep.getIopubMsg()
            if (iopubMsg == "null") break
            try {
                Message msg = parseMessage(iopubMsg) //(I didn't show this part)
                messages.add(msg)
                String commId = (String) msg.getContent().get("comm_id")
                if (commId != null) {
                    kernel.addCommIdManagerMapping(commId, SCALA_KERNEL)
                }
            } catch (IOException e) {
                log.error("There was an error: ${e.getMessage()}")
                return new MagicKernelResponse(MagicCommandOutcomeItem.Status.ERROR, messages)
            }
        }
        return new MagicKernelResponse(MagicCommandOutcomeItem.Status.OK, messages)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-22
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多