Skip to content

Commit dcda58b

Browse files
authored
Enable WIT usage in JupyterLab/Cloud AI Platform notebooks (#2301)
Update WitWidget packaging/logic to enable it to work in JupyterLab notebooks, which handle extensions like WitWidget differently than standard Jupyter notebooks. This also enables use in Cloud AI Platform notebooks.
1 parent d1104f6 commit dcda58b

File tree

8 files changed

+52
-22
lines changed

8 files changed

+52
-22
lines changed

tensorboard/plugins/interactive_inference/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,19 @@ containing:
312312
```
313313
!pip install witwidget
314314
```
315-
For TensorFlow GPU support, use the witwidget-gpu package instead of witwidget.
315+
For TensorFlow GPU support, use the `witwidget-gpu` package instead of `witwidget`.
316316

317317
Then, use it as seen at the bottom of the
318318
[What_If_Tool_Notebook_Usage.ipynb notebook](https://colab.research.google.com/github/tensorflow/tensorboard/blob/master/tensorboard/plugins/interactive_inference/What_If_Tool_Notebook_Usage.ipynb).
319+
320+
### How do I enable it for use in a JupyterLab or Cloud AI Platform notebook?
321+
Install and enable WIT for JupyterLab by running a cell containing:
322+
```
323+
!pip install witwidget
324+
!jupyter labextension install wit-widget
325+
!jupyter labextension install @jupyter-widgets/jupyterlab-manager
326+
```
327+
For TensorFlow GPU support, use the `witwidget-gpu` package instead of `witwidget`.
328+
Note that you may need to run `!sudo jupyter labextension ...` commands depending on your notebook setup.
329+
330+
Use of WIT after installation is the same as with the other notebook installations.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# wit-widget: What-If Tool JupyterLab extension
2+
3+
This npm package is for using the [What-If Tool](https://pair-code.github.io/what-if-tool/) as a JupyterLab extension.
4+
5+
For more information, see the [What-If Tool documentation](https://github.com/tensorflow/tensorboard/blob/master/tensorboard/plugins/interactive_inference/README.md).

tensorboard/plugins/interactive_inference/witwidget/notebook/jupyter/js/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
1515

16-
window.__webpack_public_path__ =
16+
window.__nbextension_path__ =
1717
document.querySelector('body').getAttribute('data-base-url') +
1818
'nbextensions/wit-widget/';
1919

tensorboard/plugins/interactive_inference/witwidget/notebook/jupyter/js/lib/wit.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
1515

16+
var witHtmlLocation = require('file-loader!./wit_jupyter.html');
1617
var widgets = require('@jupyter-widgets/base');
1718

1819
// What-If Tool View. Renders the tool and provides communication with the
@@ -40,9 +41,14 @@ var WITView = widgets.DOMWidgetView.extend({
4041
const height = parseInt(
4142
this.model.attributes.layout.attributes.height, 10) - 20;
4243
const iframe = document.createElement('iframe');
43-
const templateLocation =
44-
window.__webpack_public_path__ + 'wit_jupyter.html';
45-
const src = `<link rel="import" href="${templateLocation}">
44+
45+
// Adjust WIT html location if running in a jupyter notebook
46+
// and not in jupyterlab.
47+
if (document.body.getAttribute('data-base-url') != null) {
48+
witHtmlLocation = window.__nbextension_path__ + 'wit_jupyter.html';
49+
}
50+
51+
const src = `<link rel="import" href="${witHtmlLocation}">
4652
<tf-interactive-inference-dashboard local id="wit"></tf-interactive-inference-dashboard>
4753
<script>
4854
const wit = document.getElementById('wit');

tensorboard/plugins/interactive_inference/witwidget/notebook/jupyter/js/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
"name": "wit-widget",
33
"description": "What-If Tool jupyter widget",
44
"author": "Google LLC",
5-
"main": "lib/index.js",
6-
"repository": {
7-
"type": "git",
8-
"url": "https://github.com/tensorflow/tensorboard.git"
9-
},
10-
"private": true,
5+
"main": "dist/index.js",
6+
"version": "1.1.0",
7+
"license" : "Apache 2.0",
8+
"homepage": "https://github.com/tensorflow/tensorboard/tree/master/tensorboard/plugins/interactive_inference",
119
"keywords": [
1210
"jupyter",
1311
"widgets",
@@ -16,19 +14,21 @@
1614
"jupyterlab-extension"
1715
],
1816
"files": [
19-
"lib/**/*.js",
20-
"dist/*.js"
17+
"dist/*",
18+
"lib/*"
2119
],
2220
"scripts": {
2321
"build": "webpack",
2422
"test": "echo \"Error: no test specified\" && exit 1"
2523
},
2624
"devDependencies": {
27-
"webpack": "^3.5.5",
28-
"rimraf": "^2.6.1"
25+
"file-loader": "^3.0.1",
26+
"rimraf": "^2.6.1",
27+
"webpack": "^3.5.5"
2928
},
3029
"dependencies": {
31-
"@jupyter-widgets/base": "^1.0.0"
30+
"@jupyter-widgets/base": "^1.0.0",
31+
"@jupyter-widgets/jupyterlab-manager": "^0.38.1"
3232
},
3333
"jupyterlab": {
3434
"extension": "lib/labplugin"

tensorboard/plugins/interactive_inference/witwidget/notebook/jupyter/js/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var version = require('./package.json').version;
1919
// Custom webpack rules are generally the same for all webpack bundles, hence
2020
// stored in a separate local variable.
2121
var rules = [
22-
{ test: /\.css$/, use: ['style-loader', 'css-loader']}
22+
{ test: /\.css$/, use: ['style-loader', 'css-loader']},
23+
{ test: /\.(html)$/, use: [{loader: 'file-loader', options: {}}]},
2324
]
2425

2526

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# To release a new version of witwidget on PyPI:
1+
# To release a new version of witwidget on PyPI and NPM:
22

3-
1. Update tensorboard/plugins/interactive_inference/witwidget/version.py (set release version, remove 'dev')
4-
2. `bazel run tensorboard/plugins/interactive_inference/witwidget/pip_package:build_pip_package`
5-
3. Upload the whl files created by the previous step to PyPI as per instructions
3+
1. Ensure updated version numbers have been merged to the master branch in
4+
tensorboard/plugins/interactive_inference/witwidget/version.py
5+
and tensorboard/plugins/interactive_inference/witwidget/notebook/jupyter/js/package.json
6+
2. Clone this repository and checkout the commit that set the new version numbers.
7+
3. `bazel run tensorboard/plugins/interactive_inference/witwidget/pip_package:build_pip_package`
8+
4. Upload the whl files created by the previous step to PyPI as per instructions
69
at https://packaging.python.org/tutorials/packaging-projects/#uploading-the-distribution-archives.
7-
4. Commit the version.py change.
10+
5. Publish the new NPM package through running `npm publish` in the `js/` subdirectory of the package
11+
files generated during the build step.
12+

tensorboard/plugins/interactive_inference/witwidget/pip_package/build_pip_package.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mkdir -p js
4040
pushd js
4141

4242
cp -LR "$plugin_runfile_dir"/witwidget/notebook/jupyter/js/* .
43+
cp "$plugin_runfile_dir/tf_interactive_inference_dashboard/wit_jupyter.html" lib/
4344

4445
# Install Node dependencies
4546
npm install

0 commit comments

Comments
 (0)