@@ -106,106 +106,42 @@ 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 = '' ):
125
- """
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
140
- """
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' ):
109
+ def maybe_exclude_notebooks ():
158
110
"""
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'
111
+ Skip building the notebooks if pandoc is not installed.
112
+ This assumes that nbsphinx is installed.
169
113
"""
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
114
+ base = os .path .dirname (__file__ )
115
+ notebooks = [os .path .join (base , 'source' , nb )
116
+ for nb in ['style.ipynb' ]]
117
+ contents = {}
118
+ try :
119
+ import nbconvert
120
+ nbconvert .utils .pandoc .get_pandoc_version ()
121
+ except (ImportError , nbconvert .utils .pandoc .PandocMissing ):
122
+ print ("Warning: Pandoc is not installed. Skipping Notebooks." )
123
+ for nb in notebooks :
124
+ with open (nb , 'rt' ) as f :
125
+ contents [nb ] = f .read ()
126
+ os .remove (nb )
127
+ yield
128
+ for nb , content in contents .items ():
129
+ with open (nb , 'wt' ) as f :
130
+ f .write (content )
179
131
180
132
181
133
def html ():
182
134
check_build ()
183
135
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 -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
136
+ with maybe_exclude_notebooks ():
137
+ if os .system ('sphinx-build -P -b html -d build/doctrees '
138
+ 'source build/html' ):
139
+ raise SystemExit ("Building HTML failed." )
140
+ try :
141
+ # remove stale file
142
+ os .remove ('build/html/pandas.zip' )
143
+ except :
144
+ pass
209
145
210
146
211
147
def zip_html ():
0 commit comments