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 15from IPython.core.display import display, Javascript, Latex, SVG, HTML 16from IPython.core.magic import ( 17 Magics, magics_class, cell_magic 18) 19 20#----------------------------------------------------------------------------- 21# Magic implementation classes 22#----------------------------------------------------------------------------- 23 24 25@magics_class 26class DisplayMagics(Magics): 27 """Magics for displaying various output types with literals 28 29 Defines javascript/latex/svg/html cell magics for writing 30 blocks in those languages, to be rendered in the frontend. 31 """ 32 33 @cell_magic 34 def js(self, line, cell): 35 """Run the cell block of Javascript code 36 37 Alias of `%%javascript` 38 """ 39 self.javascript(line, cell) 40 41 @cell_magic 42 def javascript(self, line, cell): 43 """Run the cell block of Javascript code""" 44 display(Javascript(cell)) 45 46 47 @cell_magic 48 def latex(self, line, cell): 49 """Render the cell as a block of latex 50 51 The subset of latex which is support depends on the implementation in 52 the client. In the Jupyter Notebook, this magic only renders the subset 53 of latex defined by MathJax 54 [here](https://docs.mathjax.org/en/v2.5-latest/tex.html).""" 55 display(Latex(cell)) 56 57 @cell_magic 58 def svg(self, line, cell): 59 """Render the cell as an SVG literal""" 60 display(SVG(cell)) 61 62 @cell_magic 63 def html(self, line, cell): 64 """Render the cell as a block of HTML""" 65 display(HTML(cell)) 66