yamlmagic

A magic for making YAML in the IPython notebook

BSD-3-CLAUSE License

Downloads
1.5K
Stars
8
Committers
1

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# yamlmagic\n", "> an IPython magic for capturing data in YAML into a running IPython kernel.\n", "\n", "[![Build Status][svg]][status]\n", "![pypi][]\n", "\n", "[svg]: https://travis-ci.org/bollwyvl/yamlmagic.svg?branch=master\n", "[status]: https://travis-ci.org/bollwyvl/yamlmagic\n", "[pypi]: https://pypip.in/version/yamlmagic/badge.svg?style=flat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install\n", "From the command line (or with ! in a notebook cell):\n", "bash\n", "pip install yamlmagic\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Enable\n", "### Ad-hoc\n", "In the notebook, you can use the %load_ext or %reload_ext line magic." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%reload_ext yamlmagic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Configuration\n", "In your profile's ipython_kernel_config.py, you can add the following line to automatically load yamlmagic into all your running kernels:\n", "\n", "python\n", "c.InteractiveShellApp.extensions = ['yaml_magic']\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use\n", "The %%yaml cell magic will either act as simple parser:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " require(\n", " [\n", " "notebook/js/codecell",\n", " "codemirror/mode/yaml/yaml"\n", " ],\n", " function(cc){\n", " cc.CodeCell.options_default.highlight_modes.magic_yaml = {\n", " reg: ["^%%yaml"]\n", " }\n", " }\n", " );\n", " " ], "text/plain": [ "<IPython.core.display.Javascript object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{'a_toplevel_key': 1}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%yaml\n", "a_toplevel_key: 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which can be accessed by the special last result variable _:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'a_toplevel_key': 1}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or will update a named variable with the parsed document:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " require(\n", " [\n", " "notebook/js/codecell",\n", " "codemirror/mode/yaml/yaml"\n", " ],\n", " function(cc){\n", " cc.CodeCell.options_default.highlight_modes.magic_yaml = {\n", " reg: ["^%%yaml"]\n", " }\n", " }\n", " );\n", " " ], "text/plain": [ "<IPython.core.display.Javascript object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%yaml x\n", "- a: 1\n", " b: 2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[{'a': 1, 'b': 2}]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, yaml.SafeLoader will be used, which won't allow the powerful but dangerous (and unportable) !python/ tags. If you'd like to use them, provide the -l (or --loader) argument with a BaseLoader subclass available via a local variable..." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from yaml import Loader\n", "class FooLoader(Loader):\n", " # some special things you have built\n", " pass" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " require(\n", " [\n", " "notebook/js/codecell",\n", " "codemirror/mode/yaml/yaml"\n", " ],\n", " function(cc){\n", " cc.CodeCell.options_default.highlight_modes.magic_yaml = {\n", " reg: ["^%%yaml"]\n", " }\n", " }\n", " );\n", " " ], "text/plain": [ "<IPython.core.display.Javascript object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "[{'a': 1.0, 'b': 2.0}]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%yaml --loader FooLoader\n", "- a: !!python/float 1\n", " b: !!python/float 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...or dotted-notation path to a loader:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " require(\n", " [\n", " "notebook/js/codecell",\n", " "codemirror/mode/yaml/yaml"\n", " ],\n", " function(cc){\n", " cc.CodeCell.options_default.highlight_modes.magic_yaml = {\n", " reg: ["^%%yaml"]\n", " }\n", " }\n", " );\n", " " ], "text/plain": [ "<IPython.core.display.Javascript object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "[{'a': 1.0, 'b': 2.0}]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%yaml --loader yaml.Loader\n", "- a: !!python/float 1\n", " b: !!python/float 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contribute\n", "Issues and pull requests welcome!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## License\n", "yamlmagic is released as free software under the BSD 3-Clause license." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Thank\n", "- @tonyfast for asking for this\n", "- @robclewley for documentation-shaming a gist into a module " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }