Exemple #1
0
    def _serve(self, blocking=False, debug=False, port=None, bind_all=False):
        from captum.insights.attr_vis.server import start_server

        return start_server(self,
                            blocking=blocking,
                            debug=debug,
                            _port=port,
                            bind_all=bind_all)
Exemple #2
0
    def _serve_colab(self, blocking=False, debug=False, port=None):
        import ipywidgets as widgets
        from IPython.display import HTML, display

        from captum.insights.attr_vis.server import start_server

        # TODO: Output widget only captures beginning of server logs. It seems
        # the context manager isn't respected when the web server is run on a
        # separate thread. We should fix to display entirety of the logs
        out = widgets.Output()
        with out:
            port = start_server(self,
                                blocking=blocking,
                                debug=debug,
                                _port=port)
        shell = """
            <div id="root"></div>
            <script>
            (function() {
              document.querySelector("base").href = "http://localhost:%PORT%";
              function reloadScriptsAndCSS(root) {
                // Referencing TensorBoard's method for reloading scripts,
                // we remove and reinsert each script
                for (const script of root.querySelectorAll("script")) {
                  const newScript = document.createElement("script");
                  newScript.type = script.type;
                  if (script.src) {
                    newScript.src = script.src;
                  }
                  if (script.textContent) {
                    newScript.textContent = script.textContent;
                  }
                  root.appendChild(newScript);
                  script.remove();
                }
                // A similar method is used to reload styles
                for (const link of root.querySelectorAll("link")) {
                  const newLink = document.createElement("link");
                  newLink.rel = link.rel;
                  newLink.href = link.href;
                  document.querySelector("head").appendChild(newLink);
                  link.remove();
                }
              }
              const root = document.getElementById("root");
              fetch(".")
                .then(x => x.text())
                .then(html => void (root.innerHTML = html))
                .then(() => reloadScriptsAndCSS(root));
            })();
            </script>
        """.replace("%PORT%", str(port))
        html = HTML(shell)
        display(html)
        display(out)