Skip to content

MkHtmlTable

:fa-brands-github: Show source on GitHub

Class representing a html table.

Description

Compared to MkTable, this will end up with a more verbose output, but it can contain more complex Markdown in cells.

Example: Regular

Jinja

{% set code_col = [_mk.MkCode("print('hello world')
sys.exit()"), _mk.MkCode("print('hello world')
sys.exit()")] %}
{% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %}
{% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %}
{% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %}
{{ data | MkHtmlTable }}

Python

MkHtmlTable(
    data={
        "Admonitions": [MkAdmonition(None), MkAdmonition(None)],
        "Code": [MkCode(None), MkCode(None)],
        "Tabs": [MkTabbed(tabs=[...]), MkTabbed(tabs=[...])],
    }
)

Code Admonitions Tabs
print('hello world')
sys.exit()

Info

Admonition inside cell

Tab a

Tab b

print('hello world')
sys.exit()

Info

Admonition inside cell

Tab a

Tab b

<table markdown="1">
<tr>
<td>
Code
</td>
<td>
Admonitions
</td>
<td>
Tabs
</td>
</tr>
<tr>
<td>
```` {.python }
print('hello world')
sys.exit()
````
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
<tr>
<td>
```` {.python }
print('hello world')
sys.exit()
````
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
</table>
<table>
<tr>
<td>
Code
</td>
<td>
Admonitions
</td>
<td>
Tabs
</td>
</tr>
<tr>
<td>
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="nb">print</span><span class="p">(</span><span class="s1">'hello world'</span><span class="p">)</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
</span></code></pre></div>
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
<tr>
<td>
<div class="language-python highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="nb">print</span><span class="p">(</span><span class="s1">'hello world'</span><span class="p">)</span>
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
</span></code></pre></div>
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
</table>
<table>
<tr><th>Code</th><th>Admonitions</th><th>Tabs</th></tr>
<tr>
<td>

````python
print('hello world')
sys.exit()
````

</td>
<td>

> [!NOTE]
> Admonition inside cell

</td>
<td>

<details open>
<summary>A</summary>

Tab a

</details>

<details>
<summary>B</summary>

Tab b

</details>

</td>
</tr>
<tr>
<td>

````python
print('hello world')
sys.exit()
````

</td>
<td>

> [!NOTE]
> Admonition inside cell

</td>
<td>

<details open>
<summary>A</summary>

Tab a

</details>

<details>
<summary>B</summary>

Tab b

</details>

</td>
</tr>
</table>
MkHtmlTable
├── MkCode(None)
│   ╰── MkText("print('hello world')\nsys.exit()")
├── MkCode(None)
│   ╰── MkText("print('hello world')\nsys.exit()")
├── MkAdmonition(None)
│   ╰── MkText('Admonition inside cell')
├── MkAdmonition(None)
│   ╰── MkText('Admonition inside cell')
├── MkTabbed(tabs=[...])
│   ├── MkTab(title='A', content='Tab a', new=True)
│   │   ╰── MkText('Tab a')
│   ╰── MkTab(title='B', content='Tab b')
│       ╰── MkText('Tab b')
├── MkTabbed(tabs=[...])
│   ├── MkTab(title='A', content='Tab a', new=True)
│   │   ╰── MkText('Tab a')
│   ╰── MkTab(title='B', content='Tab b')
│       ╰── MkText('Tab b')

Bases: MkBaseTable

Name Children Inherits
MkBaseTable
mknodes.basenodes.mkbasetable
Base Class for MkTables. Only deals with managing the data.
graph TD
  530410512["mkhtmltable.MkHtmlTable"]
  3678415322["mkbasetable.MkBaseTable"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  2751680898["builtins.object"]
  3678415322 --> 530410512
  835538629 --> 3678415322
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkhtmltable/metadata.toml
[metadata]
icon = "octicon:table-24"
status = "new"
name = "MkHtmlTable"

[examples.regular]
title = "Regular"
jinja = """
{% set code_col = [_mk.MkCode("print('hello world')\nsys.exit()"), _mk.MkCode("print('hello world')\nsys.exit()")] %}
{% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %}
{% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %}
{% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %}
{{ data | MkHtmlTable }}
"""

[output.markdown]
template = """
{% set table = node.data %}
<table markdown="1">
<tr>
{% for header in table.keys() %}
<td>
{{ header }}
</td>
{% endfor %}
</tr>
{% for row in node.iter_rows() %}
<tr>
{% for item in row %}
<td>
{{ item }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
"""
mknodes.basenodes.mkhtmltable.MkHtmlTable
class MkHtmlTable(mkbasetable.MkBaseTable):
    """Class representing a html table.

    Compared to MkTable, this will end up with a more verbose output,
    but it can contain more complex Markdown in cells.
    """

    STATUS = "new"

    async def get_element(self) -> xml.Table | None:
        table_data = self.data  # property
        if not any(table_data[k] for k in table_data):
            return None
        root = xml.Table(markdown=True)
        data = [[str(k) for k in row] for row in self.iter_rows()]
        headers = list(table_data.keys())
        data.insert(0, headers)
        for items in data:
            tr = xml.Tr(parent=root)
            for item in items:
                td = xml.Td(parent=tr)
                td.text = "\n" + item + "\n"
        return root

    async def to_md_unprocessed(self) -> str:
        root = await self.get_element()
        return root.to_string(space="") if root is not None else ""
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes
def create_section_for_nodes(
    nav: mk.MkNav,
    klasses: Sequence[type[mk.MkNode]],
) -> mk.MkTable:
    table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
    for kls in klasses:
        tpl = "classpage_custom.jinja"
        page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
        nav += page
        if kls.STATUS:
            page.metadata.status = kls.STATUS
        elif kls.JS_FILES:
            page.metadata.status = "js"
        elif kls.CSS:
            page.metadata.status = "css"
        page.created_by = create_section_for_nodes
        link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
        extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
        table.add_row((link, kls.__doc__, extensions))
    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 0x703933662e50>,
                                                                           '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: 95
hide:
- toc
icon: octicons/table-24
status: new
title: MkHtmlTable