-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnbtools.py
211 lines (159 loc) · 6.31 KB
/
nbtools.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
"""
Useful tools for running Clawpack from a Jupyter notebook.
This version also includes tools for viewing plots created via `make .plots`
if you are unable to open the html file `_plots/_PlotIndex.html`.
"""
import os
from IPython.core.display import display
from IPython.display import HTML, IFrame
try:
from IPython.display import FileLink
except:
print("*** Ipython version does not support FileLink")
def make_driver(args, env, outfile, verbose):
"""
Use subprocess to run a make command and capture the output.
:Input:
- *args*: arguments to the make command
- *env*: environment to run in (if None, use *os.environ*)
Useful if $CLAW must be set in notebook.
- *outfile*: file name of file for capturing stdout and stderr
- *vervose*: if True, print out command and display link to output file
If the return code is non-zero, print a warning and link to output file
regardless of value of *verbose*.
"""
import subprocess
import os, sys
if env is None:
env = os.environ
if verbose:
print("Executing shell command: make %s" % args)
sys.stdout.flush()
ofile = open(outfile,'w')
cmd_list = ['make'] + args.split()
job = subprocess.Popen(cmd_list, stdout=ofile,stderr=ofile,env=env)
return_code = job.wait()
errors = (return_code != 0)
if errors:
print("*** Possible errors, return_code = %s" % return_code)
if verbose or errors:
local_file = FileLink(outfile)
print("Done... Check this file to see output:")
display(local_file)
def make_htmls(outfile=None, env=None, verbose=False, readme_link=True):
"""Perform 'make .htmls' and display link."""
if outfile is None:
outfile='htmls_output.txt'
args = '.htmls'
make_driver(args, env, outfile, verbose)
if readme_link:
print("See the README.html file for links to input files...")
display(FileLink('README.html'))
def make_data(env=None, verbose=True):
"""Perform 'make data' and display link."""
outfile='data_output.txt'
args = 'data'
make_driver(args, env, outfile, verbose)
def make_exe(new=False, env=None, verbose=True):
"""
Perform 'make .exe' and display link.
If *new = True*, do 'make new' instead to force recompilation of everything.
"""
outfile='compile_output.txt'
if new:
args = 'new'
else:
args = '.exe'
make_driver(args, env, outfile, verbose)
def make_output(label=None, env=None, verbose=True):
"""Perform 'make output' and display link."""
if label is None:
label = ''
else:
if label[0] != '_':
label = '_' + label
outdir = '_output%s' % str(label)
outfile = 'run_output%s.txt' % str(label)
args = 'output OUTDIR=%s' % outdir
make_driver(args, env, outfile, verbose)
return outdir
def make_plots(label=None, env=None, verbose=True):
"""Perform 'make plots' and display links"""
if label is None:
label = ''
else:
if label[0] != '_':
label = '_' + label
outdir = '_output%s' % str(label)
plotdir = '_plots%s' % str(label)
outfile = 'plot_output%s.txt' % str(label)
args = 'plots OUTDIR=%s PLOTDIR=%s' % (outdir,plotdir)
make_driver(args, env, outfile, verbose)
if verbose:
index_file = FileLink('%s/_PlotIndex.html' % plotdir)
print("View plots created at this link:")
display(index_file)
return plotdir
def make_output_and_plots(label=None, env=None, verbose=True):
outdir = make_output(label,env,verbose)
plotdir = make_plots(label,env,verbose)
return outdir,plotdir
def make_all(label=None, env=None, verbose=True):
"""Perform 'make all' and display links"""
if label is None:
label = ''
else:
if label[0] != '_':
label = '_' + label
outdir = '_output%s' % str(label)
plotdir = '_plots%s' % str(label)
outfile = 'make_all_output%s.txt' % str(label)
args = 'all OUTDIR=%s PLOTDIR=%s' % (outdir,plotdir)
make_driver(args, env, outfile, verbose)
if verbose:
index_file = FileLink('%s/_PlotIndex.html' % plotdir)
print("View plots created at this link:")
display(index_file)
return plotdir
# Tools to view plots from _plots if you cannot open _PlotIndex.html, e.g
# due to permission issues on a JupyterHub
def embed_html(elem, width_percent=50, height_pixels=None):
"""
Create an html string to embed an element (png or html file) in notebook.
The desired width is specified as width_percent of browser window.
For large elements you might need to specify a height in pixels
for the scrolling window.
Use HTML(embed_html(...)) to display in a notebook,
or display(HTML(embed_html(...))) also works, and must be used
if more than one thing is to displayed from the same cell.
"""
if height_pixels is None:
embed_html = f"<embed src='{elem}' width='{width_percent}%%'/>'"
else:
embed_html = f"<embed src='{elem}' width='{width_percent}%%'" \
+ f" height='{height_pixels}px' />"
return embed_html
# Convenience functions to display frames, gauges, or animations that
# routinely appear in _plots:
def show_frame(frameno, figno=0, plotdir='_plots',
width_percent=50, height_pixels=None):
elem = '%s/frame%sfig%s.png' % (plotdir, str(frameno).zfill(4), figno)
if not os.path.isfile(elem):
raise ValueError('%s not found' % elem)
embed_frame = embed_html(elem, width_percent, height_pixels)
return display(HTML(embed_frame))
def show_gauge(gaugeno, figno=300, plotdir='_plots',
width_percent=50, height_pixels=None):
elem = '%s/gauge%sfig%s.png' % (plotdir, str(gaugeno).zfill(4), figno)
if not os.path.isfile(elem):
raise ValueError('%s not found' % elem)
embed_gauge = embed_html(elem, width_percent, height_pixels)
return display(HTML(embed_gauge))
def show_movie(figno=0, plotdir='_plots',
width_percent=70, height_pixels=800):
elem = '%s/movie_fig%s.html' % (plotdir, figno)
if not os.path.isfile(elem):
raise ValueError('%s not found' % elem)
embed_movie = embed_html(elem, width_percent, height_pixels)
print(embed_movie)
return display(HTML(embed_movie))