Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iframe is now loaded when using google colab #9

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_')