Skip to content

Commit

Permalink
iframe is now loaded when using google colab (#9)
Browse files Browse the repository at this point in the history
Part of fix for #3

Added logic to check if the script is being ran in google colab, then replace the server url with proxied url for the specified port if that is the case
Tested in google colab, created a widget and verified that the iframe appeared
!apt-get install openjdk-11-jdk-headless

import os
from google.colab.output import eval_js
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-11-openjdk-amd64"

from google.colab import drive
drive.mount('/content/drive')

!pip install --upgrade pip setuptools wheel
!pip install deephaven-core deephaven-server
!pip install '/content/drive/MyDrive/deephaven_ipywidgets-0.2.0-py2.py3-none-any.whl'

import deephaven_server
from deephaven_server import Server
import portpicker
from google.colab.output import eval_js

dh_port = portpicker.pick_unused_port()
s = Server(port=dh_port, jvm_args=["-Xmx4g", "-Dhttp.requireHttp2=false"], dh_args={'http.requireHttp2': False})
s.start()

from google.colab import output
output.enable_custom_widget_manager()

from deephaven import empty_table
t = empty_table(10).update("x=i")

from deephaven_ipywidgets import DeephavenWidget
DeephavenWidget(t)
  • Loading branch information
jnumainville authored Dec 16, 2022
1 parent 564576a commit ee39e19
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion deephaven_ipywidgets/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# Copyright (c) Deephaven Data Labs LLC.
# Distributed under the terms of the Modified BSD License.

version_info = (0, 2, 0)
version_info = (0, 2, 1)
__version__ = ".".join(map(str, version_info))
34 changes: 22 additions & 12 deletions deephaven_ipywidgets/deephaven.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
from uuid import uuid4
from ._frontend import module_name, module_version


def _str_object_type(obj):
"""Returns the object type as a string value"""
return f"{obj.__class__.__module__}.{obj.__class__.__name__}"
"""Returns the object type as a string value"""
return f"{obj.__class__.__module__}.{obj.__class__.__name__}"


def _path_for_object(obj):
"""Return the iframe path for the specified object. Inspects the class name to determine."""
name = _str_object_type(obj)
"""Return the iframe path for the specified object. Inspects the class name to determine."""
name = _str_object_type(obj)

if name in ('deephaven.table.Table', 'pandas.core.frame.DataFrame'):
return 'table'
if name == 'deephaven.plot.figure.Figure':
return 'chart'
raise TypeError(f"Unknown object type: {name}")
if name in ('deephaven.table.Table', 'pandas.core.frame.DataFrame'):
return 'table'
if name == 'deephaven.plot.figure.Figure':
return 'chart'
raise TypeError(f"Unknown object type: {name}")


class DeephavenWidget(DOMWidget):
Expand Down Expand Up @@ -60,9 +62,17 @@ def __init__(self, deephaven_object, height=600, width=0):
# Generate a new table ID using a UUID prepended with a `t_` prefix
object_id = f"t_{str(uuid4()).replace('-', '_')}"

port = Server.instance.port
server_url = f"http://localhost:{Server.instance.port}/"

try:
from google.colab.output import eval_js
server_url = eval_js(f"google.colab.kernel.proxyPort({port})")
except ImportError:
pass

# Generate the iframe_url from the object type
server_url = f"http://localhost:{Server.instance.port}"
iframe_url = f"{server_url}/iframe/{_path_for_object(deephaven_object)}/?name={object_id}"
iframe_url = f"{server_url}iframe/{_path_for_object(deephaven_object)}/?name={object_id}"

# Add the table to the main modules globals list so it can be retrieved by the iframe
__main__.__dict__[object_id] = deephaven_object
Expand All @@ -72,4 +82,4 @@ def __init__(self, deephaven_object, height=600, width=0):
self.set_trait('object_id', object_id)
self.set_trait('object_type', _str_object_type(deephaven_object))
self.set_trait('width', width)
self.set_trait('height', height)
self.set_trait('height', height)
2 changes: 1 addition & 1 deletion deephaven_ipywidgets/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ def test_example_creation_blank(MockTableClass):
mock_table.__class__.__module__ = 'deephaven.table'
mock_table.__class__.__name__ = 'Table'
w = DeephavenWidget(mock_table)
assert w.server_url == 'http://localhost:9876'
assert w.server_url == 'http://localhost:9876/'
assert str(w.iframe_url).startswith('http://localhost:9876/iframe/table/?name=t_')

0 comments on commit ee39e19

Please sign in to comment.