mlsnippet.report¶
-
class
mlsnippet.report.Container(children=None)¶ Bases:
mlsnippet.report.element.ElementElementclass built up with children elements.The
Containerclass is a base class for implementingElementwhich is rendered mainly by rendering children elements one after another.-
__init__(children=None)¶ Construct the
Container.Parameters: children – An Element, a list ofElement, or a list of objects which can be converted intoElementviaas_element(). (defaultNone, no children is specified)
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
add(child)¶ Add a child
Element.Parameters: child – Object which can be converted into Elementbyas_element().
-
-
class
mlsnippet.report.Block(children=None)¶ Bases:
mlsnippet.report.container.ContainerSubclass of
Container, which renders all its children elements within of<div class="block">...</div>.-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
-
class
mlsnippet.report.Paragraph(children=None)¶ Bases:
mlsnippet.report.container.ContainerSubclass of
Container, which renders all its children elements within of<p>...</p>-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
-
mlsnippet.report.paragraph_text(text)¶ Create a
Textenclosed in aParagraph.Parameters: text (str) – Text content. Returns: The Paragraph.Return type: Paragraph
-
class
mlsnippet.report.Section(title, children=None)¶ Bases:
mlsnippet.report.container.ContainerSubclass of
Container, which renders all its children elements within<div class="section">...</div>, with a title.-
__init__(title, children=None)¶ Construct the
Section.Parameters: - title (str) – Title of this
Section. - children – List of objects which can be converted into
Elementviaas_element(). (defaultNone, no children is specified)
- title (str) – Title of this
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
-
class
mlsnippet.report.UniqueNames¶ Bases:
objectClass for obtaining unique names.
-
__init__()¶ Construct the
UniqueNames.
-
uniquify(name, obj=None)¶ Get a unique name according to the candidate name and the obj.
Parameters: - name (str) – The candidate name. Suffices
_[id]will be appended to this name, in order to obtain a unique name. - obj (any) – If not
None, the uniquified name will be memorized for this object, such that the next timeuniquify()is called, the same name will be returned instead of obtaining a new one. (defaultNone)
Returns: The uniquified name, or the memorized name for obj.
Return type: Raises: ValueError– If obj is specified, and name does not equal to the name specified in the last call.- name (str) – The candidate name. Suffices
-
-
class
mlsnippet.report.ToHtmlContext(is_ipython=False)¶ Bases:
objectThe context for
mlsnippet.report.Element.to_html().This class carries the context for
mlsnippet.report.Element.to_html(). It collects the elements along the call-stack (i.e., the elements along the path of callingElement.to_html()), and provide various utilities for producing the HTML document.-
__init__(is_ipython=False)¶ Construct the
ToHtmlContext.Parameters: is_ipython (bool) – Whether or not the elements are displayed in a Jupyter notebook? (default False)
-
get_unique_id(element, suffix=None)¶ Get a unique id for element within this context.
The unique id will be generated w.r.t. the class name of element. If element is a
NamedElement, then its name will be considered instead of its class name, provided name is notNone.Parameters: - element (mlsnippet.report.Element) – The element instance.
- suffix (str or None) – If not
None, it will be appended to the class name or name of the element, when obtaining the unique id. (defaultNone)
Returns: The unique id for element.
Return type:
-
is_ipython¶ Whether or not the elements are displayed in a Jupyter notebook?
-
push(**kwds)¶ Push an
mlsnippet.report.Elementonto the call-stack, and open a context.Parameters: element (mlsnippet.report.Element) – The element to be pushed. If it is a
mlsnippet.report.Section, then thesection_levelcounter will be increased by one within the opened context.Yields: A context where the element is on the call-stack.
Raises: TypeError– If element is notElement.RuntimeError– If aSectionis pushed, but the currentsection_levelis already>=5.
-
section_level¶ Get the opened section level.
Returns: - The section level counter, starting from 1.
- If no section has been opened, return 0.
Return type: int
-
section_title_tag()¶ Get the tag for section title at current level.
Returns: One of {“h3”, “h4”, “h5”, “h6”, “h7”}. Return type: str Raises: RuntimeError– If no section has been opened.
-
-
mlsnippet.report.demo_report()¶ Get a demonstration
Report.Returns: The demonstration report. Return type: Report
-
class
mlsnippet.report.Element¶ Bases:
objectBase class for report elements.
-
_repr_html_()¶ Render this
Elementinto HTML in a Jupyter notebook.Returns: The rendered HTML. Return type: str
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
to_html(context=None)¶ Render this
Elementinto HTML.Parameters: context (ToHtmlContext) – Context for rendering Elementinto HTML. If not specified, will create a default context.Returns: The rendered HTML. Return type: str
-
-
mlsnippet.report.as_element(obj)¶ Convert obj into
Element.It will be converted according the following rules in order:
- Return obj itself if it is a
Element. - A
HTMLwill be constructed if obj is astr. - The conversion function will be called, if the type of obj
has been designated a function via
register_as_element(). The registered conversion functions will be tried in the order of registration. The first function which can successfully convert the object is selected. - A
MagicHTMLwill be constructed if obj has a magic method __html__.
Parameters: obj – The object to be converted. Returns: An instance of Elementconverted from obj.Return type: Element Raises: TypeError– If obj cannot be converted by any of the above rules.- Return obj itself if it is a
-
mlsnippet.report.register_as_element(typ_, convert_func)¶ Register a conversion function which converts objects of particular type into
Element.Parameters: - typ – The type which this conversion function is designed for.
- convert_func – The conversion function which converts objects of
type_ into
Element.
Raises: TypeError– If typ_ is not a class.
-
class
mlsnippet.report.NamedElement(name=None)¶ Bases:
mlsnippet.report.element.ElementBase class for
Elementwith name.-
__init__(name=None)¶ Construct the
NamedElement.Parameters: name (str or None) – If specified, it will be used as the name of this element.
-
-
class
mlsnippet.report.Text(text)¶ Bases:
mlsnippet.report.element.ElementPlain text element.
-
__init__(text)¶ Construct the
Text.Parameters: text (str) – The text content. The HTML entities in text will be escaped when rendered as HTML.
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
text¶ Get the text content.
-
-
class
mlsnippet.report.HTML(content)¶ Bases:
mlsnippet.report.element.ElementHTML content element.
-
__init__(content)¶ Construct the
HTML.Parameters: content (str) – The HTML content. The HTML entities will be output as-is when rendered as HTML.
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
content¶ Get the HTML content.
-
-
class
mlsnippet.report.MagicHTML(obj, cacheable=False)¶ Bases:
mlsnippet.report.element.ElementWrapping object with __html__ method into
Element.-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
cacheable¶ Whether or not the output of __html__ is cacheable?
-
obj¶ Get the wrapped object.
-
-
class
mlsnippet.report.InlineMath(mathjax)¶ Bases:
mlsnippet.report.element.MathEquationInline math equation.
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
-
class
mlsnippet.report.BlockMath(mathjax)¶ Bases:
mlsnippet.report.element.MathEquationBlock math equation.
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
-
class
mlsnippet.report.Resource(data, mime_type, title=None)¶ Bases:
mlsnippet.report.element.ElementBase class for resource elements.
Resources are particular types of binary data, for example, images and CSV files. This class provides utilities to embed such resources in a report, as HTML content.
-
data¶ Get the binary data of the resource.
-
get_uri(context)¶ Get the URI for embedding the resource into HTML.
Parameters: context (ToHtmlContext) – The context for rendering HTML. Returns: The URI string. Return type: str
-
classmethod
guess_extension(mime_type)¶ Get the preferred file extension for specified mime-type.
Return value is a string giving a filename extension, including the leading dot “.”.
Parameters: mime_type (str) – The mime-type. Returns: - The extension, or
Noneif mime_type - is not registered.
Return type: str or None - The extension, or
-
classmethod
guess_mime_type(extension)¶ Get the preferred mime-type for specified file extension.
Parameters: extension (str) – The filename extension, including the leading dot “.”. Returns: - The mime-type, or
Noneif extension - is not registered.
Return type: str or None - The mime-type, or
-
mime_type¶ Get the mime-type of the resource.
-
-
class
mlsnippet.report.Attachment(data, mime_type, title=None)¶ Bases:
mlsnippet.report.element.ResourceSubclass of
Resource, rendered as a download link.-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
-
class
mlsnippet.report.Image(data, mime_type, title=None, inline=False)¶ Bases:
mlsnippet.report.element.ResourceSubclass of
Resource, rendered as an image.-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
classmethod
from_figure(figure, dpi=None, tight_bbox=True, title=None, inline=False)¶ Construct a
Imagefrommatplotlib.pyplot.Figure.Parameters: - figure (matplotlib.pyplot.Figure) – The matplotlib figure.
- dpi (int or None) – DPI for rendering the figure.
(default
None, use the DPI of figure) - tight_bbox (bool) – Whether to have a tight bounding box?
(default
True) - title (str or None) – Title of the image. (default
None) - inline (bool) – Whether or not to render this image as
inline HTML element? (default
False)
Returns: - The
Imageobject, carrying a PNG image of rendered figure.
Return type: Raises: TypeError– If figure is not amatplotlib.pyplot.Figure.
-
classmethod
from_image(image, format='PNG', title=None, inline=False)¶ Construct a
ImagefromPIL.Image.Image.Parameters: Returns: The
Imageobject.Return type: Raises: RuntimeError– If PIL is not installed.TypeError– If image is not aPIL.Image.Image.ValueError– If format is not supported.
-
inline¶ Get whether or not to render this image as inline HTML element.
-
-
class
mlsnippet.report.DataFrameTable(data_frame)¶ Bases:
mlsnippet.report.element.ElementElement to render
pandas.DataFrameas an HTML table.-
__init__(data_frame)¶ Construct the
DataFrameTable.Parameters: data_frame (pandas.DataFrame) – The data frame object. Raises: TypeError– If data_frame is not apandas.DataFrame.
-
_to_html(context)¶ Actual method to render this
Elementinto HTML. Subclasses ofElementshould override this method.This method is designed to be called by
to_html(), with context ensured to be created, and self pushed to the call-stack of context. Note that if an element needs to render another element within this method, it should call :meth:`to_html of that element, with context passed to it.Parameters: context (ToHtmlContext) – The context for rendering. Returns: The rendered HTML. Return type: str
-
data_frame¶ Get the
DataFrameobject.
-
-
class
mlsnippet.report.Library¶ Bases:
objectBase class for HTML libraries.
An HTML library carries stylesheets and scripts, which will be output at the end of HEAD (stylesheets) and BODY (scripts).
-
get_scripts()¶ Get the scripts of this library.
Derived classes should override this method to provide the source code of the scripts, including the “<script>” tag. For example:
class JQuery(Library): def get_scripts(self): return '<script src="https://cdnjs.cloudflare.com/ajax/' 'libs/jquery/3.2.1/jquery.min.js"></script>'
The scripts will be output at the end of BODY tag in the rendered HTML.
Returns: - Source code of the scripts, or
None - if no script is provided.
Return type: str or None - Source code of the scripts, or
-
get_styles()¶ Get the stylesheets of this library.
Derived classes should override this method to provide the source code of the stylesheets, including the “<style>” or “<link>” tag. For example:
class Bootstrap(Library): def get_styles(self): return '<link rel="stylesheet" ' 'href="https://cdnjs.cloudflare.com/ajax/libs/' 'twitter-bootstrap/3.3.7/css/bootstrap.min.css" />'
The stylesheets will be output at the end of HEAD tag in the rendered HTML.
Returns: - Source code of the stylesheets, or
None - if no stylesheet is provided.
Return type: str or None - Source code of the stylesheets, or
-
-
class
mlsnippet.report.SingletonLibrary¶ Bases:
mlsnippet.report.library.LibraryBase for singleton
Libraryclasses.Subclasses of
SingletonLibrarywould become singleton. For example,JQueryis a subclass ofSingletonLibrary, such that the following assertion holds:assert(JQuery() is JQuery())
-
static
__new__(S, ...) → a new object with type S, a subtype of T¶
-
static
-
class
mlsnippet.report.JQuery¶ Bases:
mlsnippet.report.library.SingletonLibraryJQuery library, loaded via
requirejs(["jquery"]).-
get_scripts()¶ Get the scripts of this library.
Derived classes should override this method to provide the source code of the scripts, including the “<script>” tag. For example:
class JQuery(Library): def get_scripts(self): return '<script src="https://cdnjs.cloudflare.com/ajax/' 'libs/jquery/3.2.1/jquery.min.js"></script>'
The scripts will be output at the end of BODY tag in the rendered HTML.
Returns: - Source code of the scripts, or
None - if no script is provided.
Return type: str or None - Source code of the scripts, or
-
-
class
mlsnippet.report.MathJax¶ Bases:
mlsnippet.report.library.SingletonLibraryMathJax library, loaded via
requirejs(["mathjax"]).-
get_scripts()¶ Get the scripts of this library.
Derived classes should override this method to provide the source code of the scripts, including the “<script>” tag. For example:
class JQuery(Library): def get_scripts(self): return '<script src="https://cdnjs.cloudflare.com/ajax/' 'libs/jquery/3.2.1/jquery.min.js"></script>'
The scripts will be output at the end of BODY tag in the rendered HTML.
Returns: - Source code of the scripts, or
None - if no script is provided.
Return type: str or None - Source code of the scripts, or
-
-
class
mlsnippet.report.Report(title, children, description=None)¶ Bases:
objectClass for building report.
-
body¶ Get the body
Container, which contains allElementobjects converted from children.Returns: The body container. Return type: Container
-
description¶ Get the description of the report.
Returns: The description of the report. Return type: str or None
-
save(path_or_file)¶ Save the rendered HTML as file.
Parameters: path_or_file – The file path, or a file-like object to write.
-
title¶ Get the title report.
-
-
mlsnippet.report.escape(s)¶ Escape HTML entities in s.
-
mlsnippet.report.camel_to_underscore(name)¶ Convert a camel-case name to underscore.