MkBinaryImage¶
Image carrying the data by itself.¶
Description
This node basically is a regular image link, but carries the image data by itself. The data will get written to the “virtual” folder at the end of the process. It can hold either str or bytes as data.
Example: From data
Jinja
{% set svg = '<svg width="90pt" height="90pt" version="1.1" viewBox="0 0 15.875 10.583" xmlns="http://www.w3.org/2000/svg">
<g fill="none" stroke="#F00" stroke-width=".3">
<path d="m6.1295 3.6601 3.2632 3.2632z"/>
<path d="m9.3927 3.6601-3.2632 3.2632z"/>
</g>
</svg>' %}
{{ svg | MkBinaryImage(path="some_image.svg", caption="A simple cross") }}
Python
MkBinaryImage(
'<svg width="90pt" height="90pt" version="1.1" viewBox="0 0 15.875 10.583" xmlns="http://www.w3.org/2000/svg">\n <g fill="none" stroke="#F00" stroke-width=".3">\n <path d="m6.1295 3.6601 3.2632 3.2632z"/>\n <path d="m9.3927 3.6601-3.2632 3.2632z"/>\n </g>\n </svg>',
"some_image.svg",
caption="A simple cross",
)
<picture>
<img alt="A simple cross" src="some_image.svg">
</picture>
<picture>
<img alt="A simple cross" src="some_image.svg">
</picture>
<picture>
<img alt="A simple cross" src="some_image.svg">
</picture>
Example: From icon
Jinja
{{ mk.MkBinaryImage.for_icon("file-image", width=200) }}
Python
MkBinaryImage(
'<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M13 9h5.5L13 3.5zM6 2h8l6 6v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V4c0-1.11.89-2 2-2m0 18h12v-8l-4 4l-2-2zM8 9a2 2 0 0 0-2 2a2 2 0 0 0 2 2a2 2 0 0 0 2-2a2 2 0 0 0-2-2"/></svg>',
"file_image.svg",
width=200,
)
<picture>
<img alt="" src="file_image.svg">
</picture>
<picture>
<img alt="" src="file_image.svg">
</picture>
<picture>
<img alt="" src="file_image.svg">
</picture>
Name |
Children |
Inherits |
|---|---|---|
MkImage |
graph TD
1150703457["mkbinaryimage.MkBinaryImage"]
4050704754["mkimage.MkImage"]
1359323199["mknode.MkNode"]
2751680898["builtins.object"]
4050704754 --> 1150703457
1359323199 --> 4050704754
2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkbinaryimage/metadata.toml¶
[metadata]
icon = "mdi:file-image"
name = "MkBinaryImage"
group = "image"
[examples.from_data]
title = "From data"
jinja = """
{% set svg = '<svg width="90pt" height="90pt" version="1.1" viewBox="0 0 15.875 10.583" xmlns="http://www.w3.org/2000/svg">
<g fill="none" stroke="#F00" stroke-width=".3">
<path d="m6.1295 3.6601 3.2632 3.2632z"/>
<path d="m9.3927 3.6601-3.2632 3.2632z"/>
</g>
</svg>' %}
{{ svg | MkBinaryImage(path="some_image.svg", caption="A simple cross") }}
"""
[examples.from_icon]
title = "From icon"
jinja = """
{{ mk.MkBinaryImage.for_icon("file-image", width=200) }}
"""
mknodes.basenodes.mkbinaryimage.MkBinaryImage¶
19class MkBinaryImage(mkimage.MkImage):
20 """Image carrying the data by itself.
21
22 This node basically is a regular image link, but carries the image data by itself.
23 The data will get written to the "virtual" folder at the end of the process.
24 It can hold either str or bytes as data.
25 """
26
27 ICON = "material/file-image"
28
29 def __init__(
30 self,
31 data: bytes | str,
32 path: str,
33 *,
34 target: linkprovider.LinkableType | None = None,
35 caption: str = "",
36 title: str = "",
37 align: Literal["left", "right"] | None = None,
38 width: int | None = None,
39 **kwargs: Any,
40 ) -> None:
41 """Constructor.
42
43 Args:
44 data: Image data
45 path: path for the image (including extension)
46 target: Optional URL or node the image should link to
47 caption: Image caption
48 title: Image title
49 align: Image alignment
50 width: Image width in pixels
51 kwargs: Keyword arguments passed to parent
52 """
53 super().__init__(
54 path=path,
55 target=target,
56 caption=caption,
57 title=title,
58 align=align,
59 width=width,
60 **kwargs,
61 )
62 self.data = data
63
64 @property
65 def files(self) -> dict[str, str | bytes]:
66 path = "/".join(self.resolved_parts) + "/" + self.path
67 return {path: self.data} | self._files
68
69 @classmethod
70 def for_icon(cls, icon: str, **kwargs: Any) -> Self:
71 """Return a MkBinaryImage with data for given icon.
72
73 Args:
74 icon: Icon to get a MkBinaryImage for (example: material/file-image)
75 kwargs: Keyword arguments passed to constructor
76 """
77 content = iconcache.get_icon_svg(icon)
78 path = f"{textfilters.slugify(icon)}.svg"
79 return cls(data=content, path=path, **kwargs)
80
81 @classmethod
82 def for_file(
83 cls,
84 path: str | os.PathLike[str],
85 storage_options: dict[str, Any] | None = None,
86 **kwargs: Any,
87 ) -> Self:
88 """Return a MkBinaryImage with data for given icon.
89
90 Args:
91 path: Path to an image (also takes fsspec protocol URLs)
92 storage_options: Options for fsspec backend
93 kwargs: Keyword arguments passed to constructor
94 """
95 data = pathhelpers.load_bytes(path, **(storage_options or {}))
96 return cls(data=data, path=upath.UPath(os.fspath(path)).name, **kwargs)
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes¶
93def create_section_for_nodes(
94 nav: mk.MkNav,
95 klasses: Sequence[type[mk.MkNode]],
96) -> mk.MkTable:
97 table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
98 for kls in klasses:
99 tpl = "classpage_custom.jinja"
100 page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
101 nav += page
102 if kls.STATUS:
103 page.metadata.status = kls.STATUS
104 elif kls.JS_FILES:
105 page.metadata.status = "js"
106 elif kls.CSS:
107 page.metadata.status = "css"
108 page.created_by = create_section_for_nodes
109 link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
110 extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
111 table.add_row((link, kls.__doc__, extensions))
112 return table
Resources
Resources(css=[],
markdown_extensions={'admonition': {},
'pymdownx.details': {},
'pymdownx.highlight': {'anchor_linenums': True,
'line_spans': '__span',
'pygments_lang_class': True},
'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
'format': <function fence_code_format at 0x7ff77e88b060>,
'name': 'mermaid'}]},
'pymdownx.tabbed': {},
'tables': {}},
plugins=[Plugin('mkdocstrings')],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
source_function: create_section_for_nodes
source_line_no: 93
hide:
- toc
icon: material/file-image
title: MkBinaryImage