KarthikRIyer / swiftplot

Swift library for Data Visualization :bar_chart:
Apache License 2.0
398 stars 36 forks source link

Example notebooks don't work in Colab but work in Jupyter #117

Closed vojtamolda closed 4 years ago

vojtamolda commented 4 years ago

Hello everyone,

I'm having trouble running the machine learning example notebook in Google Colab. When I open and run the cells, the compiling, installation and execution happen perfectly without errors but none of the plots are displayed.

The problem seems to be specific to Colab. When I start a swift-jupter kernel and do the same process locally in my own Jupyter notebook all is displayed and works well.

Here's a link to open the example for reproduction.

Does anyone has any idea how to fix it? There's a decent chance that it may be out of anyone's but a Googlers' reach. I quickly checked the messages being passed to the kernel and everything looks correct.

Thanks and cheers,

Vojta

KarthikRIyer commented 4 years ago

@vojtamolda The notebooks use pure swift to generate the Jupyter messages. For some reason that does not work on colab. To display the plot on colab, instead of including the EnableJupyterDisplay.swift file use this:

import Python
%include "EnableIPythonDisplay.swift"
func display(base64EncodedPNG: String) {
  let displayImage = Python.import("IPython.display")
  let codecs = Python.import("codecs")
  let imageData = codecs.decode(Python.bytes(base64EncodedPNG, encoding: "utf8"), encoding: "base64")
  displayImage.Image(data: imageData, format: "png").display()
}

This is also mentioned in the README under How to include the library in your Jupyter Notebook.

Hope this helps :smile:

vojtamolda commented 4 years ago

Oh... Thanks for such a quick response and sorry that I missed it in the readme file.

I see now. It works around the bug by using the Python bridge that should be theoretically passing the same messages as the code in EnableJupyterDisplay.swift but somehow it actually works.