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 (169 lines) | stat: -rw-r--r-- 3,373 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import annotations

import os
from base64 import encodebytes

from nbformat.v3.nbbase import (
    nbformat,
    nbformat_minor,
    new_author,
    new_code_cell,
    new_heading_cell,
    new_metadata,
    new_notebook,
    new_output,
    new_text_cell,
    new_worksheet,
)

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

ws = new_worksheet()

ws.cells.append(
    new_text_cell(
        "html",
        source="Some NumPy Examples",
    )
)


ws.cells.append(new_code_cell(input="import numpy", prompt_number=1, collapsed=False))

ws.cells.append(
    new_text_cell(
        "markdown",
        source="A random array",
    )
)

ws.cells.append(
    new_text_cell(
        "raw",
        source="A random array",
    )
)

ws.cells.append(new_heading_cell("My Heading", level=2))

ws.cells.append(new_code_cell(input="a = numpy.random.rand(100)", prompt_number=2, collapsed=True))
ws.cells.append(
    new_code_cell(
        input="a = 10\nb = 5\n",
        prompt_number=3,
    )
)
ws.cells.append(
    new_code_cell(
        input="a = 10\nb = 5",
        prompt_number=4,
    )
)

ws.cells.append(
    new_code_cell(
        input='print "ünîcødé"',
        prompt_number=3,
        collapsed=False,
        outputs=[
            new_output(
                output_type="pyout",
                output_text="<array a>",
                output_html="The HTML rep",
                output_latex="$a$",
                output_png=png,
                output_jpeg=jpeg,
                output_svg="<svg>",
                output_json='{"json": "data"}',
                output_javascript="var i=0;",
                prompt_number=3,
            ),
            new_output(
                output_type="display_data",
                output_text="<array a>",
                output_html="The HTML rep",
                output_latex="$a$",
                output_png=png,
                output_jpeg=jpeg,
                output_svg="<svg>",
                output_json='{"json": "data"}',
                output_javascript="var i=0;",
            ),
            new_output(
                output_type="pyerr",
                ename="NameError",
                evalue="NameError was here",
                traceback=["frame 0", "frame 1", "frame 2"],
            ),
            new_output(output_type="stream", output_text="foo\rbar\r\n"),
            new_output(output_type="stream", stream="stderr", output_text="\rfoo\rbar\n"),
        ],
    )
)

authors = [
    new_author(
        name="Bart Simpson",
        email="bsimpson@fox.com",
        affiliation="Fox",
        url="http://www.fox.com",
    )
]
md = new_metadata(
    name="My Notebook",
    license="BSD",
    created="8601_goes_here",
    modified="8601_goes_here",
    gistid="21341231",
    authors=authors,
)

nb0 = new_notebook(worksheets=[ws, new_worksheet()], metadata=md)

nb0_py = """# -*- coding: utf-8 -*-
# <nbformat>%i.%i</nbformat>

# <htmlcell>

# Some NumPy Examples

# <codecell>

import numpy

# <markdowncell>

# A random array

# <rawcell>

# A random array

# <headingcell level=2>

# My Heading

# <codecell>

a = numpy.random.rand(100)

# <codecell>

a = 10
b = 5

# <codecell>

a = 10
b = 5

# <codecell>

print "ünîcødé"

""" % (
    nbformat,
    nbformat_minor,
)