File: nbexamples.py

package info (click to toggle)
nbformat 5.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,072 kB
  • sloc: python: 4,746; makefile: 167; javascript: 2
file content (150 lines) | stat: -rw-r--r-- 3,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import annotations

import os
from base64 import encodebytes

from nbformat.v4.nbbase import (
    new_code_cell,
    new_markdown_cell,
    new_notebook,
    new_output,
    new_raw_cell,
)

# some random base64-encoded *text*
png = encodebytes(os.urandom(5)).decode("ascii")
jpeg = encodebytes(os.urandom(6)).decode("ascii")

cells = []
cells.append(
    new_markdown_cell(
        source="Some NumPy Examples",
    )
)


cells.append(
    new_code_cell(
        source="import numpy",
        execution_count=1,
    )
)

cells.append(
    new_markdown_cell(
        source="Cell with attachments",
        attachments={
            "attachment1": {
                "text/plain": "\n".join(["a", "b", "c"]),
                "application/vnd.stuff+json": ["a", 1, "x"],
            }
        },
    )
)

cells.append(
    new_raw_cell(
        source="A random array",
    )
)

cells.append(
    new_markdown_cell(
        source="## My Heading",
    )
)

cells.append(
    new_code_cell(
        source="a = numpy.random.rand(100)",
        execution_count=2,
    )
)
cells.append(
    new_code_cell(
        source="a = 10\nb = 5\n",
        execution_count=3,
    )
)
cells.append(
    new_code_cell(
        source="a = 10\nb = 5",
        execution_count=4,
    )
)

cells.append(
    new_code_cell(
        source="json_outputs()",
        execution_count=12,
        outputs=[
            new_output(
                output_type="display_data",
                data={
                    "text/plain": "<json outputs>",
                    "application/json": {"key": "value", "x": 5, "lis": [1, 2, "x"]},
                    "application/vnd.listofstr+json": ["a", "b", "c"],
                    "application/vnd.numbers+json": [1, 2, 3],
                    "application/vnd.number+json": 42,
                    "application/vnd.object+json": {
                        "number": 5,
                        "array": [1, 2],
                        "str": "x",
                    },
                    "application/vnd.string+json": "ok",
                },
            )
        ],
    )
)

cells.append(
    new_code_cell(
        source='print "ünîcødé"',
        execution_count=3,
        outputs=[
            new_output(
                output_type="execute_result",
                data={
                    "text/plain": "<array a>",
                    "text/html": "The HTML rep",
                    "text/latex": "$a$",
                    "image/png": png,
                    "image/jpeg": jpeg,
                    "image/svg+xml": "<svg>",
                    "application/json": {"key": "value"},
                    "application/javascript": "var i=0;",
                },
                execution_count=3,
            ),
            new_output(
                output_type="display_data",
                data={
                    "text/plain": "<array a>",
                    "text/html": "The HTML rep",
                    "text/latex": "$a$",
                    "image/png": png,
                    "image/jpeg": jpeg,
                    "image/svg+xml": "<svg>",
                    "application/json": {"key": "value"},
                    "application/javascript": "var i=0;",
                },
            ),
            new_output(
                output_type="error",
                ename="NameError",
                evalue="NameError was here",
                traceback=["frame 0", "frame 1", "frame 2"],
            ),
            new_output(output_type="stream", text="foo\rbar\r\n"),
            new_output(output_type="stream", name="stderr", text="\rfoo\rbar\n"),
        ],
    )
)

nb0 = new_notebook(
    cells=cells,
    metadata={
        "language": "python",
    },
)