Home Java javaTutorial Jupyter Notebook for Java

Jupyter Notebook for Java

Sep 11, 2024 am 06:31 AM

The powerful of Jupyter Notebook

Jupyter Notebooks are an excellent tool, originally developed to help data scientists and engineers to simplify their work with data using python programming language. In fact, the interactive nature of notebooks makes them ideal for quickly seeing the code results without setting up a development environment, compiling, packaging and so on. This feature has been crucial for adoption in data science, machine learning, and statistical modeling where development skill was less essential than data manipulation expertise.

Advantages

Below are some of the advantages of Jupyter notebook

  1. Interactive Development: Notebooks allow developers to write code in small chunks, test them immediately, and visualize results. This interactive workflow promotes faster iteration and debugging, ideal for data exploration, algorithm development, and quick prototyping.
  2. Rich Visualizations: Typically, Notebook is integrated with powerful visualization libraries that can display plots, graphs, and other visual outputs inline.
  3. Documentation and Code Together: Notebooks combine executable code with markdown cells, allowing developers to document their code, explain logic, etc.., creating more readable and maintainable codebases.
  4. Collaboration: By sharing notebooks, team members can review and run code without setting up a development environment, making collaboration easier, especially in cross-functional teams involving non-technical stakeholders.
  5. Reproducibility: Notebooks can be rerun from top to bottom, ensuring that any analysis or test can be consistently reproduced. This is crucial for debugging, testing, or presenting results.

Summarizing we can say that

Jupyter notebooks streamline the development process, from initial exploration to production-ready code, offering flexibility and real-time feedback.

Break the Python barrier

Considering the advantages that offer Jupyter notebooks, would be great for software developers to use such notebook approach to develop, for example, USE CASE TESTS for projects or providing useful INTERACTIVE HOW-TO.

The question here is:

IS IT POSSIBLE TO USE A JUPYTER NOTEBOOK FOR PROGRAMMING LANGUAGE OTHER THAN PYTHON❓?

The answer is YES?.

The Jupiter Architecture

The Jupyter tools have been architected to support multiple programming languages though the Kernel concept, see diagram below:

Jupyter Notebook for Java

The kernel is how the Jupyter notebook server evaluates blocks of code written by the user inside the notebook document (.ipynb), so it is sufficient to have a kernel that can evaluate the code of the programming language of your choice to have it supported by Jupyter notebook.
Of course, it is easy to deduce that every potential programming language that a Jupyter Kernel can support should support Read–eval–print loop (REPL) feature.

The question becomes:

ARE THERE JUPYTER KERNEL OTHER THAN PYTHON ONE❓?

The answer is Yes?.

Lately I've been working on Langgraph4J which is a Java implementation of the more famous Langgraph.js which is a Javascript library used to create agent and multi-agent workflows by Langchain. Interesting note is that [Langchain.js] uses Javascript Jupyter notebooks powered by a DENO Jupiter Kernel to implement and document How-Tos.
So, I faced a dilemma on how to use (or possibly simulate) the same approach in Java and, without much hope, I started looking for a Jupyter Kernel that supported Java considering that, from the JDK 9 version, there was the introduction of JShell that enabled the REPL for Java.

The Java Jupyter Kernel

After a bit of research (and a weird thought of trying to throw myself into a DIY implementation) I landed on rapaio-jupyter-kernel which is a Jupyter Kernel that supports Java ?. The project states:

Jupyter kernel for Java language based on JShell. It implements Jupyter message specification version 5.4, and it requires Java = 22.

It is amazing; I'm starting to use it and WOW!?. Take a look of some of its features, below I've summarized the most representative ones:

Java Jupyter notebook features


You can write normal Java.

var result = 2 + 2;
result
Copy after login

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))
Copy after login

Complex[a=11.0, b=21.0]

// methods can also be implemented
int add(int a, int b) { return a+b; }
add(2,3)
Copy after login

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
Copy after login

| 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";
Copy after login

 

%dependency /add com.github.javafaker:javafaker:\{version}
Copy after login

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
Copy after login

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()
Copy after login

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
Copy after login

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
Copy after login

or the types you defined in this session

%jshell /types
Copy after login

Execute bash commands

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

%%bash
java --version
Copy after login

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"
Copy after login

Hello John

Show an image for immediate inspection

%image https://www.google.com/logos/doodles/2024/paris-games-sailing-6753651837110529.4-law.gif
Copy after login

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")));
Copy after login

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")
Copy after login

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");
Copy after login

 

updateDisplay(id, "text/html", "Html <i>test</i> <b>snippet</b>:<p><ulist><li>bullet 1</li><li>bullet 2</li></ulist></p>")
Copy after login

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);
Copy after login

Complex[a=10.0, b=3.141592653589793]


결론

Jupyter 노트북의 다양성은 Python을 넘어 확장되며, rapaio-jupyter-kernel과 같은 커널 통합은 Java 개발자에게 새로운 지평을 열었습니다. 제가 선호하는 기능은 HOW-TO를 대화식으로 작성하여 상황에 맞게 문서화할 수 있는 기능입니다. 그러나 잠재적인 사용 사례는 다양하며 이를 탐색하는 것은 여러분에게 달려 있으므로 살펴보고 알려주시기 바랍니다.

이 지식이 도움이 되었으면 좋겠습니다. 그동안 즐거운 코딩을 즐겨보세요! ? 

? Java 노트북에 대한 나의 실험은 Github에 있습니까? 


원본은 2024년 9월 6일 https://bsorrentino.github.io에 게시되었습니다.

The above is the detailed content of Jupyter Notebook for Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles