an IPython magic for capturing data in YAML into a running IPython kernel.
From the command line (or with ! in a notebook cell):
pip install git+https://github.com/josejg/yamlmagic.gitIn the notebook, you can use the %load_ext or %reload_ext line
magic.
%reload_ext yamlmagicIn your profile's ipython_kernel_config.py, you can add the
following line to automatically load yamlmagic into all your running
kernels:
c.InteractiveShellApp.extensions = ['yaml_magic']The %%yaml cell magic will either act as simple parser:
%%yaml
a_toplevel_key: 1<IPython.core.display.Javascript object>
{'a_toplevel_key': 1}
which can be accessed by the special last result variable _:
_{'a_toplevel_key': 1}
Or will update a named variable with the parsed document:
%%yaml x
- a: 1
b: 2<IPython.core.display.Javascript object>
x[{'a': 1, 'b': 2}]
By default, yaml.SafeLoader will be used, which won't allow the
powerful but
dangerous
(and unportable) `!python/
tags <http://pyyaml.org/wiki/PyYAMLDocumentation#YAMLtagsandPythontypes>`__.
If you'd like to use them, provide the -l (or --loader) argument
with a BaseLoader subclass available via a local variable...
from yaml import Loader
class FooLoader(Loader):
# some special things you have built
pass%%yaml --loader FooLoader
!!python/float 0<IPython.core.display.Javascript object>
0.0
...or dotted-notation path to a loader:
%%yaml --loader yaml.Loader
!!python/float 0<IPython.core.display.Javascript object>
0.0
Issues and pull requests welcome!
yamlmagic is released as free software under the BSD 3-Clause
license.
- [@tonyfast](http://robclewley.github.io) for asking for this
- [@robclewley](http://robclewley.github.io) for documentation-shaming a gist into a module