MkIcon

github Show source on GitHub

Pyconify-based Icon.

Example: With caption

Jinja

{{ "mdi:file" | MkIcon }}

Python

MkIcon('mdi:file')
file
<img src="https://api.iconify.design/mdi/file.svg?height=24" alt="file">
<img src="https://api.iconify.design/mdi/file.svg?height=24" alt="file">
<img src="https://api.iconify.design/mdi/file.svg?height=24" alt="file">

Example: Colored

Jinja

{{ "mdi:file" | MkIcon(color="red") }}

Python

MkIcon('mdi:file', color='red')
file
<img src="https://api.iconify.design/mdi/file.svg?height=24&color=red" alt="file">
<img src="https://api.iconify.design/mdi/file.svg?height=24&color=red" alt="file">
<img src="https://api.iconify.design/mdi/file.svg?height=24&color=red" alt="file">

Example: Rotated

Jinja

{{ "mdi:file" | MkIcon(rotate=90) }}

Python

MkIcon('mdi:file', rotate=90)
file
<img src="https://api.iconify.design/mdi/file.svg?height=24" alt="file">
<img src="https://api.iconify.design/mdi/file.svg?height=24" alt="file">
<img src="https://api.iconify.design/mdi/file.svg?height=24" alt="file">

Example: Sized

Jinja

{{ "noto:wrench" | MkIcon(height=100, width=100) }}

Python

MkIcon('noto:wrench', height=100, width=100)
wrench
<img src="https://api.iconify.design/noto/wrench.svg?height=100" alt="wrench">
<img src="https://api.iconify.design/noto/wrench.svg?height=100" alt="wrench">
<img src="https://api.iconify.design/noto/wrench.svg?height=100" alt="wrench">

class MkIcon(mknode.MkNode)

Name

Children

Inherits

MkNode
mknodes.basenodes.mknode
Base class for everything which can be expressed as Markup.

graph TD
  1574311463["mkicon.MkIcon"]
  1359323199["mknode.MkNode"]
  2751680898["builtins.object"]
  1359323199 --> 1574311463
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkicon/metadata.toml
[metadata]
icon = "mdi:image"
status = "new"
name = "MkIcon"
group = "image"

[examples.with_caption]
title = "With caption"
jinja = """
{{ "mdi:file" | MkIcon }}
"""

[examples.colored]
title = "Colored"
jinja = """
{{ "mdi:file" | MkIcon(color="red") }}
"""

[examples.rotated]
title = "Rotated"
jinja = """
{{ "mdi:file" | MkIcon(rotate=90) }}
"""

[examples.sized]
title = "Sized"
jinja = """
{{ "noto:wrench" | MkIcon(height=100, width=100) }}
"""

[output.html]
template = """
{{ node.icon_name | get_icon_svg(color=node.color, height=node.height, width=node.width, flip=node.flip, rotate=node.rotate, box=node.box)}}
"""
mknodes.basenodes.mkicon.MkIcon
15class MkIcon(mknode.MkNode):
16    """Pyconify-based Icon."""
17
18    ICON = "material/image"
19    ATTR_LIST_SEPARATOR = "\n"
20    STATUS = "new"
21
22    def __init__(
23        self,
24        icon_name: str,
25        *,
26        color: str | None = None,
27        height: str | int | None = None,
28        width: str | int | None = None,
29        flip: iconfilters.Flip | None = None,
30        rotate: iconfilters.Rotation | None = None,
31        box: bool | None = None,
32        **kwargs: Any,
33    ) -> None:
34        """Constructor.
35
36        Args:
37            icon_name: Icon name
38            color: Icon color. Replaces currentColor with specific color, resulting in
39                   icon with hardcoded palette.
40            height: Icon height. If only one dimension is specified, such as height, other
41                    dimension will be automatically set to match it.
42            width: Icon width. If only one dimension is specified, such as height, other
43                   dimension will be automatically set to match it.
44            flip: Flip icon.
45            rotate: Rotate icon. If an int is provided, it is assumed to be in degrees.
46            box: Adds an empty rectangle to SVG that matches the icon's viewBox.
47                 It is needed when importing SVG to various UI design tools that ignore
48                 viewBox. Those tools, such as Sketch, create layer groups that
49                 automatically resize to fit content. Icons usually have empty pixels
50                 around icon, so such software crops those empty pixels and icon's
51                 group ends up being smaller than actual icon, making it harder to
52                 align it in design.
53            kwargs: Keyword arguments passed to parent
54        """
55        super().__init__(**kwargs)
56        self.icon_name = icon_name
57        self.color = color
58        self.height = height
59        self.width = width
60        self.flip = flip
61        self.rotate = rotate
62        self.box = box
63
64    def _svg_params(self) -> dict[str, Any]:
65        return dict(
66            color=self.color,
67            height=self.height,
68            width=self.width,
69            flip=self.flip,
70            rotate=self.rotate,
71            box=self.box,
72        )
73
74    def get_input_requests(self) -> list[inputs.InputRequest]:
75        return [inputs.InputRequest.create("icon", icon=self.icon_name, **self._svg_params())]
76
77    @property
78    def svg(self) -> str:
79        """The icon SVG (from the inputs store, fetched now if it is not there yet)."""
80        try:
81            return self.ctx.inputs.get("icon", icon=self.icon_name, **self._svg_params())
82        except inputs.MissingInputError, inputs.InputPendingError:
83            raise
84        except Exception:  # noqa: BLE001
85            logger.warning("Could not find icon %r", self.icon_name)
86            return ""
87
88    def get_svg(self) -> str:
89        """Return the icon SVG (from the inputs store, fetched now if it is not there yet)."""
90        try:
91            return self.ctx.inputs.get("icon", icon=self.icon_name, **self._svg_params())
92        except inputs.MissingInputError, inputs.InputPendingError:
93            raise
94        except Exception:  # noqa: BLE001
95            logger.warning("Could not find icon %r", self.icon_name)
96            return ""
97
98    def to_md_unprocessed(self) -> str:
99        return self.get_svg()
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/image
status: new
title: MkIcon