Skip to content

Commit 95b1970

Browse files
committed
torchx/notebook: added workspacefile magic for use with notebooks
1 parent 0f81544 commit 95b1970

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ torchtext>=0.11.0
1919
torchvision>=0.11.1
2020
ts>=0.5.1
2121
usort==0.6.4
22+
ipython

torchx/notebook.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Facebook, Inc. and its affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
"""
9+
This contains TorchX utilities for creating and running components and apps from
10+
an Jupyter/IPython Notebook.
11+
"""
12+
13+
import posixpath
14+
15+
import fsspec
16+
from IPython.core.magic import register_cell_magic
17+
18+
19+
def get_workspace() -> str:
20+
"""
21+
get_workspace returns the TorchX notebook workspace fsspec path.
22+
"""
23+
return "memory://torchx-workspace/"
24+
25+
26+
@register_cell_magic
27+
def workspacefile(line: str, cell: str) -> None:
28+
workspace = get_workspace()
29+
fs, path = fsspec.core.url_to_fs(workspace)
30+
path = posixpath.join(path, line)
31+
32+
base = posixpath.dirname(path)
33+
if not fs.exists(base):
34+
fs.mkdirs(base, exist_ok=True)
35+
36+
with fs.open(path, "wt") as f:
37+
f.write(cell)
38+
39+
print(f"Added {line} to workspace {workspace}")

torchx/test/notebook_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Facebook, Inc. and its affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
import io
9+
import posixpath
10+
import unittest
11+
from unittest.mock import MagicMock, patch
12+
13+
import fsspec
14+
from IPython.testing.globalipapp import get_ipython
15+
16+
17+
class VersionTest(unittest.TestCase):
18+
@classmethod
19+
def setUpClass(cls) -> None:
20+
cls.ip = get_ipython()
21+
22+
def test_get_workspace(self) -> None:
23+
from torchx.notebook import get_workspace
24+
25+
self.assertEqual(get_workspace(), "memory://torchx-workspace/")
26+
27+
@patch("sys.stdout", new_callable=io.StringIO)
28+
def test_workspacefile(self, stdout: MagicMock) -> None:
29+
from torchx.notebook import get_workspace
30+
31+
cell = "print('Arrakis')"
32+
file_path = "foo/bar.py"
33+
out = self.ip.run_cell_magic("workspacefile", file_path, cell)
34+
self.assertEqual(out, None)
35+
self.assertEqual(
36+
stdout.getvalue(),
37+
"Added foo/bar.py to workspace memory://torchx-workspace/\n",
38+
)
39+
fs, workspace_path = fsspec.core.url_to_fs(get_workspace())
40+
path = posixpath.join(workspace_path, file_path)
41+
with fs.open(path, "rt") as f:
42+
self.assertEqual(f.read(), cell)

0 commit comments

Comments
 (0)