예제 #1
0
def slides():
    parser = argparse.ArgumentParser(
        description='Generate slides from markdown files.')
    parser.add_argument('filename',
                        nargs='+',
                        help='the source markdown files')
    parser.add_argument('--tab', default=None, help='the tab')
    args = parser.parse_args(sys.argv[2:])
    tab = args.tab
    cf = config.Config()
    sd = Slides(cf)
    for fn in args.filename:
        fns = glob.glob(fn)
        if not len(fns):
            logging.warning('Not found ' + fn)
            return
        for md in fns:
            with open(md, 'r') as f:
                nb = notebook.read_markdown(f.read())
            if tab:
                nb = notebook.split_markdown_cell(nb)
                nb = notebook.get_tab_notebook(nb, tab, cf.default_tab)
            output_fn = str(pathlib.Path(md).with_suffix('')) + (
                '_' + tab if tab else '_') + '_slides.ipynb'
            sd.generate(nb, output_fn)
예제 #2
0
    def test_get_tab_notebook(self):
        nb = notebook.split_markdown_cell(
            notebook.read_markdown(_markdown_src))
        new_nb = notebook.get_tab_notebook(nb,
                                           tab='python2',
                                           default_tab='python3')
        cells = new_nb.cells
        self.assertEqual(cells[0].cell_type, 'markdown')
        self.assertEqual(cells[1].cell_type, 'markdown')
        self.assertEqual(cells[1].metadata['tab'], ['python2'])
        self.assertEqual(cells[2].cell_type, 'markdown')
        self.assertEqual('tab' in cells[2].metadata, False)
        self.assertEqual(cells[3].metadata['tab'], ['python2'])
        self.assertEqual(cells[4].cell_type, 'code')
        self.assertEqual(cells[4].metadata['tab'], ['python2'])
        self.assertEqual(len(cells), 6)

        new_nb = notebook.get_tab_notebook(nb,
                                           tab='python3',
                                           default_tab='python3')
        cells = new_nb.cells
        self.assertEqual(cells[3].metadata['tab'], ['python3'])
        self.assertEqual(len(cells), 5)

        nb = notebook.read_markdown(_multi_tab_cell)
        cells = notebook.get_tab_notebook(nb,
                                          tab='python2',
                                          default_tab='python3').cells
        self.assertEqual(len(cells), 3)
        self.assertEqual(cells[1].metadata['tab'], ['python2'])

        cells = notebook.get_tab_notebook(nb,
                                          tab='python3',
                                          default_tab='python3').cells
        self.assertEqual(len(cells), 3)
        self.assertEqual(cells[1].metadata['tab'], ['python3'])
예제 #3
0
def _save_code(input_fn,
               output_fp,
               save_mark='@save',
               tab=None,
               default_tab=None):
    """get the code blocks (import, class, def) that will be saved"""
    with open(input_fn, 'r', encoding='UTF-8') as f:
        nb = notebook.read_markdown(f.read())
    if tab:
        nb = notebook.get_tab_notebook(nb, tab, default_tab)
        if not nb:
            return
    saved = []
    for cell in nb.cells:
        if cell.cell_type == 'code':
            block = _save_block(cell.source, save_mark)
            if block: saved.append(block)
    if saved:
        logging.info('Found %d blocks in %s', len(saved), input_fn)
        for block in saved:
            code = '# Defined in file: %s\n%s\n\n\n' % (input_fn, block)
            output_fp.write(code)
예제 #4
0
def _process_and_eval_notebook(scheduler,
                               input_fn,
                               output_fn,
                               run_cells,
                               config,
                               timeout=20 * 60,
                               lang='python'):
    with open(input_fn, 'r') as f:
        md = f.read()
    nb = notebook.read_markdown(md)
    tab = config.tab
    if tab:
        # get the tab
        nb = notebook.split_markdown_cell(nb)
        nb = notebook.get_tab_notebook(nb, tab, config.default_tab)
        if not nb:
            logging.info(f"Skip to eval tab {tab} for {input_fn}")
            # write an empty file to track the dependencies
            open(output_fn, 'w')
            return
        # replace alias
        if tab in config.library:
            nb = library.replace_alias(nb, config.library[tab])
    nb = library.format_code_nb(nb)

    if not run_cells:
        logging.info(f'Converting {input_fn} to {output_fn}')
        _job(nb, output_fn, run_cells, timeout, lang)
    else:
        # use at most 2 gpus to eval a notebook
        num_gpus = resource.get_notebook_gpus(nb, 2)
        scheduler.add(1,
                      num_gpus,
                      target=_job,
                      args=(nb, output_fn, run_cells, timeout, lang),
                      description=f'Evaluating {input_fn}')
예제 #5
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