@@ -106,106 +106,47 @@ def clean():
106
106
107
107
108
108
@contextmanager
109
- def cleanup_nb (nb ):
110
- try :
111
- yield
112
- finally :
113
- try :
114
- os .remove (nb + '.executed' )
115
- except OSError :
116
- pass
117
-
118
-
119
- def get_kernel ():
120
- """Find the kernel name for your python version"""
121
- return 'python%s' % sys .version_info .major
122
-
123
-
124
- def execute_nb (src , dst , allow_errors = False , timeout = 1000 , kernel_name = '' ):
109
+ def maybe_exclude_notebooks ():
125
110
"""
126
- Execute notebook in `src` and write the output to `dst`
127
-
128
- Parameters
129
- ----------
130
- src, dst: str
131
- path to notebook
132
- allow_errors: bool
133
- timeout: int
134
- kernel_name: str
135
- defualts to value set in notebook metadata
136
-
137
- Returns
138
- -------
139
- dst: str
111
+ Skip building the notebooks if pandoc is not installed.
112
+ This assumes that nbsphinx is installed.
140
113
"""
141
- import nbformat
142
- from nbconvert .preprocessors import ExecutePreprocessor
143
-
144
- with io .open (src , encoding = 'utf-8' ) as f :
145
- nb = nbformat .read (f , as_version = 4 )
146
-
147
- ep = ExecutePreprocessor (allow_errors = allow_errors ,
148
- timeout = timeout ,
149
- kernel_name = kernel_name )
150
- ep .preprocess (nb , resources = {})
151
-
152
- with io .open (dst , 'wt' , encoding = 'utf-8' ) as f :
153
- nbformat .write (nb , f )
154
- return dst
155
-
156
-
157
- def convert_nb (src , dst , to = 'html' , template_file = 'basic' ):
158
- """
159
- Convert a notebook `src`.
160
-
161
- Parameters
162
- ----------
163
- src, dst: str
164
- filepaths
165
- to: {'rst', 'html'}
166
- format to export to
167
- template_file: str
168
- name of template file to use. Default 'basic'
169
- """
170
- from nbconvert import HTMLExporter , RSTExporter
171
-
172
- dispatch = {'rst' : RSTExporter , 'html' : HTMLExporter }
173
- exporter = dispatch [to .lower ()](template_file = template_file )
174
-
175
- (body , resources ) = exporter .from_filename (src )
176
- with io .open (dst , 'wt' , encoding = 'utf-8' ) as f :
177
- f .write (body )
178
- return dst
179
-
114
+ base = os .path .dirname (__file__ )
115
+ notebooks = [os .path .join (base , 'source' , nb )
116
+ for nb in ['style.ipynb' ]]
117
+
118
+ checkpoints = os .listdir (
119
+ os .path .join (base , "source" , ".ipynb_checkpoints" )
120
+ )
121
+ notebooks .extend ([os .path .join (base , 'source' , '.ipynb_checkpoints' , cp )
122
+ for cp in checkpoints ])
123
+ contents = {}
124
+ try :
125
+ import nbconvert
126
+ nbconvert .utils .pandoc .get_pandoc_version ()
127
+ except (ImportError , nbconvert .utils .pandoc .PandocMissing ):
128
+ print ("Warning: Pandoc is not installed. Skipping Notebooks." )
129
+ for nb in notebooks :
130
+ with open (nb , 'rt' ) as f :
131
+ contents [nb ] = f .read ()
132
+ os .remove (nb )
133
+ yield
134
+ for nb , content in contents .items ():
135
+ with open (nb , 'wt' ) as f :
136
+ f .write (content )
180
137
181
138
def html ():
182
139
check_build ()
183
140
184
- notebooks = [
185
- 'source/html-styling.ipynb' ,
186
- ]
187
-
188
- for nb in notebooks :
189
- with cleanup_nb (nb ):
190
- try :
191
- print ("Converting %s" % nb )
192
- kernel_name = get_kernel ()
193
- executed = execute_nb (nb , nb + '.executed' , allow_errors = True ,
194
- kernel_name = kernel_name )
195
- convert_nb (executed , nb .rstrip ('.ipynb' ) + '.html' )
196
- except (ImportError , IndexError ) as e :
197
- print (e )
198
- print ("Failed to convert %s" % nb )
199
-
200
- if os .system ('sphinx-build -j 2 -P -b html -d build/doctrees '
201
- 'source build/html' ):
202
- raise SystemExit ("Building HTML failed." )
203
- try :
204
- # remove stale file
205
- os .remove ('source/html-styling.html' )
206
- os .remove ('build/html/pandas.zip' )
207
- except :
208
- pass
141
+ with maybe_exclude_notebooks ():
142
+ if os .system ('sphinx-build -P -b html -d build/doctrees '
143
+ 'source build/html' ):
144
+ raise SystemExit ("Building HTML failed." )
145
+ try :
146
+ # remove stale file
147
+ os .remove ('build/html/pandas.zip' )
148
+ except :
149
+ pass
209
150
210
151
211
152
def zip_html ():
0 commit comments