Example #1
0
    def test_add_html_tab(self):
        nb = notebook.split_markdown_cell(notebook.read_markdown(_markdown_src))
        _, new_nb = self._split_and_merge(nb, ['python3', 'python2'])
        new_nb = notebook.add_html_tab(new_nb, tabs=['python3', 'python2'])
        cells = new_nb.cells
        self.assertEqual(len(cells), 18)
        self.assertRegex(cells[1].source, 'mdl-js-tabs')
        self.assertRegex(cells[2].source, 'mdl-tabs__panel.*python2')
        self.assertRegex(cells[4].source, '</div>')
        self.assertRegex(cells[5].source, '</div>')
        self.assertRegex(cells[8].source, 'mdl-tabs__panel.*python3')
        self.assertRegex(cells[12].source, 'mdl-tabs__panel.*python2')

        nb = notebook.split_markdown_cell(notebook.read_markdown(_all_tab_cell))        
        _, new_nb = self._split_and_merge(nb, ['python3', 'python2', 'python4']) 
        cells = new_nb.cells
        self.assertEqual(len(cells), 5)
        self.assertEqual(cells[4].metadata['tab'], ['python3', 'python2'])

        new_nb = notebook.add_html_tab(new_nb, tabs=['python3', 'python2', 'python4'])
        cells = new_nb.cells
        self.assertEqual(len(cells), 15)
        self.assertRegex(cells[3].source, 'mdl-js-tabs')
        self.assertRegex(cells[4].source, 'mdl-tabs__panel.*python3')
        self.assertRegex(cells[7].source, 'mdl-tabs__panel.*python2')
        self.assertRegex(cells[11].source, 'mdl-tabs__panel.*python4')
Example #2
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)
Example #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)