Skip to content

Instantly share code, notes, and snippets.

@korakot
Created April 11, 2018 10:01
Show Gist options
  • Save korakot/a3f12629efef2e38f1abbec9924245b5 to your computer and use it in GitHub Desktop.
Save korakot/a3f12629efef2e38f1abbec9924245b5 to your computer and use it in GitHub Desktop.
Create a new cell magic in Jupyter (and Colab)
from IPython.display import HTML
from IPython.core.magic import register_cell_magic
# equivalent to %%js
@register_cell_magic
def myjs(line, cell):
return HTML(f'''
<script>
{cell}
</script>
''')
# assign cell content to a variable too
@register_cell_magic
def var(line, cell):
name = line.split()[0]
globals()[name] = cell
return cell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment