Site icon JVM Advent

Jupyter Notebooks and Java?

Would you believe me if I said it was possible to run Java almost anywhere as long as you have access to Jupyter Notebooks ?

Jupyter Notebooks are an interactive computing environment that allows you to create and share documents that contain live code, equations, visualizations, and narrative text.

Notebooks always intrigued me as they allow for easy exploration. Java generally has not allowed for easy exploration in its early days. In recent years we have seen tools such as JBang and OpenJDK improvements that makes getting started with Java inherently simpler. For example single script files (JEP 330) and single main methods (JEP 445) have helped.

Jupyter Notebooks have started to become ubiquitous. I believe it’s valuable to make Java function seamlessly within Jupyter Notebooks or, at the very least, to understand how to use them together effectively. This article will provide insights into why integrating Java into Jupyter Notebooks is useful. Additionally, I will demonstrate how to enable Java in Jupyter on virtually any Jupyter-enabled platform, whether local or cloud-based. Finally – encourage you to checkout out https://github.com/jupyter-java where this story will hopefully evolve further.

So lets start with some of the reasons why Java in Jupyter Notebooks are interesting!

Interactive Learning and Explorative Development

Jupyter Notebooks provide an interactive interface where you can write and execute code in segments (called cells), see the output immediately, and make changes on the fly. This is different from the traditional Java development process, where you typically write a complete program, compile it, and then run it.

Not only does it let you experiment – it can also be used to let you setup notebooks that can interactively teach and educate others how to use the basics of Java or the API of your library.

Instead of just documenting how the API works one can put in snippets of code just next to the documentation that let you tinker and fix code to understand how it works. All without having to setup several layers of IDE and tooling to get started. That is powerful.

Support for Multiple Languages

Although Jupyter started with Python (hence the name, a play on Julia, Python, and R), it supports many languages by having Kernels that be written in and support any language, including Java.

Surprisingly to many there are already quite a list of Java based kernels. Especially for the non-Java JVM languages.

The main challenge with all these are that to install and enable them they each have their own install setup and not all “just works” in online IDE’s. Would it not be great to have a way to use these kernels anywhere?

Data Science and Visualization

Jupyter is popular in data science due to its support for data manipulation and visualization libraries. You can easily visualize data with graphs and charts, which is integrated directly in the notebook alongside the code that produces them.

Python is king in data science and although I think it is possible for Java to improve in that area it is not data science that drives my interest. No, the interesting part for those visualizations in Jupyter Notebooks are not just text and images – it can be anything that renders to html/css/javascript. You can imagine a Kernel that understands how to turn any Java Collection into a table, a BufferImage into a png, a JSONObject into a collapsible tree or A File or Path into a link to open. The possibilities are endless.

Web-based and local IDE Interfaces

Jupyter runs in the browser, offering a web-based interface. This makes it platform-independent and accessible from anywhere. Jupyter style notebook are available in VSCode based IDE’s as well as Jupyter Labs online services as well as being rendered on GitHub – meaning Jupyter Notebook works locally and in the cloud – making notebooks ubiquitously available.

Documentation and Sharing

Notebooks allow you to combine code with Markdown text, equations written in LaTeX, and more. This makes it easy to create documents that explain the code and its output, which can be useful for educational purposes, note taking, data analysis reports, and more.

The key thing is to make these notebooks accessible with Java available so everyone can open and access them.

In conclusion, think of Jupyter Notebooks as an IDE where you can not only write and test your code in chunks but also document your process and visualize results in real-time, all within the same document.

Now how do you get to use Java in all these various notebook environments?

Meet Jupyter-Java

Until recently finding information about how to use Jupyter with Java been a challenge. Especially how to use the various java kernels and installing them in a way that worked across the various environments.

I created jupyter-java a Git Hub organization with its own landing page, discussion forum and a few repos all related to working with Java using Jupyter Notebooks. It is still in its infancy but contributions are very welcome.

I made it when I realized I could make Java Kernel installation a breeze compared to today’s complexity.

Easy Java Kernel Installation

This is available as a JBang script that you can run as follows:

jbang install-kernel@jupyter-java

By default it currently installs IJava based kernel but already supports installing the other following Kernels:

NameLanguage
IJavaJava
RaipaoJava, requires Java 21
KotlinKotlin
GanyemedeJava, Groovy, Kotlin
Java kernels and language supported by install-kernel@jupyter-java

If you run jbang install-kernel@jupyter-java <kernel name> and have VSCode with Jupyter Extension installed you can now open a New Jupyter Notebook and choose Jupyter Kernel > jbang-<kernel name> to have that notebook working with the respective kernel.

The install-kernel script has various options to tweak the setup. For example if you want to be using latest greatest Java 21 preview features you can use:

jbang install-kernel@jupyter-java --java 21 —-enable-preview

And with that you can use String templates in your Jupyter Notebooks.

Java in any cloud Jupyter notebook

Notebooks are available in many places but they rarely if at all have Java enabled. With a little bit of additional JBang magic you can put the following into most Jupyter notebook environments and make a Java kernel with preview features available:

!pip install jbang
import jbang
jbang.exec(“trust add https://github.com/jupyter-java”)
jbang.exec(“install-kernel@jupyter-java --java 21 —-enable-preview”)

Those four lines do the following:

  1. Install jbang-python, a Python module that installs JBang and provide API to use it from Python
  2. Import jbang module in Python
  3. Trust https://github.com/jupyter-java as a trusted url (an additional layer of protection so JBang doesn’t execute the code of a completely random stranger)
  4. Run install-kernel@jupyter-java to generate a Java Kernel setup.

All fairly simple stuff – but together it enables Java on many online services. Below are the various services it works on already today:

Online NotebookDescription
Google ColabJupyter notebooks hosted by Google – just need a google account
MybinderFree service that offer to run docker images with Jupyter included
Red Hat Data ScienceAccessible for free via Red Hat OpenShift Sandbox – provides Jupyter notebooks
GitPod.ioGitPod.io VSCode online environment, has free tier
GitHub CodespacesGitHub codespaces based on VSCode, requires subscription – has OpenSource friendly free tier
OpenShift WorkspacesRed Hat OpenShift VSCode online environment, has free tier
Online services that install-kernel@jupyter-java works with

If you want to try it out immediately go to github.com/jupyter-java/anywhere and use one of the direct links present in that repository.

Challenges and Future Prospects

My idea is that Jupyter-Java can if nothing else provide a starting point for Java developers to get started with Jupyter Notebooks. My hope is that it encourages you and others to evolve and improve the support for Java in Jupyter Notebooks as it is really nice to have available.

Maybe Java API’s will provide custom renderings ? Maybe a JBang enabled kernel is in the future? Maybe Jupyter Notebooks in Quarkus dev-ui would be possible ?

Lots of interesting opportunities.

Conclusion

Jupyter Notebooks and Java are possible and have multiple advantages and their combined use will help make Java more accessible and useful.

My encouragement to you in this jolly season is to go out and try Jupyter Notebooks – get started with https://github.com/jupyter-java/anywhere. With install-kernel@jupyter-java it is trivial to get started.

Explore the different Kernel variants, try use your favorite API’s, get a feel for it and then post questions or ideas in the discussions – lets spread the word and make the (Java) world a better place.

Author: Max Andersen

Exit mobile version