Skip to content

Code blocks

Code
docs/staging/code_blocks.py
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
p.P(
    """
    Here we use nested trackers. The outer tracker uses a 
    'top-c' layout, so the code is a collapsed admonition at the top
    right above this paragraph.

    The other blocks below are nested inside this one.
    """
)

with p.H2("Tracking code (tabbed)"):
    p.Raw(
        """
        One of the features of mkreports is that we can include
        code right from the script into the output. This is done by 
        starting a context manager and all code in the 
        scope of the context manager will be recorded.
        """
    )

    x = 3
    a = fib(x)

    p.P(f"fib({x}) = {a}")

    p.P(
        """
        By default, the **tabbed** version will be used, but there
        are also the options

        - **top-c**: Collapsed code block at the top
        - **top-o**: Open code block at the top
        - **bottom-c**: Collapsed code block at the bottom
        - **bottom-o**: Open code block at the bottom

        The default style can be set when creating the page using the 
        `code_layout` parameter, but can also be set one at a time later
        using the `ctx` method on a page.
            """
    )

with p.H2("Tracking code (top-c)").ctx(layout="top-c"):
    p.P("The code as a collapsed admonition before the output.")
    x = 4
    a = fib(x)

    p.P(f"fib({x}) = {a}")

with p.H2("Tracking code (top-o)").ctx(layout="top-o"):
    p.P("The code as an open code block before the output.")
    x = 5
    a = fib(x)

    p.P(f"fib({x}) = {a}")

with p.H2("Tracking code (bottom-c)").ctx(layout="bottom-c"):
    p.P("The code as a collapsed admoniton after the output.")
    x = 6
    a = fib(x)

    p.Raw(f"fib({x}) = {a}")

with p.H2("Tracking code (bottom-o)").ctx(layout="bottom-o"):
    p.P("The code as an open code block after the output.")
    x = 7
    a = fib(x)

    p.P(f"fib({x}) = {a}")

with p.H2("Adding code files"):
    p.P(
        """
        In addtion we can also add entire code files, for 
        example at the end of a page the file or files that 
        created a page. This can be wrapped into a collapsed
        admonition so that the file is hidden.
        """
    )

    p.CollapsedCodeFile(__file__)

p.P(
    """
    And at the end will add another copy of the code-file, 
    but automatically when ending the page context manager.
    """
)

Here we use nested trackers. The outer tracker uses a 'top-c' layout, so the code is a collapsed admonition at the top right above this paragraph.

The other blocks below are nested inside this one.

Tracking code (tabbed)

One of the features of mkreports is that we can include code right from the script into the output. This is done by starting a context manager and all code in the scope of the context manager will be recorded.

fib(3) = 2

By default, the tabbed version will be used, but there are also the options

  • top-c: Collapsed code block at the top
  • top-o: Open code block at the top
  • bottom-c: Collapsed code block at the bottom
  • bottom-o: Open code block at the bottom

The default style can be set when creating the page using the code_layout parameter, but can also be set one at a time later using the ctx method on a page.

docs/staging/code_blocks.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
p.Raw(
    """
    One of the features of mkreports is that we can include
    code right from the script into the output. This is done by 
    starting a context manager and all code in the 
    scope of the context manager will be recorded.
    """
)

x = 3
a = fib(x)

p.P(f"fib({x}) = {a}")

p.P(
    """
    By default, the **tabbed** version will be used, but there
    are also the options

    - **top-c**: Collapsed code block at the top
    - **top-o**: Open code block at the top
    - **bottom-c**: Collapsed code block at the bottom
    - **bottom-o**: Open code block at the bottom

    The default style can be set when creating the page using the 
    `code_layout` parameter, but can also be set one at a time later
    using the `ctx` method on a page.
        """
)

Tracking code (top-c)

Code
docs/staging/code_blocks.py
61
62
63
64
65
p.P("The code as a collapsed admonition before the output.")
x = 4
a = fib(x)

p.P(f"fib({x}) = {a}")

The code as a collapsed admonition before the output.

