From 6e3a5dfeaf311d30ffb454734beec4f21be135fb 2012-07-20 23:52:35 From: MinRK Date: 2012-07-20 23:52:35 Subject: [PATCH] add %%javascript, %%svg, and %%latex display magics adds %%latex example to notebook tour --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 4770c95..7accd97 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2040,7 +2040,7 @@ class InteractiveShell(SingletonConfigurable): self.define_magic = self.magics_manager.define_magic self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, - m.ConfigMagics, m.DeprecatedMagics, m.ExecutionMagics, + m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, ) diff --git a/IPython/core/magics/__init__.py b/IPython/core/magics/__init__.py index 3b72f15..29085bd 100644 --- a/IPython/core/magics/__init__.py +++ b/IPython/core/magics/__init__.py @@ -18,6 +18,7 @@ from .basic import BasicMagics from .code import CodeMagics, MacroToEdit from .config import ConfigMagics from .deprecated import DeprecatedMagics +from .display import DisplayMagics from .execution import ExecutionMagics from .extension import ExtensionMagics from .history import HistoryMagics diff --git a/IPython/core/magics/display.py b/IPython/core/magics/display.py new file mode 100644 index 0000000..4e6e234 --- /dev/null +++ b/IPython/core/magics/display.py @@ -0,0 +1,47 @@ +"""Simple magics for display formats""" +#----------------------------------------------------------------------------- +# Copyright (c) 2012 The IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + +# Our own packages +from IPython.core.display import display, Javascript, Latex, SVG +from IPython.core.magic import ( + Magics, magics_class, line_magic, cell_magic +) + +#----------------------------------------------------------------------------- +# Magic implementation classes +#----------------------------------------------------------------------------- + + +@magics_class +class DisplayMagics(Magics): + """Magics for displaying various output types with literals + + Defines javascript/latex cell magics for writing blocks in those languages, + to be rendered in the frontend. + """ + + @cell_magic + def javascript(self, line, cell): + """Run the cell block of Javascript code""" + display(Javascript(cell)) + + + @cell_magic + def latex(self, line, cell): + """Render the cell as a block of latex""" + display(Latex(cell)) + + @cell_magic + def svg(self, line, cell): + """Render the cell as an SVG literal""" + display(SVG(cell)) diff --git a/docs/examples/notebooks/00_notebook_tour.ipynb b/docs/examples/notebooks/00_notebook_tour.ipynb index 821bcf9..5e5acf5 100644 --- a/docs/examples/notebooks/00_notebook_tour.ipynb +++ b/docs/examples/notebooks/00_notebook_tour.ipynb @@ -1092,6 +1092,53 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "Or you can enter latex directly with the `%%latex` cell magic:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%%latex\n", + "\\begin{aligned}\n", + "\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", + "\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", + "\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", + "\\nabla \\cdot \\vec{\\mathbf{B}} & = 0\n", + "\\end{aligned}" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "latex": [ + "\\begin{aligned}\n", + "\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", + "\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", + "\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", + "\\nabla \\cdot \\vec{\\mathbf{B}} & = 0\n", + "\\end{aligned}" + ], + "output_type": "display_data", + "text": [ + "" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There is also a `%%javascript` cell magic for running javascript directly,\n", + "and `%%svg` for manually entering SVG content." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "# Loading external codes\n", "* Drag and drop a ``.py`` in the dashboard\n", "* Use ``%load`` with any local or remote url: [the Matplotlib Gallery!](http://matplotlib.sourceforge.net/gallery.html)\n", @@ -1192,4 +1239,4 @@ "metadata": {} } ] -} +} \ No newline at end of file