/ TEACHING, JUPYTER

Teaching Java with Jupyter notebooks

Whatever the side of the fence you’ve been on in trainings - teacher or student, I believe we all share the same experience: it’s very hard to stay focused during a couple of hours. This has only become worse in recent years, with mobile phones being a huge source of distraction.

The consequence is that the most important part is the practice. Students need to put in application what they just learned in order to:

  • get a better understanding
  • have a longer knowledge retention period

I started teaching when I was still a student, more than 15 years ago, without any teaching training. When I prepare a new lesson, I focus a lot on the hands-on part. Sometimes, I go as far as preparing a workshop first, and then build the course upon it.

Here are some approaches I’ve used over the years.

Grasping at straws…​ in the dark

Earlier, I wrote my workshops on Word documents. To distribute them, I first used USB keys, and later Dropbox and Google Drive when Internet and cloud-based apps became ubiquitous.

I coded the solutions live in front of the audience, while explaining and answering questions if necessary. Upon request, I handed them over the solution code in zip archives.

This approach obviously has a lot of issues:

What if nobody asks for the solution

In that case, no student has access to it. And they cannot review it at a later stage.

What if some students don’t attend

The solution can obviously be given to them. However, it’s in its final form: they don’t get to see the steps. I think that is a huge obstacle in the understanding process and preparing for the exam.

What if the problem statement needs to be updated

The teacher must contact each of the students individually to tell them about downloading the document again.

Docker

I contemplated the usage of Docker images for workshops. I don’t think this is a good idea for several reasons:

  • Students will need to learn how to perform the setup anyway
  • Docker is a great way to setup application servers. I have more doubts about IDEs.
  • My experience taught me it’s quite hard to install new software such as the Docker engine in public education organizations

Source control and pages hosting

Some years ago, I learned Git. I realized it could solve the above issues, if coupled with some other technologies.

  1. Workshop instructions are written in Asciidoctor format
  2. Instructions and code are managed in Git.
    • The master branch contains the workshop starting state, while the solution branch contains each step for every task up to the final state. Finally, the documentation contains the instructions, as well as the build script (see below)
    • Everything is stored on Github.
  3. Each pushed commit triggers a build on TravisCI. This transforms the Asciidoctor sources into HTML.
  4. The generated HTML is made available on Github Pages.

Gitlab is a great alternative if you want to keep the sources private. It offers build capabilities similar to Github’s, but as an integrated build tool, as well as hosting.

Sample workshops (in French) can be seen for Java data structures and Java EE servlets

I don’t believe there’s a universal solution: this works pretty well with the second workshop, about servlets. Java EE development requires a lot of setup, and workshops are no exceptions. Besides, web development requires visual feedback.

However, for Java fundamentals - or for any other language fundamentals for that matter, an IDE is overkill. They allow for debugging, but on the flip side, they make things more complicated than they should.

Also, using a testing framework to check if results are correct is awkward. It’s hard to first tell about, say, TestNG if students don’t know about annotations. This becomes a case of a dog chasing its tail.

Jupyter notebook

That IDE barrier got me thinking about an alternative.

Project Jupyter exists to develop open-source software, open-standards, and services for interactive computing across dozens of programming languages.

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.

— http://jupyter.org/

In a nutshell, Jupyter notebooks allow to execute code from a local document in a browser and see the output in the same page: it’s all about interactivity. For example, the following is taken from Jupyter’s website:

import ipyvolume.pylab as p3
import numpy as np

fig = p3.figure()
q = p3.quiver(*stream.data[:,0:50,:200], color="red", size=7)
p3.style.use("dark") # looks better
p3.animation_control(q, interval=200)
p3.show()

Jupyter notebook visualization example

While Jupyter’s is a Python-based solution, it allows to integrate additional "kernels" - languages. One such kernel provides Java capabilities - IJava.

It requires:

  1. Java 9
  2. Jupyter notebooks

The kernel can now be installed using the documented procedure.

To test the newly installed kernel, use the following command:

jupyter console --kernel=java

This should output the following:

Jupyter console 5.1.0

Java 9.0.4+11 :: IJava kernel 1.1.0-SNAPSHOT
Protocol v5.0 implementation by jupyter-jvm-basekernel 2.2.1-SNAPSHOT

In [1]:

Try typing any valid Java command e.g. System.out.println("Hello world!");.

To launch the notebook, use the following command in the folder you want to create (or use) notebook files:

jupyter notebook

I played a bit to re-create the Java workshop referenced above using a Jupyter notebook.

This is a part of the result:

jupyter notebook screenshot

If you have installed Jupyter, you can compare the workshop on Github Pages with the notebook. Just open the latter in a browser and play around.

Conclusion

Several tools are available for free to help teachers and trainers in their tasks. For coding courses covering basics, Jupyter notebooks are a great asset, removing the hassle of setting up an IDE.

Nicolas Fränkel

Nicolas Fränkel

Nicolas Fränkel is a Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Currently working for Exoscale. Also double as a teacher in universities and higher education schools, a trainer and triples as a book author.

Read More