首页 > Java > java教程 > 适用于 Java 的 Jupyter 笔记本

适用于 Java 的 Jupyter 笔记本

WBOY
发布: 2024-09-11 06:31:33
原创
850 人浏览过

Jupyter Notebook 的强大

Jupyter Notebooks 是一个出色的工具,最初是为了帮助数据科学家和工程师使用 Python 编程语言简化数据处理工作而开发的。事实上,笔记本的交互性使其非常适合快速查看代码结果,而无需搭建开发环境、编译、打包等。此功能对于数据科学、机器学习和统计建模的采用至关重要,在这些领域,开发技能不如数据操作专业知识那么重要。

优点

以下是 Jupyter Notebook 的一些优点

  1. 交互式开发:笔记本允许开发人员以小块形式编写代码,立即测试它们并可视化结果。这种交互式工作流程可促进更快的迭代和调试,非常适合数据探索、算法开发和快速原型设计。
  2. 丰富的可视化:通常,Notebook 与强大的可视化库集成,可以内联显示绘图、图表和其他视觉输出。
  3. 文档和代码在一起:笔记本将可执行代码与 Markdown 单元相结合,允许开发人员记录他们的代码、解释逻辑等......,创建更具可读性和可维护性的代码库。
  4. 协作:通过共享笔记本,团队成员无需设置开发环境即可查看和运行代码,使协作变得更加容易,尤其是在涉及非技术利益相关者的跨职能团队中。
  5. 再现性:笔记本可以从上到下重新运行,确保任何分析或测试都可以一致地再现。这对于调试、测试或呈现结果至关重要。

总结一下我们可以说

Jupyter 笔记本简化了从初始探索到生产就绪代码的开发过程,提供了灵活性和实时反馈。

打破Python障碍

考虑到 Jupyter Notebook 的优势,对于软件开发人员来说,使用这种 Notebook 方法进行开发会非常有用,例如,项目用例测试或提供有用的交互式操作指南

这里的问题是:

是否可以使用 JUPYTER Notebook 进行 Python 以外的编程语言❓?

答案是?.

木星架构

Jupyter 工具的架构旨在通过 内核 概念支持多种编程语言,请参见下图:

Jupyter Notebook for Java

内核是 Jupyter 笔记本服务器评估用户在笔记本文档 (.ipynb) 内编写的代码块的方式,因此拥有一个可以评估您选择的编程语言的代码的内核就足够了Jupyter笔记本支持它。
当然,很容易推断出 Jupyter 内核可以支持的每种潜在编程语言都应该支持读取-评估-打印循环 (REPL) 功能。

问题变成:

除了 Python ONE 之外还有 JUPYTER 内核吗❓?

答案是?。

最近我一直在研究 Langgraph4J,它是更著名的 Langgraph.js 的 Java 实现,Langgraph.js 是 Langchain 用于创建代理和多代理工作流程的 Javascript 库。有趣的是,[Langchain.js] 使用由 DENO Jupiter 内核支持的 Javascript Jupyter 笔记本来实现和记录 How-Tos。
因此,我面临着如何在 Java 中使用(或可能模拟)相同方法的困境,并且没有太多希望,我开始寻找支持 Java 的 Jupyter 内核,因为从 JDK 9 版本开始,引入了为 Java 启用 REPL 的 JShell。

Java Jupyter 内核

经过一些研究(以及尝试将自己投入 DIY 实现的奇怪想法),我找到了 rapaio-jupyter-kernel,它是一个支持 Java 的 Jupyter 内核。该项目指出:

基于 JShell 的 Java 语言 Jupyter 内核。它实现了 Jupyter 消息规范版本 5.4,并且需要 Java = 22。

太棒了;我开始使用它,哇!?。看看它的一些功能,下面我总结了最具代表性的功能:

Java Jupyter 笔记本功能


你可以编写普通的Java。

var result = 2 + 2;
result
登录后复制

4

// including classes
record Complex(double a, double b) {
    public Complex add(Complex c) {
        return new Complex(a+c.a, b+c.b);
    }
}
Complex x = new Complex(10,20);
x.add(new Complex(1,1))
登录后复制

复杂[a=11.0, b=21.0]

// methods can also be implemented
int add(int a, int b) { return a+b; }
add(2,3)
登录后复制

5

Magic commands

Besides Java code, a cell can contain special commands implemented by the kernel. These are called magic code and there are two types: magic lines and magic cells.
Magic lines are lines which are prefixed with %. After the prefix it is followed by the magic command and the optional parameters. Below is an example of magic line:

// magic line which asks JShell to list the types defined in this notebook in this moment
%jshell /types
登录后复制

| record Complex

Magic commands interpolation

Sometimes there is a need to run a magic command in a more dynamic way. This can be done using magic interpolation.
Magic interpolation is the interpolation of marked content which starts with \{ and ends with }. Any content decorated with those markers is evaluated in jshell and the result is transformed in a String which replaces the decorated content in the magic command.

String version = "1.0.2";
登录后复制

 

%dependency /add com.github.javafaker:javafaker:\{version}
登录后复制