fib(4) = 3


Tracking code (top-o)

docs/staging/code_blocks.py
68
69
70
71
72
p.P("The code as an open code block before the output.")
x = 5
a = fib(x)

p.P(f"fib({x}) = {a}")

The code as an open code block before the output.

fib(5) = 5


Tracking code (bottom-c)

The code as a collapsed admoniton after the output.

fib(6) = 8

Code
docs/staging/code_blocks.py
75
76
77
78
79
p.P("The code as a collapsed admoniton after the output.")
x = 6
a = fib(x)

p.Raw(f"fib({x}) = {a}")

Tracking code (bottom-o)

The code as an open code block after the output.

fib(7) = 13

docs/staging/code_blocks.py
82
83
84
85
86
p.P("The code as an open code block after the output.")
x = 7
a = fib(x)

p.P(f"fib({x}) = {a}")

Adding code files

In addtion we can also add entire code files, for example at the end of a page the file or files that created a page. This can be wrapped into a collapsed admonition so that the file is hidden.

Code
docs/staging/code_blocks.py
from mkreports import Report


def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)


def use_code_blocks(report: Report) -> None:
    """
    Show different ways on how code blacks can be included.
    """
    p = report.page("usage/code_blocks.md", truncate=True)
    with p.H1("Code blocks").ctx("top-c"):
        p.P(
            """
            Here we use nested trackers. The outer tracker uses a 
            'top-c' layout, so the code is a collapsed admonition at the top
            right above this paragraph.

            The other blocks below are nested inside this one.
            """
        )

        with p.H2("Tracking code (tabbed)"):
            p.Raw(
                """
                One of the features of mkreports is that we can include
                code right from the script into the output. This is done by 
                starting a context manager and all code in the 
                scope of the context manager will be recorded.
                """
            )

            x = 3
            a = fib(x)

            p.P(f"fib({x}) = {a}")

            p.P(
                """
                By default, the **tabbed** version will be used, but there
                are also the options

                - **top-c**: Collapsed code block at the top
                - **top-o**: Open code block at the top
                - **bottom-c**: Collapsed code block at the bottom
                - **bottom-o**: Open code block at the bottom

                The default style can be set when creating the page using the 
                `code_layout` parameter, but can also be set one at a time later
                using the `ctx` method on a page.
                    """
            )

        with p.H2("Tracking code (top-c)").ctx(layout="top-c"):
            p.P("The code as a collapsed admonition before the output.")
            x = 4
            a = fib(x)

            p.P(f"fib({x}) = {a}")

        with p.H2("Tracking code (top-o)").ctx(layout="top-o"):
            p.P("The code as an open code block before the output.")
            x = 5
            a = fib(x)

            p.P(f"fib({x}) = {a}")

        with p.H2("Tracking code (bottom-c)").ctx(layout="bottom-c"):
            p.P("The code as a collapsed admoniton after the output.")
            x = 6
            a = fib(x)

            p.Raw(f"fib({x}) = {a}")

        with p.H2("Tracking code (bottom-o)").ctx(layout="bottom-o"):
            p.P("The code as an open code block after the output.")
            x = 7
            a = fib(x)

            p.P(f"fib({x}) = {a}")

        with p.H2("Adding code files"):
            p.P(
                """
                In addtion we can also add entire code files, for 
                example at the end of a page the file or files that 
                created a page. This can be wrapped into a collapsed
                admonition so that the file is hidden.
                """
            )

            p.CollapsedCodeFile(__file__)

        p.P(
            """
            And at the end will add another copy of the code-file, 
            but automatically when ending the page context manager.
            """
        )
docs/staging/code_blocks.py
89
90
91
92
93
94
95
96
97
98
p.P(
    """
    In addtion we can also add entire code files, for 
    example at the end of a page the file or files that 
    created a page. This can be wrapped into a collapsed
    admonition so that the file is hidden.
    """
)

p.CollapsedCodeFile(__file__)

And at the end will add another copy of the code-file, but automatically when ending the page context manager.