4 ms·
Best solution I've found is to paste the whole thing into a cell with this header #@title Give your cell a headline %%js_module where js_module is def
by bsimpson 3y ago
Best solution I've found is to paste the whole thing into a cell with this header
#@title Give your cell a headline
%%js_module
where js_module is defined here:
# Copyright 2023 Google LLC.
# SPDX-License-Identifier: Apache-2.0
from IPython.core.display import display, HTML
from IPython.core.magic import register_cell_magic
import time
@register_cell_magic
def js_module(line, cell):
if cell.startswith('http'):
src, hash = cell.split('#') if '#' in cell else [cell, '']
timestamp = time.time() if 'cachebust' in line else ''
display(HTML(
"<script type = 'module' src = '{src}?{timestamp}'></script>".format(src = src, hash = hash, timestamp = timestamp)
))
else:
display(HTML(
"<script type = 'module'>{cell}</script>".format(cell = cell)
))
The #@title lets you collapse the cell, so the JavaScript isn't showing. Colab has a strong philosophy of ensuring the user can see everything they're running, so I don't think there's a better way to encapsulate the bundled JS.