How I Made Python Interoperate with Kotlin

Shivhar Jalkote
Globant
Published in
4 min readSep 20, 2022

I’m working with POC (proof-of-concept), where we are using Ktor (Kotlin Framework) as a back-end DSL. In that project, we are extracting a summary from a substantial volume of text. We found it challenging to write machine learning code in Kotlin. Kotlin is young as compared to Python, R etc.

Any technology that we chose would need to be:

  • Have strong library support, allowing us to focus on business problems
  • Platform Independence
  • Good developer productivity
  • Reliable at scale
  • Future-proofed, able to support our business growth

We compared languages with these requirements in mind. We created the chart below to help us compare and contrast the strengths and weaknesses of each option.

Language Comparison

Python :

Pros :

  • Simple and concise
  • A great library ecosystem
  • Flexibility
  • Platform independence
  • Community support

Cons :

  • Lesser Speed
  • Runtime errors
  • Not easy to test

R :

Pros:

  • Open source
  • Used in Data wrangling
  • Used in Quality Plotting & Graphing
  • Platform Independent
  • Getting popular

Cons :

  • Weak origin
  • Data handling
  • Basic security
  • Lesser Speed

Kotlin :

Pros :

  • Inherits the Java ecosystem.
  • Null Safety
  • Fast and scalable
  • Has native primitives for concurrency

Cons :

  • Has limited support for machine learning
  • Has limited community support

We agreed to use Python for machine learning. Backend is developed in the Kotlin Language and text summarization is done using Python. We needed to interoperate between Kotlin and Python. These are some options for Kotlin-Python interoperability.

Interop Options

Jython :

Pros :

  • Interactive experimentation (Kotlin/Java & Python)
  • Rapid application development
  • Performance is better compared to Python

Cons :

  • Not all libraries support Jython
  • Need extra setup to run the program

A separate Python server interacting with the Kotlin server:

Pros :

  • Flexibility
  • Separation of concerns

Cons :

  • Latency
  • Extra network calls

Both options don’t fit the requirement so we decided to go with a Python script instead of a Python project. Python script will run on the same machine where the Kotlin server is available.

Environment Setup

Python script requires different libraries for execution. So we need some environment set up for it. For setup, please follow the below steps :

  1. Check Python is installed on your system using the command “python — version”. If Python is not installed, please install it.
  2. Check pip is installed on your system using the command “pip — version”. If pip is not installed, please install it.
  • Install pip using “Python3 get-pip.py” command.
  • Update pip to the latest version “pip3 install — upgrade pip”.

3. Install required libraries to Python scripts like (Numpy, and NLTK) using pip.

4. Run the Python script. For more information on how we built the Python script, refer to this article.

The above setup is required to run a Python script on your local machine. So another challenge is to run a Python script from Kotlin code and get the output.

Final Interop

Here is a piece of code we used for running Python script from Kotlin code.

This code is responsible for executing the script and getting output from it. Let’s understand the code step by step :

  1. Create a process to run the script :

Create a process which takes the summary script file path, summary text, and summary title as input parameters. This process is responsible for executing commands on the terminal and generating output.

2. Read the output :

We are creating the BufferReader for reading output from the terminal line by line.

3. Read the error logs:

If something went wrong while running the script or generating output, then we need to read error logs produced by the script or process. Using BufferReader reading error logs printed on the terminal.

4. Destroy the process : After reading the output of the script, we need to terminate the process.

Summary

Using the above steps we were able to execute the Python script, handle errors and provide its output as a response to our client. Hope it helps those who’re looking for a good solution for Kotlin-Python interoperability. To know more about how this piece fits into our larger project, refer to these articles:

UI and overall architecture

Extractive Summarization

Abstractive Summarization

Special thanks to Mukund Sharma, Mahesh Jadkar and Mitesh Dewda for conceiving and editing this article.

--

--