/
notebook.py
287 lines (247 loc) · 9.4 KB
/
notebook.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
""" Defines the Notebook class """
from __future__ import division, print_function, absolute_import, unicode_literals
#*****************************************************************
# pyGSTi 0.9: Copyright 2015 Sandia Corporation
# This Software is released under the GPL license detailed
# in the file "license.txt" in the top-level pyGSTi directory
#*****************************************************************
from .notebookcell import NotebookCell
import os as _os
import json as _json
import webbrowser as _browser
import textwrap as _textwrap
from subprocess import call as _call
class Notebook(object):
'''
Python representation of an IPython notebook
'''
DefaultTemplate = 'Empty.ipynb'
def __init__(self, cells=None, notebookTextFiles=None):
'''
Create an IPython notebook from a list of cells, list of notebookTextFiles, or both.
Parameters
----------
cells : list, optional
List of NotebookCell objects
notebookTextFiles : list, optional
List of filenames (text files with '@@markdown' or '@@code' designating cells)
'''
if cells is None:
cells = []
self.cells = cells
if notebookTextFiles is not None:
for filename in notebookTextFiles:
self.add_notebook_text_file(filename)
def to_json_dict(self, templateFilename=DefaultTemplate):
'''
Using an existing (usually empty) notebook as a template, generate the json for a new notebook.
Parameters
----------
templateFilename : str, optional
Name of an existing notebook file to build from
'''
templateFilename = _os.path.join(_os.path.dirname(_os.path.abspath(__file__)),
'templates', templateFilename )
with open(templateFilename, 'r') as infile:
notebookDict = _json.load(infile)
notebookDict['cells'].extend([c.to_json_dict() for c in self.cells])
return notebookDict
def save_to(self, outputFilename, templateFilename=DefaultTemplate):
'''
Save this class to a file as a jupyter notebook
Parameters
----------
outputFilename : str
File to save the output jupyter notebook to
templateFilename : str, optional
Name of an existing notebook file to build from
'''
jsonDict = self.to_json_dict(templateFilename)
with open(outputFilename, 'w') as outfile:
_json.dump(jsonDict, outfile)
def add(self, cell):
'''
Add a cell to the notebook
Parameters
----------
cell : NotebookCell object
'''
self.cells.append(cell)
def add_block(self, block, cellType):
'''
Add a block to the notebook
Parameters
----------
block : str
block of either code or markdown
cellType : str
tag for the cell. Either 'code' or 'markdown'
'''
lines = block.splitlines(True)
self.add(NotebookCell(cellType, lines))
def add_file(self, filename, cellType):
'''
Read in a cell block from a file
Parameters
----------
filename: str
filename containing either code or markdown
cellType : str
tag for the cell. Either 'code' or 'markdown'
'''
with open(filename, 'r') as infile:
block = infile.read()
self.add_block(block, cellType)
def add_code(self, block):
'''
Add code to notebook
Parameters
----------
block : str
Block of python code
'''
self.add_block(_textwrap.dedent(block), 'code')
def add_markdown(self, block):
'''
Add markdown to notebook
Parameters
----------
block : str
Block of markdown (or HTML)
'''
self.add_block(block, 'markdown')
def add_code_file(self, filename):
'''
Add a code file to the notebook
Parameters
----------
filename : str
name of file containing python code
'''
self.add_file(filename, 'code')
def add_markdown_file(self, filename):
'''
Add a markdown file to the notebook
Parameters
----------
filename : str
name of file containing markdown
'''
self.add_file(filename, 'markdown')
def add_notebook_text(self, text):
'''
Add custom formatted text to the notebook
Text contains both python and markdown, with
cells differentiated by @@code and @@markdown tags.
At least one cell tag must be present for the file to be correctly parsed
Parameters
----------
text : str
notebook formatted text
'''
assert '@@' in text, 'At least one cell tag must be present for the file to be correctly parsed'
for block in text.split('@@'):
if block == '':
continue
if block.startswith('code'):
block = block.replace('code', '', 1)
block = block.strip('\n')
'''
TODO: Auto-move comments to markdown
lines = []
for line in block.splitlines():
if '#' in line:
i = line.index('#')
'''
self.add_code(block)
elif block.startswith('markdown'):
block = block.replace('markdown', '', 1)
block = block.strip('\n')
self.add_markdown(block)
else:
raise ValueError('Invalid notebook text block heading:\n{}'.format(block))
def add_notebook_text_file(self, filename):
'''
Add a custom formatted text file to the notebook
Text file contains both python and markdown, with
cells differentiated by @@code and @@markdown tags.
At least one cell tag must be present for the file to be correctly parsed
Parameters
----------
filename : str
name of file containing notebook formatted text
'''
with open(filename, 'r') as infile:
self.add_notebook_text(infile.read())
def add_notebook_text_files(self, filenames):
'''
Add multiple notebook text files to the notebook, in order
Parameters
----------
filenames : list(str)
names of file containing notebook formatted text
'''
for filename in filenames:
self.add_notebook_text_file(filename)
def add_notebook_file(self, filename):
'''
Append an .ipynb file to this notebook
Parameters
----------
filename : str
ipynb file to append
'''
with open(filename, 'r') as infile:
notebookDict = _json.load(infile)
for cell in notebookDict['cells']:
self.add(NotebookCell(cell['cell_type'], cell['source']))
def add_notebook_files(self, filenames):
'''
Add multiple notebook files to the notebook, in order
Parameters
----------
filenames : list(str)
names of file containing ipynb json
'''
for filename in filenames:
self.add_notebook_file(filename)
def launch_new(self, outputFilename, templateFilename=DefaultTemplate):
'''
Save and then launch this notebook with a new jupyter server. Note that
this function waits to return until the notebook server exists, and so
is difficult to work with.
Parameters
----------
outputFilename : str
filename to save this notebook to
templateFilename : str, optional
filename to build this notebook from (see save_to)
'''
self.save_to(outputFilename, templateFilename)
_call('jupyter notebook {}'.format(outputFilename), shell=True) #this waits for notebook to complete
#_os.system('jupyter notebook {}'.format(outputFilename)) # same behavior as above
#processid = _os.spawnlp(_os.P_NOWAIT, 'jupyter', 'notebook', _os.path.abspath(outputFilename)) #DOESN'T WORK
#print("DB: spawned notebook %d!" % processid)
def launch(self, outputFilename, templateFilename=DefaultTemplate, port='auto'):
'''
Save and then launch this notebook
Parameters
----------
outputFilename : str
filename to save this notebook to
templateFilename : str, optional
filename to build this notebook from (see save_to)
'''
self.save_to(outputFilename, templateFilename)
outputFilename = _os.path.abspath(outputFilename) #for path manips below
from notebook import notebookapp
servers = list(notebookapp.list_running_servers())
for serverinfo in servers:
rel = _os.path.relpath(outputFilename, serverinfo['notebook_dir'])
if ".." not in rel: # notebook servers don't allow moving up directories
if port == 'auto'or int(serverinfo['port']) == port:
url = _os.path.join( serverinfo['url'], 'notebooks', rel)
_browser.open(url); break
else:
print("No running notebook server found that is rooted above %s" %
outputFilename)