Source code for Goulib.notebook

#!/usr/bin/env python
# coding: utf8
python function to write html texts and values inline within a Notebook

Advantagously replaces "print" since this is more powerfull and has no compatibility issues between python 2 and 3

exemple: h('the result is',time,'[ms]')


from __future__ import print_function

__author__ = "Philippe Guglielmetti"
__copyright__ = "Copyright 2016, Philippe Guglielmetti"
__credits__= [""]
__license__ = "LGPL"

import logging, six

from IPython.display import display, HTML, Math
from .markup import tag
from .itertools2 import isiterable

[docs]def html(obj, sep=None): try: return obj._repr_html_() except AttributeError: pass #skip logging.error if sep is None: sep=' ' bra,ket='','' else: if isinstance(obj,dict): res=',\n'.join("%s:%s"%(html(k),html(v)) for k,v in six.iteritems(obj)) return '{%s}'%res elif isinstance(obj,list): bra,ket='[',']' else: bra,ket='(',')' if isiterable(obj): #iterable, but not a string return bra+sep.join(html(a,sep=',') for a in obj)+ket try: return unicode(obj,'utf-8') #to render accented chars correctly except : pass return str(obj)
[docs]def h1(*args): display(HTML(tag('h1',html(args))))
[docs]def h2(*args): display(HTML(tag('h2',html(args))))
[docs]def h3(*args): display(HTML(tag('h3',html(args))))
[docs]def h4(*args): display(HTML(tag('h4',html(args))))
[docs]def h(*args): display(HTML(html(args)))
#redefine "print" for notebooks ... try: # get_ipython #is defined from within IPython (notebook) except: pass else: pass #for pythoscope # print = h # this is ok in Python 3, but not before
[docs]def hinfo(*args): display(HTML(tag('div',html(args),style="background-color:#337ab7;color:#ffffff")))
[docs]def hsuccess(*args): display(HTML(tag('div',html(args),style="background-color:#5cb85c;color:#ffffff")))
[docs]def hwarning(*args): display(HTML(tag('div',html(args),style="background-color:#f0ad4e;color:#ffffff")))
[docs]def herror(*args): display(HTML(tag('div',html(args),style="background-color:#d9534f;color:#ffffff")))
[docs]def latex(obj): """ to force LaTeX representation """ return Math(obj.latex())