示例#1
0
def get_module_text(notebook_path):
    '''Read ipynb file and get all code from code cells with #export or # export at the beginning'''
    nb = read_nb(notebook_path)
    module = ''
    for cell in nb['cells']:
      if cell['cell_type']=='code':
        if cell['source'].startswith('#export') or cell['source'].startswith('# export'):
          module = module + cell['source'] + '\n\n'
    return module
示例#2
0
def get_properties_from_cells(fn: str,globs:dict,return_files:bool = True,):
    """Gets the properties from all #property cells"""

    nb = read_nb(fn)

    properties = {}

    for cell in nb["cells"]:
        if is_property(cell):
            add_cell_to_properties(cell,properties,globs=globs)

    files = files_in_properties(properties)
    return properties,files
示例#3
0
def get_codes(fn: str, default: str = "main.py") -> dict:
    nb = read_nb(fn)

    module_to_code = defaultdict(str)

    module_to_code[default] = ""

    for cell in nb["cells"]:
        code = is_code(cell, default)
        if code:
            module_to_code[code] += cell["source"]

    return dict(module_to_code)
示例#4
0
def _test_nb(fn, flags=None):
    "Execute tests in notebook in `fn` with `flags`"
    os.environ["IN_TEST"] = '1'
    if flags is None: flags = []
    try:
        nb = read_nb(fn)
        nb = before_test(nb)  # <- THIS is the only change to nbdev code
        for f in get_all_flags(nb['cells']):
            if f not in flags: return
        ep = NoExportPreprocessor(flags, timeout=600, kernel_name='python3')
        pnb = nbformat.from_dict(nb)
        ep.preprocess(pnb)
    finally:
        os.environ.pop("IN_TEST")
示例#5
0
def get_codes(fn:str,default:str = "main.py") -> dict:
    """Returns a dictionary where each key contains the name
    of the module, and the value is the code in it."""
    nb = read_nb(fn)

    module_to_code = defaultdict(str)

    module_to_code[default] = ""

    for cell in nb["cells"]:
        code = is_code(cell,default)
        if code:
            module_to_code[code] += cell["source"]

    return dict(module_to_code)