Create Your Own Kotlin Playground (and Get a Data Science Head Start) with Jupyter Notebook
Learn the basics of Jupyter Notebook and how to turn it into an interactive interpreter for Kotlin. You’ll also learn about Data Frames, an important data structure for data science applications. By Joey deVilla.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Create Your Own Kotlin Playground (and Get a Data Science Head Start) with Jupyter Notebook
30 mins
- Kotlin? For Data Science?
- Introducing Jupyter Notebook
- Getting Started
- Creating Your First Notebook
- Understanding Code Cells
- Working With Markdown Cells
- Initializing krangl
- Diving into Data Frames
- Introducing Data Frames
- Creating a Data Frame from Scratch
- Getting the Data Frame’s Schema
- Getting the Data Frame’s Dimensions and Column Names
- Examining the Data Frame’s Columns
- Examining the Data Frame’s Rows
- Accessing Data Frame “Cells” by Column and Row
- Where to Go From Here?
If you’ve ever wished for a Kotlin version of a REPL (read-evaluate-play loop) like the ones in command-line Python or Node.js or for a lightweight local version of the Kotlin Playground, there’s a way to do it: by using Jupyter Notebook with a Kotlin kernel. In this article, you’ll set up a Kotlin playground that runs locally on your computer!
In this article, you’ll learn the basics of Jupyter Notebook and how to turn it into an interactive interpreter you can use to learn Kotlin, as a scratchpad for your Kotlin ideas and experiments or as your annotated library of code snippets. You’ll also learn about a data structure you can use to do data science with Kotlin.
Kotlin? For Data Science?
Right now, when you bring up the topic of data science, the Python and R programming languages come up most often. Python’s easy-to-learn and readable syntax as well as its vast ecosystem of data processing and analysis libraries (especially pandas and NumPy) and R’s standing among the statistics-minded and its Tidyverse data science packages have made them data science mainstays.
There’s no reason Kotlin, too, can’t be a good language for data science. Like Python, it has a concise and readable syntax, and it’s far easier to learn and less verbose than Java. As a JVM language, it’s compatible with the galaxy of Java libraries developed for and used by its base of millions of developers. It will run circles around both Python and R.
Python and R might have a head start, but that doesn’t mean you can’t use Kotlin for data science. Kotlin not only has the speed advantage, but it also has a platform advantage: Android. It’s the OS that runs on most mobile devices. And with Windows 11, Android apps will be on the OS that runs on most desktop and laptop devices. As people expect their apps to do more and more, apps need to use data science — and they won’t be written in Python or R. Think of doing data science in Kotlin as your head start.
Introducing Jupyter Notebook
This article will be a little different from most of the Kotlin and Android articles on this site. Instead of using IntelliJ IDEA or Android Studio and building an app for an Android device, you’ll use Jupyter Notebook, which you can think of as a combination of an interactive code playground and a wiki.
Jupyter Notebook, a web application, lets you create documents that combine:
- Human-readable narrative in the form of rich text, hyperlinks, images and anything else you can include in a web page, as well as …
- Computer-readable code that can import and process data and user input and present the results.
By combining a document with executable code, you can literally “show your work”. The narrative part of the notebook explains your idea to the reader, while the code part performs the processing and calculations right in front of them.
This capability of Jupyter Notebook has made it popular with scientists and researchers, including the winners of the 2017 Nobel Prize in physics and the winner of the 2018 Nobel Prize in economics. You’ll also find Jupyter Notebook used outside academia, in places such as Netflix and Yelp.
Python is the default programming language for Jupyter Notebook, but it’s not the only language. A library of kernels allows Jupyter Notebook to support dozens of other programming languages, including Kotlin.
In this article, you’ll install Jupyter Notebook on your computer (and yes, it runs on macOS, Windows and Linux), install the Kotlin kernel and then use the krangl library to try out some basic data science.
Getting Started
The simplest way to install Jupyter Notebook is to use Anaconda’s “Individual Edition” distribution. This is an open-source, no-cost distribution of Python that installs a Python interpreter (which is required to run Jupyter Notebook), Jupyter Notebook and a set of coding utilities and tools selected with the data scientist in mind.
Go to Anaconda’s “Individual Edition” page and download the appropriate installer for your computer and operating system. You’ll find Anaconda installer applications for macOS and Windows, as well as a shell script for installing Anaconda on Linux.
Once you’ve installed Anaconda on your computer, the next step will be to give Jupyter Notebook the ability to run Kotlin code by installing the Kotlin kernel. Do this by opening Terminal (on macOS or Linux) or Command Prompt (on Windows) and entering the following command:
conda install -c jetbrains kotlin-jupyter-kernel
Once the command completes, you’ll be able to launch Jupyter Notebook. The simplest way to do so is from the command line. Enter the following into Terminal or Command Prompt:
jupyter notebook
Your computer should open a new browser window or tab to a page that looks like this:
This page displays the contents of your home directory. You can use it to navigate through and manage your file system.
Creating Your First Notebook
To create your first notebook, click the New button located near the upper-right corner of the page. A menu will appear, and one of the options will be Kotlin. Select that option to create a new Kotlin notebook.
A new browser tab or window will open, containing a page that looks like this:
The text area below the menu bar and menu buttons on the page is a cell. Think of a Jupyter Notebook as being like a spreadsheet with a single column of cells. Each cell can contain either content in Markdown or code in a programming language. The cells together form a document called a notebook, containing text the reader can read and code they can execute.
Depending on how you write the notebook, it can either be code with rich text annotations or an article or essay enhanced by code.
Understanding Code Cells
Start with code cells, which can execute code entered into them. By default, Jupyter Notebook cells are code cells.
You specify the programming language a Jupyter Notebook will support when you create it. In this case, you specified Kotlin as your Jupyter Notebook’s language, so it expects you’ll enter Kotlin into its code cells.
Right now, the single cell in your notebook is a default cell, which means it’s a code cell. Enter the following code into it:
println("Hello, Jupyter world!")
Now that you’ve entered the code, it’s time to run it. To run the code, make sure you’ve selected the cell and either:
- Click the Run button; or
- Press Shift-Enter.
You should see the following:
Jupyter Notebook just ran the code you entered and then presented you with a new cell. Once again, newly created cells are code cells.
Enter the following into the new cell and run it. Once again, you run the code in a cell by selecting it and clicking the Run button or pressing Shift-Enter:
val items = listOf( "Alpha", "Bravo", "Charlie", "Delta" )
This time, you see a new cell but no output. That’s expected; creating a List
shouldn’t cause any output.
Try printing the contents of items
. Enter the following into the new cell and run it:
println(items)
You should see the following output:
[Alpha, Bravo, Charlie, Delta]
Note that the variable items
is still in scope. This is an important feature of notebooks: Anything you declare in a cell that you run remains declared for any cells you run afterward.
If you look at the cells you’ve run so far, you’ll see numbers to their left that show the order you ran the cells:
The numbers 1, 2 and 3 indicate the order in which you ran the cells. You first printed the sentence “Hello, Jupyter world!”, then defined the items
list and finally printed the content of items
.
You can declare more than just values for use by later cells. Functions, classes and just about anything that can be named or assigned to a variable can be declared for use in subsequent cells.
To see this in action, enter and run the following in a new code cell:
val rand = kotlin.random.Random fun rollTheDice(): Pair<Int, Int> { return Pair(rand.nextInt(6) + 1, rand.nextInt(6) + 1) } class Demo { fun sayHello() { println("Hello there!") } }
You’ve just defined a function named rollTheDice()
and a class named Demo
. Both will be in scope in subsequent cells.
Enter the following into a new code cell and run it:
rollTheDice()
You’ll see an ordered pair of two integers, each being between one and six inclusive.
Note that the cell you just ran contains only rollTheDice()
and no print()
or println()
function. Jupyter Notebook is made to execute a line of code that evaluates to a value, such as a variable name or function. It then prints out that value.
Classes also work as you would expect. Enter this code into a new code cell and run it:
val greeter = Demo() greeter.sayHello()
The notebook responds by printing Hello there!
immediately after the code cell.
If you’ve wondered why iOS developers got to use Swift Playgrounds but there wasn’t such a thing as Kotlin Playgrounds, here’s some good news: You have them now!