Exemplo n.º 1
0
 def merge(self):
     assert self.config.tab == 'all'
     assert self.config.eval_dir.endswith('_all')
     assert len(self.config.tabs) > 1, self.config.tabs
     default_eval_dir = self.config.eval_dir[:-4]
     notebooks = find_files(os.path.join(default_eval_dir, '**', '*.ipynb'))
     # TODO(mli) if no default tab, then will not trigger merge
     updated_notebooks = get_updated_files(notebooks, default_eval_dir,
                                           self.config.eval_dir, 'ipynb',
                                           'ipynb')
     tab_dirs = [
         default_eval_dir + '_' + tab for tab in self.config.tabs[1:]
     ]
     for default, merged in updated_notebooks:
         src_notebooks = [default]
         for tab_dir in tab_dirs:
             fname = os.path.join(
                 tab_dir, os.path.relpath(default, default_eval_dir))
             if os.path.exists(fname) and os.stat(fname).st_size > 0:
                 src_notebooks.append(fname)
         logging.info(f'merge {src_notebooks} into {merged}')
         src_nbs = [
             nbformat.read(open(fn, 'r'), as_version=4)
             for fn in src_notebooks
         ]
         if len(src_nbs) > 1:
             dst_nb = notebook.merge_tab_notebooks(src_nbs)
             dst_nb = notebook.add_html_tab(dst_nb, self.config.default_tab)
         else:
             dst_nb = src_nbs[0]
         mkdir(os.path.dirname(merged))
         with open(merged, 'w') as f:
             nbformat.write(dst_nb, f)
     self._copy_resources(default_eval_dir, self.config.eval_dir)
Exemplo n.º 2
0
 def test_merge_tab_notebooks(self):
     nb = notebook.split_markdown_cell(notebook.read_markdown(_markdown_src))
     nb2 = notebook.get_tab_notebook(nb, tab='python2', default_tab='python3')
     nb3 = notebook.get_tab_notebook(nb, tab='python3', default_tab='python3')
     new_nb = notebook.merge_tab_notebooks([nb2, nb3])
     self.assertEqual(len(nb.cells), len(new_nb.cells))
     for cell, new_cell in zip(nb.cells, new_nb.cells):
         if new_cell.source != cell.source:
             self.assertTrue(new_cell.source in cell.source)
Exemplo n.º 3
0
    def test_add_html_tab(self):
        nb = notebook.split_markdown_cell(notebook.read_markdown(_markdown_src))
        nb2 = notebook.get_tab_notebook(nb, tab='python2', default_tab='python3')
        nb3 = notebook.get_tab_notebook(nb, tab='python3', default_tab='python3')
        nb4 = notebook.merge_tab_notebooks([nb2, nb3])
        new_nb = notebook.add_html_tab(nb4, default_tab='python3')

        writer = nbconvert.RSTExporter()
        body, _ = writer.from_notebook_node(new_nb)
Exemplo n.º 4
0
 def _split_and_merge(self, nb, tabs):        
     split_nb = [notebook.get_tab_notebook(nb, tab, tabs[0]) for tab in tabs]
     merged_nb = notebook.merge_tab_notebooks(split_nb)
     return split_nb, merged_nb