Adding dependency com.github.javafaker:javafaker:1.0.2

Dependency management ?

You can add dependencies using %dependency /add and after adding all dependencies you can call %dependency /resolve

%dependency /add com.github.javafaker:javafaker:1.0.2
%dependency /resolve
登录后复制

Adding dependency com.github.javafaker:javafaker:1.0.2
Solving dependencies
Resolved artifacts count: 5
Add to classpath: /home/ati/work/rapaio-jupyter-kernel/target/mima_cache/com/github/javafaker/javafaker/1.0.2/javafaker-1.0.2.jar
Add to classpath: /home/ati/work/rapaio-jupyter-kernel/target/mima_cache/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar
Add to classpath: /home/ati/work/rapaio-jupyter-kernel/target/mima_cache/org/yaml/snakeyaml/1.23/snakeyaml-1.23-android.jar
Add to classpath: /home/ati/work/rapaio-jupyter-kernel/target/mima_cache/com/github/mifmif/generex/1.0.2/generex-1.0.2.jar
Add to classpath: /home/ati/work/rapaio-jupyter-kernel/target/mima_cache/dk/brics/automaton/automaton/1.11-8/automaton-1.11-8.jar

When added you can import and use the dependency.

import com.github.javafaker.Faker;
var faker = new Faker();
faker.name().fullName()
登录后复制

Hayley Anderson

Resolving conflict dependencies

You there are conflicts you can manage them with optional. Let's take an example which have conflicts:

%dependency /add com.google.guava:guava:20.0 --optional
%dependency /add com.google.inject:guice:4.2.2
%dependency /add com.google.guava:guava:25.1-android
%dependency /resolve
登录后复制

Help on magic commands

The magic %help provides more examples and guidance.

JShell commands

Some JShell commands are implemented. For example you can inspect which variables are defined

%jshell /vars
登录后复制

or the types you defined in this session

%jshell /types
登录后复制

Execute bash commands

You can execute bash scripting commands. Here we display the java version number.

%%bash
java --version
登录后复制

openjdk 22.0.2 2024-07-16
OpenJDK Runtime Environment Corretto-22.0.2.9.1 (build 22.0.2+9-FR)
OpenJDK 64-Bit Server VM Corretto-22.0.2.9.1 (build 22.0.2+9-FR, mixed mode, sharing)

You can even define variables. In fact all the lines below cell magic marker are executed as a bash script.

%%bash
name="John"
echo "Hello $name"
登录后复制

Hello John

Show an image for immediate inspection

%image https://www.google.com/logos/doodles/2024/paris-games-sailing-6753651837110529.4-law.gif
登录后复制

Jupyter Notebook for Java

Display data

Jupyter notebooks uses outputs to display objects of various types. By default when an object is returned as the result of the last code operation, that result is displayed.
The object which is displayed can be anything. If the object has a display handler registered, than that renderer is used to transform the object into a displayable content. If there is no registered display handler than the object is transformed into a string and that will be displayed.
Previously we used magic commands to display an image. However for BufferedImages there is a registered handler and if you obtain an instance of a BufferedImage it will be displayed properly.

import javax.imageio.*;
display(ImageIO.read(new URL("https://www.google.com/logos/doodles/2024/paris-games-sailing-6753651837110529.4-law.gif")));
登录后复制

Displayed data has a mime type. You can use that to describe how the object should be interpreted. For example we display a markdown snippet and we direct the output interpretation of the snippet through MIME type.

display("text/markdown", "Markdown *test* **snippet**:\n* bullet 1\n* bullet 2")
登录后复制

Markdown test snippet:

  • bullet 1
  • bullet 2

display command returns an id which identifies the piece of output from the notebook which handles the display. Notice that we captured the id of the display. This id can be used to update the same display with a different content. For example we can update the content of that display with a html snippet, using the MIME type for interpretation.

String id = display("text/markdown", "Markdown *test* **snippet**:\n* bullet 1\n* bullet 2");
登录后复制

 

updateDisplay(id, "text/html", "Html <i>test</i> <b>snippet</b>:<p><ulist><li>bullet 1</li><li>bullet 2</li></ulist></p>")
登录后复制

A Java object is displayed as a String using Objects.toString. As such, if the object has an implementation of toString, that method will be called.

new Complex(10,Math.PI);
登录后复制

Complex[a=10.0, b=3.141592653589793]


Conclusion

La polyvalence des notebooks Jupyter s'étend au-delà de Python, l'intégration de noyaux comme rapaio-jupyter-kernel innove pour les développeurs Java. Ma fonctionnalité préférée est la possibilité d'écrire des HOW-TO de manière interactive en les documentant contextuellement, mais il existe de nombreux cas d'utilisation potentiels et c'est à vous de les explorer, alors explorons et faites-le-moi savoir.

J'espère que ces connaissances vous seront utiles, en attendant, profitez du codage ! ? 

? Mes expérimentations de notebooks Java sont sur Github ? 


Publié à l'origine sur https://bsorrentino.github.io le 6 septembre 2024.

以上是适用于 Java 的 Jupyter 笔记本的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板