API Reference¶
This section contains the complete API reference for CMX.
Core Module¶
The main module provides the global doc object and data utilities.
Backends¶
CMX supports multiple output backends for different use cases.
Markdown Backend¶
The primary backend for generating markdown documents.
The CommonMark document – the object exported as cmx.doc.
A CommonMark is an Article (so it inherits
the component factories: table, image, figure, row, video,
savefig) plus the live-document machinery: the @ / | / call operators
for adding markdown, with doc: source capture, and flush to write out.
- class cmx.backends.markdown.CommonMark(filename=None, overwrite=True, root=None, prefix=None, logger=None)[source]¶
Bases:
Article- counter = 0¶
- property hide¶
with doc.hide:runs its body but does not capture the source as a code block – handy for boilerplate you want to execute but not show.
- property skip¶
with doc.skip:skips execution of its body entirely.Implemented via frame tracing (see
SkipContextManager); this can interfere with debuggers such as PyCharm’s pydev.
- property filename¶
- md(*snippets, dedent=True, **kwargs)¶
Add markdown text:
doc("# Title").
- class cmx.backends.markdown.Github(filename=None, overwrite=True, root=None, prefix=None, logger=None)[source]¶
Bases:
CommonMarkUses tables for the layout.
- class cmx.backends.markdown.Gist(filename=None, overwrite=True, root=None, prefix=None, logger=None)[source]¶
Bases:
CommonMarkSaves everything inside a folder.
Components¶
Base components used across all backends.
Render components for the CMX markdown backend.
Each component knows how to render itself as Markdown (_md) and HTML
(_html). Components form a tree: an Article holds children, some of
which (Table, FigureRow, Row) hold children of their own.
This module is dependency-inverted the simple way: there is no class registry or
__getattribute__ proxy. Parents create their children by calling the
component classes directly, and the optional file-writing logger is threaded
down explicitly. Anything that needs heavy third-party libraries (numpy, PIL,
pyyaml) imports them lazily so import cmx stays cheap.
- class cmx.backends.components.Component(tag=None, children=None, logger=None, **kwargs)[source]¶
Bases:
objectBase node in the document tree.
A component renders to Markdown via
_mdand to HTML via_html. The default implementation emits a generic<tag>...children...</tag>; most subclasses override one or both properties.- data = None¶
- class_name = None¶
- tag = None¶
- class cmx.backends.components.Container(tag=None, children=None, logger=None, **kwargs)[source]¶
Bases:
ComponentA holder that is rendered by its parent, never on its own.
- class cmx.backends.components.Div(tag=None, children=None, logger=None, **kwargs)[source]¶
Bases:
Component- tag = 'div'¶
- class cmx.backends.components.Span(*args, sep=' ', dedent=None, **kwargs)[source]¶
Bases:
Component- tag = 'span'¶
- class cmx.backends.components.Bold(*args, sep=' ', dedent=None, **kwargs)[source]¶
Bases:
Span- tag = 'b'¶
- class cmx.backends.components.Link(href='', text='', **kwargs)[source]¶
Bases:
Component- tag = 'span'¶
- class cmx.backends.components.Img(src=None, zoom=None, alt=None, **kwargs)[source]¶
Bases:
Component- tag = 'img'¶
- zoom = None¶
- class cmx.backends.components.Image(image=None, src=None, logger=None, normalize=False, **kwargs)[source]¶
Bases:
ImgAn image that can reference a file, save data to a file, or inline base64.
srconly -> reference the file path.src+image+ alogger-> write the file, then reference it.imageonly -> inline the data as a base64data:URI.
- data = None¶
- property base64¶
- class cmx.backends.components.Video(src=None, width=320, height=240, controls=True, **kwargs)[source]¶
Bases:
Component
- cmx.backends.components.video(frames=None, *, src, logger=None, **kwargs)[source]¶
Save video frames (if given) and return the matching component.
.gifsources render as anImage; everything else as aVideo.
- class cmx.backends.components.Figure(image=None, src=None, title=None, caption=None, logger=None, **kwargs)[source]¶
Bases:
Component- tag = 'div'¶
- class cmx.backends.components.Savefig(key, caption=None, width=None, height=None, zoom=None, logger=None, **kwargs)[source]¶
Bases:
FigureSave the current matplotlib figure to
keyand reference it inline.keyis the destination path (an optional?...query suffix is stripped before writing, so it can double as a cache-buster in thesrc).title/caption/width/height/zoomcontrol how the figure is displayed; every other keyword (dpi,bbox_inches,transparent,facecolor,format…) is forwarded untouched to matplotlib’ssavefigand is not leaked into the<img>tag.
- class cmx.backends.components.Row(wrap=False, styles=None, logger=None, **kwargs)[source]¶
Bases:
Component- styles = {'display': 'flex', 'flex_direction': 'row', 'item_align': 'center'}¶
- class cmx.backends.components.FigureRow(header=None, n_cols=None, logger=None, **kwargs)[source]¶
Bases:
ContainerA horizontal band of figures rendered by its parent
Tableas up to three Markdown rows: titles, images, and captions (empty bands are dropped).- property rows¶
- class cmx.backends.components.Table(table=None, show_index=None, format='github', sep=',*', logger=None, **kwargs)[source]¶
Bases:
ComponentA Markdown/HTML table.
Pass tabular
data(a DataFrame, CSV string, or anything pandas accepts) for a data table, or build a figure grid withfigure_row().- data = None¶
- class cmx.backends.components.Html(tag=None, children=None, logger=None, **kwargs)[source]¶
Bases:
_Factory,Component- tag = 'body'¶
HTML Backend¶
HTML output support.
LaTeX Backend¶
LaTeX output support for academic papers and publications.
Utilities¶
Utils Module¶
Helper functions and utilities.
- class cmx.utils.F(fn=None)[source]¶
Bases:
objectFunctional notation wrapper - allows using @ operator for function composition
Usage: - F @ function - wraps a function - (F @ function)(args) - calls the wrapped function
- class cmx.utils.SimpleLogger(root=None, prefix=None)[source]¶
Bases:
objectA simple logger replacement for ml-logger functionality used in cmx.
This provides basic file I/O operations for saving images, videos, and text that were previously handled by ml-logger.
- save_image(image, filename, normalize=False)[source]¶
Save an image to disk.
- Args:
image: Image array (numpy array or PIL Image) filename: Path where to save the image normalize: Whether to normalize the image values
- save_video(frames, filename)[source]¶
Save video frames to a file.
- Args:
frames: List of image frames (numpy arrays) filename: Path where to save the video
- savefig(filename, **kwargs)[source]¶
Save the current matplotlib figure.
- Args:
filename: Path where to save the figure kwargs: Additional arguments passed to matplotlib’s savefig
- log_text(text, filename, overwrite=False)[source]¶
Log text to a file.
- Args:
text: Text content to write filename: Path where to save the text overwrite: If True, overwrite the file; if False, append
- load_json(filename)[source]¶
Load a JSON file.
- Args:
filename: Path to the JSON file
- Returns:
Parsed JSON data
Data Module¶
Data processing utilities.
Context Managers¶
Advanced context management utilities.
Server¶
Optional server components for live document serving.
- async cmx.server.echo(request, ws)¶
- Parameters:
request (sanic.Request)
ws (sanic.Websocket)
- async cmx.server.log(request, ws)¶
- Parameters:
request (sanic.Request)
ws (sanic.Websocket)