Example #1
0
 def generate_notebooks(self, eval_dir, colab_dir, tab):
     if not self._valid:
         return
     # copy notebook fron eval_dir to colab_dir
     utils.run_cmd(['rm -rf', colab_dir])
     utils.run_cmd(['cp -r', eval_dir, colab_dir])
     notebooks = utils.find_files('**/*.ipynb', colab_dir)
     for fn in notebooks:
         nb = notebook.read(fn)
         if not nb:
             continue
         # Use Python3 as the kernel
         update_notebook_kernel(nb, "python3", "Python 3")
         # Check if GPU is needed
         use_gpu = False
         for cell in nb.cells:
             if cell.cell_type == 'code':
                 if self.config['gpu_pattern'] in cell.source:
                     use_gpu = True
                     break
         if use_gpu:
             nb['metadata'].update({"accelerator": "GPU"})
             logging.info('Use GPU for ' + fn)
         # Update SVG image URLs
         if self.config['replace_svg_url']:
             _update_svg_urls(nb, self.config['replace_svg_url'], fn,
                              colab_dir)
         insert_additional_installation(nb, self._libs[tab],
                                        self.config['libs_header'])
         with open(fn, 'w') as f:
             f.write(nbformat.writes(nb))
Example #2
0
 def slides(self):
     self.eval()
     notebooks = find_files(
         os.path.join(self.config.eval_dir, '**', '*.ipynb'))
     updated_notebooks = get_updated_files(notebooks, self.config.eval_dir,
                                           self.config.slides_dir, 'ipynb',
                                           'ipynb')
     sd = Slides(self.config)
     for src, tgt in updated_notebooks:
         nb = notebook.read(src)
         if not nb:
             continue
         sd.generate(nb, tgt)
     sd.generate_readme()
Example #3
0
 def generate_notebooks(self, eval_dir, sagemaker_dir, tab):
     if not self._valid:
         return
     utils.run_cmd(['rm -rf', sagemaker_dir])
     utils.run_cmd(['cp -r', eval_dir, sagemaker_dir])
     notebooks = utils.find_files('**/*.ipynb', sagemaker_dir)
     for fn in notebooks:
         nb = notebook.read(fn)
         if not nb:
             continue
         colab.update_notebook_kernel(nb, self._kernel[tab])
         colab.insert_additional_installation(nb, self._libs[tab],
                                              self.config['libs_header'])
         with open(fn, 'w') as f:
             f.write(nbformat.writes(nb))
Example #4
0
def update_ipynb_toc(root):
    """Change the toc code block into a list of clickable links"""
    notebooks = find_files('**/*.ipynb', root)
    for fn in notebooks:
        nb = notebook.read(fn)
        if not nb:
            continue
        for cell in nb.cells:
            if (cell.cell_type == 'markdown' and '```toc' in cell.source):
                md_cells = markdown.split_markdown(cell.source)
                for c in md_cells:
                    if c['type'] == 'code' and c['class'] == 'toc':
                        toc = []
                        for l in c['source'].split('\n'):
                            if l and not l.startswith(':'):
                                toc.append(' - [%s](%s.ipynb)' % (l, l))
                        c['source'] = '\n'.join(toc)
                        c['type'] = 'markdown'
                cell.source = markdown.join_markdown_cells(md_cells)
        with open(fn, 'w') as f:
            f.write(nbformat.writes(nb))