treetrace.ext.google_colab

 1import IPython.display
 2
 3
 4def detect_colab():
 5    try:
 6        import google.colab  # noqa: F401
 7
 8        return True
 9    except ModuleNotFoundError:
10        return False
11
12
13def iframe_for_port(port, width="95%", height=600):
14    return IPython.display.Javascript(
15        """
16            (async ()=>{
17                fm = document.createElement('iframe')
18                fm.src = await google.colab.kernel.proxyPort(%s)
19                fm.width = '%s'
20                fm.height = '%s'
21                fm.frameBorder = 0
22                fm.style = 'background: white;'
23                document.body.append(fm)
24            })();
25            """
26        % (port, width, height)
27    )