Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Jupyter Notebook for Java
bsorrentino
bsorrentino

Posted on

     

Jupyter Notebook for Java

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 ofJupyter 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 offerJupyter notebooks, would be great for software developers to use such notebook approach to develop, for example,USE CASE TESTS for projects or providing usefulINTERACTIVE HOW-TO.

The question here is:

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

The answer isYES🤩.

The Jupiter Architecture

TheJupyter tools have been architected to support multiple programming languages though theKernel concept, see diagram below:

jupyter-architecture

The kernel is how theJupyter 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 byJupyter 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 isYes🤩.

Lately I've been working onLanggraph4J which is a Java implementation of the more famousLanggraph.js which is a Javascript library used to create agent and multi-agent workflows byLangchain. Interesting note is thatLanggraph.js uses JavascriptJupyter notebooks powered by aDENO Jupiter Kernel to implement and documentHow-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 aJupyter Kernel that supported Java considering that, from the JDK 9 version, there was the introduction of JShell that enabled theREPL 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 onrapaio-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 version5.4, and it requires Java =22.

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

Java Jupyter notebook features


You can write normal Java.

varresult=2+2;result

4

// including classesrecordComplex(doublea,doubleb){publicComplexadd(Complexc){returnnewComplex(a+c.a,b+c.b);}}Complexx=newComplex(10,20);x.add(newComplex(1,1))

Complex[a=11.0, b=21.0]

// methods can also be implementedintadd(inta,intb){returna+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.

Stringversion="1.0.2";

 

%dependency/addcom.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/addcom.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.

importcom.github.javafaker.Faker;varfaker=newFaker();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.

%%bashjava--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.

%%bashname="John"echo"Hello$name"

Hello John

Show an image for immediate inspection

%imagehttps://www.google.com/logos/doodles/2024/paris-games-sailing-6753651837110529.4-law.gif

Image description

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.

importjavax.imageio.*;display(ImageIO.read(newURL("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")

Markdowntestsnippet:

  • 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.

Stringid=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 usingObjects.toString. As such, if the object has an implementation oftoString, that method will be called.

newComplex(10,Math.PI);

Complex[a=10.0, b=3.141592653589793]


Conclusion

The versatility ofJupyter notebooks extends beyond Python, the integration of kernels likerapaio-jupyter-kernel break new ground for Java developers. My preferred feature is the possibility to write HOW-TOs interactively documenting them contextually, but there are a bunch of potential use cases and it’s up to you to explore them, so let’s explore and let me know.

I hope that this knowledge will be helpful, in the meanwhile, enjoy coding! 👋 

💻 My sperimentations of Java notebooks are onGithub 💻 


Originally published athttps://bsorrentino.github.io on September 6, 2024.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

He is a Software Architect with over 20 years of experience. He currently is the CTO of Softphone.In 2009 he has led the setup of a Soulsoftware
  • Location
    Avellino. Italy
  • Work
    CTO of Softphone and CEO (cofounder) of Soulsoftware
  • Joined

More frombsorrentino

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp