def to_notebook(self, s, **kwargs): """Convert the markdown string s to an IPython notebook. Returns a notebook. """ all_blocks = self.parse_blocks(s) cells = [] for block in all_blocks: if block['type'] == self.code: kwargs = { 'input': block['content'], 'language': block['language'] } code_cell = nbbase.new_code_cell(**kwargs) cells.append(code_cell) elif block['type'] == self.markdown: kwargs = { 'cell_type': block['type'], 'source': block['content'] } markdown_cell = nbbase.new_text_cell(**kwargs) cells.append(markdown_cell) else: raise NotImplementedError("{} is not supported as a cell" "type".format(block['type'])) ws = nbbase.new_worksheet(cells=cells) nb = nbbase.new_notebook(worksheets=[ws]) return nb
def to_notebook(self, s, **kwargs): """Convert the markdown string s to an IPython notebook. Returns a notebook. """ all_blocks = self.parse_blocks(s) if self.pre_code_block['content']: # TODO: if first block is markdown, place after? all_blocks.insert(0, self.pre_code_block) cells = [] for block in all_blocks: if block['type'] == self.code: kwargs = {'input': block['content']} code_cell = nbbase.new_code_cell(**kwargs) cells.append(code_cell) elif block['type'] == self.markdown: kwargs = {'cell_type': block['type'], 'source': block['content']} markdown_cell = nbbase.new_text_cell(**kwargs) cells.append(markdown_cell) else: raise NotImplementedError("{} is not supported as a cell" "type".format(block['type'])) ws = nbbase.new_worksheet(cells=cells) nb = nbbase.new_notebook(worksheets=[ws]) return nb
def test(self): payload = open(filename) nb = read(payload, 'json') # turn off colors, so we can properly read exceptions cell_nc = new_code_cell(input='%colors NoColor', prompt_number=0) nb.worksheets[0].cells.insert(0, cell_nc) # workaround for matplotlib backend cell_mpl = new_code_cell(input="import matplotlib; matplotlib.use('Agg')", prompt_number=1) nb.worksheets[0].cells.insert(1, cell_mpl) # set working dir to notebook path wd = os.path.abspath(os.path.dirname(filename)) runner = NotebookRunner(nb, working_dir=wd) #try: runner.run_notebook(skip_exceptions=False)
def create_code_cell(block): """Create a notebook code cell from a block.""" code_cell = nbbase.new_code_cell(input=block['content']) attr = block['attributes'] if not attr.is_empty: code_cell.metadata \ = nbbase.NotebookNode({'attributes': attr.to_dict()}) code_cell.prompt_number = attr.kvs.get('n') return code_cell