##// END OF EJS Templates
added 'py' format, which is cleaner than PyWriter...
Paul Ivanov -
Show More
@@ -1254,6 +1254,115 b' class ConverterNotebook(Converter):'
1254 """
1254 """
1255 return [output.javascript]
1255 return [output.javascript]
1256
1256
1257 class ConverterPy(Converter):
1258 """
1259 A converter that takes a notebook and converts it to a .py file.
1260
1261 What distinguishes this from PyWriter and PyReader in IPython.nbformat is
1262 that subclasses can specify what to do with each type of cell.
1263 Additionally, unlike PyWriter, this does not preserve the '# <markdown>'
1264 opening and closing comments style comments in favor of a cleaner looking
1265 python program.
1266
1267 Note:
1268 Even though this produces a .py file, it is not guaranteed to be valid
1269 python file, since the notebook may be using magics and even cell
1270 magics.
1271 """
1272 extension = 'py'
1273
1274 def __init__(self, infile, show_prompts=True, show_output=True):
1275 super(ConverterPy, self).__init__(infile)
1276 self.show_prompts = show_prompts
1277 self.show_output = show_output
1278
1279 @staticmethod
1280 def comment(input):
1281 "returns every line in input as commented out"
1282 return "# "+input.replace("\n", "\n# ")
1283
1284 @DocInherit
1285 def render_heading(self, cell):
1286 return ['#{0} {1}'.format('#'*cell.level, cell.source), '']
1287
1288 @DocInherit
1289 def render_code(self, cell):
1290 if not cell.input:
1291 return []
1292 lines = []
1293 if self.show_prompts:
1294 lines.extend(['# In[%s]:' % cell.prompt_number])
1295 src = cell.input
1296 lines.extend([src, ''])
1297 if self.show_output:
1298 if cell.outputs :
1299 lines.extend(['# Out[%s]:' % cell.prompt_number])
1300 for output in cell.outputs:
1301 conv_fn = self.dispatch(output.output_type)
1302 lines.extend(conv_fn(output))
1303 return lines
1304
1305 @DocInherit
1306 def render_markdown(self, cell):
1307 return [self.comment(cell.source), '']
1308
1309 @DocInherit
1310 def render_raw(self, cell):
1311 if self.raw_as_verbatim:
1312 return [self.comment(indent(cell.source)), '']
1313 else:
1314 return [self.comment(cell.source), '']
1315
1316 @DocInherit
1317 def render_pyout(self, output):
1318 lines = []
1319
1320 ## if 'text' in output:
1321 ## lines.extend(['*Out[%s]:*' % output.prompt_number, ''])
1322
1323 # output is a dictionary like object with type as a key
1324 if 'latex' in output:
1325 pass
1326
1327 if 'text' in output:
1328 lines.extend([self.comment(indent(output.text)), ''])
1329
1330 lines.append('')
1331 return lines
1332
1333 @DocInherit
1334 def render_pyerr(self, output):
1335 # Note: a traceback is a *list* of frames.
1336 return [indent(remove_ansi('\n'.join(output.traceback))), '']
1337
1338 @DocInherit
1339 def _img_lines(self, img_file):
1340 return [ self.comment('image file: %s' % img_file), '']
1341
1342 @DocInherit
1343 def render_display_format_text(self, output):
1344 return [self.comment(indent(output.text))]
1345
1346 @DocInherit
1347 def _unknown_lines(self, data):
1348 return [self.comment('Warning: Unknown cell'+ str(data))]
1349
1350 @DocInherit
1351 def render_display_format_html(self, output):
1352 return [self.comment(output.html)]
1353
1354 @DocInherit
1355 def render_display_format_latex(self, output):
1356 return []
1357
1358 @DocInherit
1359 def render_display_format_json(self, output):
1360 return []
1361
1362 @DocInherit
1363 def render_display_format_javascript(self, output):
1364 return []
1365
1257 #-----------------------------------------------------------------------------
1366 #-----------------------------------------------------------------------------
1258 # Standalone conversion functions
1367 # Standalone conversion functions
1259 #-----------------------------------------------------------------------------
1368 #-----------------------------------------------------------------------------
@@ -1396,7 +1505,7 b' def cell_to_lines(cell):'
1396 return s.split('\n')
1505 return s.split('\n')
1397
1506
1398
1507
1399 known_formats = "rst (default), html, quick-html, latex, markdown"
1508 known_formats = "rst (default), html, quick-html, latex, markdown, py"
1400
1509
1401 def main(infile, format='rst'):
1510 def main(infile, format='rst'):
1402 """Convert a notebook to html in one step"""
1511 """Convert a notebook to html in one step"""
@@ -1415,6 +1524,9 b" def main(infile, format='rst'):"
1415 elif format == 'latex':
1524 elif format == 'latex':
1416 converter = ConverterLaTeX(infile)
1525 converter = ConverterLaTeX(infile)
1417 latexfname = converter.render()
1526 latexfname = converter.render()
1527 elif format == 'py':
1528 converter = ConverterPy(infile)
1529 converter.render()
1418 else:
1530 else:
1419 raise SystemExit("Unknown format '%s', " % format +
1531 raise SystemExit("Unknown format '%s', " % format +
1420 "known formats are: " + known_formats)
1532 "known formats are: " + known_formats)
General Comments 0
You need to be logged in to leave comments. Login now