def filter_notebook(expr,in_file,out_file=None): c = Config() c.FilterCellsPreprocessor.expression = expr exporter = NotebookExporter(preprocessors=[FilterCellsPreprocessor], config=c) body,resources = exporter.from_file(in_file) if out_file: write_nb(body,resources,out_file) print 'written to: %s \n\n' %out_file else: return (body,resources)