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

Custom display API #3770

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion tensorboard/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,32 @@
_CONTEXT_COLAB = "_CONTEXT_COLAB"
_CONTEXT_IPYTHON = "_CONTEXT_IPYTHON"
_CONTEXT_NONE = "_CONTEXT_NONE"
_CONTEXT_CUSTOM = "_CONTEXT_CUSTOM"

_custom_display = None


def register_custom_display(func):
jerrylian-db marked this conversation as resolved.
Show resolved Hide resolved
global _custom_display
_custom_display = func


def _get_context():
"""Determine the most specific context that we're in.

Returns:
_CONTEXT_CUSTOM: If user has registered a custom display function.
_CONTEXT_COLAB: If in Colab with an IPython notebook context.
_CONTEXT_IPYTHON: If not in Colab, but we are in an IPython notebook
context (e.g., from running `jupyter notebook` at the command
line).
_CONTEXT_NONE: Otherwise (e.g., by running a Python script at the
command-line or using the `ipython` interactive shell).
"""

if _custom_display is not None:
return _CONTEXT_CUSTOM

# In Colab, the `google.colab` module is available, but the shell
# returned by `IPython.get_ipython` does not have a `get_trait`
# method.
Expand Down Expand Up @@ -145,7 +158,7 @@ def start(args_string):
except ImportError:
IPython = None

if context == _CONTEXT_NONE:
if context == _CONTEXT_NONE or context == _CONTEXT_CUSTOM:
jerrylian-db marked this conversation as resolved.
Show resolved Hide resolved
handle = None
print("Launching TensorBoard...")
else:
Expand Down Expand Up @@ -314,6 +327,7 @@ def _display(port=None, height=None, print_message=False, display_handle=None):
pass

fn = {
_CONTEXT_CUSTOM: _custom_display,
_CONTEXT_COLAB: _display_colab,
_CONTEXT_IPYTHON: _display_ipython,
_CONTEXT_NONE: _display_cli,
Expand Down
79 changes: 79 additions & 0 deletions tensorboard/notebook_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorboard import notebook
from tensorboard import manager
from tensorboard import test as tb_test

try:
# python version >= 3.3
from unittest import mock
except ImportError:
import mock # pylint: disable=unused-import


class CustomDisplayAPITest(tb_test.TestCase):
"""Tests for `notebook.register_custom_display()`."""

counter = 0

# Mock a StartLaunched result to prevent starting a Tensorboard process
# when calling notebook.start()
_info = manager.TensorBoardInfo(
version="x.x",
start_time=0,
pid=0,
port=6006,
path_prefix="prefix",
logdir="dir",
db="db",
cache_key="key",
)
_startup_result = manager.StartLaunched(info=_info)

def custom_display(self, port, height, display_handle):
del port
del height
del display_handle
self.counter = 1

def reset(self):
self.counter = 0

def test_get_context(self):
notebook.register_custom_display(self.custom_display)
self.assertEqual(notebook._get_context(), "_CONTEXT_CUSTOM")

def test_display(self):
notebook.register_custom_display(self.custom_display)
notebook.display(6006)
self.assertEqual(self.counter, 1)
self.reset()

def test_start(self):
with mock.patch(
"tensorboard.manager.start", lambda _: self._startup_result
):
notebook.register_custom_display(self.custom_display)
notebook.start("--logdir test")
self.assertEqual(self.counter, 1)
self.reset()


if __name__ == "__main__":
tb_test.main()