display.py 2.1 KB

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