| 1 |
"""Simple magics for display formats""" |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# Copyright (c) 2012 The IPython Development Team. |
| 4 |
# |
| 5 |
# Distributed under the terms of the Modified BSD License. |
| 6 |
# |
| 7 |
# The full license is in the file COPYING.txt, distributed with this software. |
| 8 |
#----------------------------------------------------------------------------- |
| 9 |
|
| 10 |
#----------------------------------------------------------------------------- |
| 11 |
# Imports |
| 12 |
#----------------------------------------------------------------------------- |
| 13 |
|
| 14 |
# Our own packages |
| 15 |
from IPython.display import display, Javascript, Latex, SVG, HTML, Markdown |
| 16 |
from IPython.core.magic import ( |
| 17 |
Magics, magics_class, cell_magic |
| 18 |
) |
| 19 |
from IPython.core import magic_arguments |
| 20 |
|
| 21 |
#----------------------------------------------------------------------------- |
| 22 |
# Magic implementation classes |
| 23 |
#----------------------------------------------------------------------------- |
| 24 |
|
| 25 |
|
| 26 |
@magics_class |
| 27 |
class DisplayMagics(Magics): |
| 28 |
"""Magics for displaying various output types with literals |
| 29 |
|
| 30 |
Defines javascript/latex/svg/html cell magics for writing |
| 31 |
blocks in those languages, to be rendered in the frontend. |
| 32 |
""" |
| 33 |
|
| 34 |
@cell_magic |
| 35 |
def js(self, line, cell): |
| 36 |
"""Run the cell block of Javascript code |
| 37 |
|
| 38 |
Alias of `%%javascript` |
| 39 |
|
| 40 |
Starting with IPython 8.0 %%javascript is pending deprecation to be replaced |
| 41 |
by a more flexible system |
| 42 |
|
| 43 |
Please See https://github.com/ipython/ipython/issues/13376 |
| 44 |
""" |
| 45 |
self.javascript(line, cell) |
| 46 |
|
| 47 |
@cell_magic |
| 48 |
def javascript(self, line, cell): |
| 49 |
"""Run the cell block of Javascript code |
| 50 |
|
| 51 |
Starting with IPython 8.0 %%javascript is pending deprecation to be replaced |
| 52 |
by a more flexible system |
| 53 |
|
| 54 |
Please See https://github.com/ipython/ipython/issues/13376 |
| 55 |
""" |
| 56 |
display(Javascript(cell)) |
| 57 |
|
| 58 |
|
| 59 |
@cell_magic |
| 60 |
def latex(self, line, cell): |
| 61 |
"""Render the cell as a block of LaTeX |
| 62 |
|
| 63 |
The subset of LaTeX which is supported depends on the implementation in |
| 64 |
the client. In the Jupyter Notebook, this magic only renders the subset |
| 65 |
of LaTeX defined by MathJax |
| 66 |
[here](https://docs.mathjax.org/en/v2.5-latest/tex.html).""" |
| 67 |
display(Latex(cell)) |
| 68 |
|
| 69 |
@cell_magic |
| 70 |
def svg(self, line, cell): |
| 71 |
"""Render the cell as an SVG literal""" |
| 72 |
display(SVG(cell)) |
| 73 |
|
| 74 |
@magic_arguments.magic_arguments() |
| 75 |
@magic_arguments.argument( |
| 76 |
'--isolated', action='store_true', default=False, |
| 77 |
help="""Annotate the cell as 'isolated'. |
| 78 |
Isolated cells are rendered inside their own <iframe> tag""" |
| 79 |
) |
| 80 |
@cell_magic |
| 81 |
def html(self, line, cell): |
| 82 |
"""Render the cell as a block of HTML""" |
| 83 |
args = magic_arguments.parse_argstring(self.html, line) |
| 84 |
html = HTML(cell) |
| 85 |
if args.isolated: |
| 86 |
display(html, metadata={'text/html':{'isolated':True}}) |
| 87 |
else: |
| 88 |
display(html) |
| 89 |
|
| 90 |
@cell_magic |
| 91 |
def markdown(self, line, cell): |
| 92 |
"""Render the cell as Markdown text block""" |
| 93 |
display(Markdown(cell)) |