Skip to content

Commit 2f76de1

Browse files
authored
core: error msg for dynamic plugin in Colab (#2388)
Colab is iterating on mechanism to use iframe within an output frame but it is not yet ready for consumption. As a result, all usage of iframes do not work properly in Colab and we should like to communicate that to users.
1 parent d151be0 commit 2f76de1

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

tensorboard/components/tf_tensorboard/tf-tensorboard.html

+53-21
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,20 @@ <h3>There’s no dashboard by the name of “<tt>[[_selectedDashboard]]</tt>.”
365365
}
366366
}
367367
</style>
368+
<template id="colabNotSupported" preserve-content>
369+
<div style="max-width: 540px; margin: 80px auto 0 auto;">
370+
<h3>Dynamic plugin isn’t supported in Colab yet.</h3>
371+
<p>
372+
Please see
373+
<a
374+
href="https://github.com/tensorflow/tensorboard/issues/1913"
375+
rel="noopener"
376+
target="_blank"
377+
>GitHub issue #1913</a>
378+
for more information.
379+
</p>
380+
</div>
381+
</template>
368382
</template>
369383
<script src="autoReloadBehavior.js"></script>
370384
<script>
@@ -581,7 +595,12 @@ <h3>There’s no dashboard by the name of “<tt>[[_selectedDashboard]]</tt>.”
581595
_refreshing: {
582596
type: Boolean,
583597
value: false,
584-
}
598+
},
599+
_inColab: {
600+
type: Boolean,
601+
value: () => !!(window.TENSORBOARD_ENV || {}).IN_COLAB,
602+
readOnly: true,
603+
},
585604
},
586605
observers: [
587606
('_updateSelectedDashboardFromActive(' +
@@ -757,26 +776,8 @@ <h3>There’s no dashboard by the name of “<tt>[[_selectedDashboard]]</tt>.”
757776
break;
758777
}
759778
case 'IFRAME': {
760-
const iframe = document.createElement('iframe');
761-
iframe.id = 'dashboard'; // used in `_selectedDashboardComponent`
762-
this.scopeSubtree(iframe);
763-
container.appendChild(iframe);
764-
const subdocument = iframe.contentDocument;
765-
const pluginBasePath = tf_backend.getRouter()
766-
.pluginRoute(selectedDashboard, '/')
767-
const base = subdocument.createElement('base');
768-
// TODO(stephanwlee): Use sanitized URL when setting the href.
769-
// setAttribute is a bypass for the security conformance which we
770-
// have no way to address.
771-
base.setAttribute(
772-
'href',
773-
String(new URL(pluginBasePath, window.location.href)));
774-
subdocument.head.appendChild(base);
775-
const script = subdocument.createElement('script');
776-
const moduleString = JSON.stringify(loadingMechanism.modulePath);
777-
script.textContent =
778-
`import(${moduleString}).then((m) => void m.render());`;
779-
subdocument.body.appendChild(script);
779+
this._renderPluginIframe(
780+
container, selectedDashboard, loadingMechanism);
780781
break;
781782
}
782783
default: {
@@ -788,6 +789,37 @@ <h3>There’s no dashboard by the name of “<tt>[[_selectedDashboard]]</tt>.”
788789
this.set('_isReloadDisabled', dashboard.disableReload);
789790
},
790791

792+
_renderPluginIframe(container, selectedDashboard, loadingMechanism) {
793+
if (this._inColab) {
794+
const templateContent = this.$.colabNotSupported.content;
795+
const node = document.importNode(templateContent, true);
796+
node.id = 'dashboard'; // used in `_selectedDashboardComponent`
797+
this.scopeSubtree(node);
798+
container.appendChild(node);
799+
return;
800+
}
801+
const iframe = document.createElement('iframe');
802+
iframe.id = 'dashboard'; // used in `_selectedDashboardComponent`
803+
this.scopeSubtree(iframe);
804+
container.appendChild(iframe);
805+
const subdocument = iframe.contentDocument;
806+
const pluginBasePath = tf_backend.getRouter()
807+
.pluginRoute(selectedDashboard, '/')
808+
const base = subdocument.createElement('base');
809+
// TODO(stephanwlee): Use sanitized URL when setting the href.
810+
// setAttribute is a bypass for the security conformance which we
811+
// have no way to address.
812+
base.setAttribute(
813+
'href',
814+
String(new URL(pluginBasePath, window.location.href)));
815+
subdocument.head.appendChild(base);
816+
const script = subdocument.createElement('script');
817+
const moduleString = JSON.stringify(loadingMechanism.modulePath);
818+
script.textContent =
819+
`import(${moduleString}).then((m) => void m.render());`;
820+
subdocument.body.appendChild(script);
821+
},
822+
791823
/**
792824
* Get the Polymer component corresponding to the currently
793825
* selected dashboard. For instance, the result might be an

0 commit comments

Comments
 (0)