semantically meaningful display outputs for notebooks¤
html is a data structure for visual information
import jinja2, datetime; from IPython.display import HTML
env = jinja2.Environment(loader=jinja2.DictLoader(templates := {}), extensions=["jinja2.ext.loopcontrols"]);
env.globals.update(dispatch=(dispatch := {}))
env.filters.update(dumps=__import__("json").dumps,**vars(__import__("builtins")))
import jinja2, datetime; from IPython.display import HTML
env = jinja2.Environment(loader=jinja2.DictLoader(templates := {}), extensions=["jinja2.ext.loopcontrols"]);
env.globals.update(dispatch=(dispatch := {}))
env.filters.update(dumps=__import__("json").dumps,**vars(__import__("builtins")))
template type dispatcher¤
for need to generate appropriate semantics for different data structures.
we'll use a dispatching dictionary to handle that work. when int or float types are found then the numeric templates is used, str types use the string, list and dict handle sequences and containers.
HTML("""<style>
data:empty::after {
content: attr(value);
}
</style>""")
dispatch.update(
numeric=(int, float), string=str, time=datetime.datetime, array=(list, tuple), mapping=dict, set=set)
function class module
templates.update(
string="""<samp>{{object}}</samp>""",
)
templates.update(
numeric="""<data value="{{object|dumps}}"></data>""",
time="""<time datetime="{{object.isoformat()}}">{{object}}</time>""",
format="""{% for f, t in dispatch.items() %}{% if object | isinstance(t) %}{% include f %}{% endif %}{% endfor %}""",
mapping="""<dl>{% for object, v in object.items() %}<dt>{% include "format" %}</dt><dd>{% set object = v %}{% include "format" %}</dd>{% endfor %}</dl>""", string="""{% if "://" in object %}<a href="{{object}}">{{object}}</a>{% else %}<samp>{{object}}</samp>{% endif %}""",
array="""<ol>{% for object in object %}<li>{% include "format" %}</li>{% endfor %}</ol>""",
set="""<ul>{% for object in object %}<li>{% include "format" %}</li>{% endfor %}</ul>""",
)
HTML(env.get_template("format").render(object={"a": 1}))
abc = "thanks for the fish"
abc
vocab typeof property resource prefix
itemid itemprop itemref itemscope itemtype
%reload_ext pidgy.extras
s = get_ipython()
times = list(itertools.chain.from_iterable(x.type_printers for x in s.display_formatter.formatters.values()))
y.type_printers