Jupyter Notebooks 是一个出色的工具,最初是为了帮助数据科学家和工程师使用 Python 编程语言简化数据处理工作而开发的。事实上,笔记本的交互性使其非常适合快速查看代码结果,而无需搭建开发环境、编译、打包等。此功能对于数据科学、机器学习和统计建模的采用至关重要,在这些领域,开发技能不如数据操作专业知识那么重要。
以下是 Jupyter Notebook 的一些优点
总结一下我们可以说
Jupyter 笔记本简化了从初始探索到生产就绪代码的开发过程,提供了灵活性和实时反馈。
考虑到 Jupyter Notebook 的优势,对于软件开发人员来说,使用这种 Notebook 方法进行开发会非常有用,例如,项目用例测试或提供有用的交互式操作指南。
这里的问题是:
是否可以使用 JUPYTER Notebook 进行 Python 以外的编程语言❓?
答案是是?.
Jupyter 工具的架构旨在通过 内核 概念支持多种编程语言,请参见下图:
内核是 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。
经过一些研究(以及尝试将自己投入 DIY 实现的奇怪想法),我找到了 rapaio-jupyter-kernel,它是一个支持 Java 的 Jupyter 内核。该项目指出:
基于 JShell 的 Java 语言 Jupyter 内核。它实现了 Jupyter 消息规范版本 5.4,并且需要 Java = 22。
太棒了;我开始使用它,哇!?。看看它的一些功能,下面我总结了最具代表性的功能:
你可以编写普通的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.jarWhen 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登录后复制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]
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中文网其他相关文章!