def __init__(self, config): self._valid = config.slides and config.slides['github_repo'] if not self._valid: return self.config = config repo = utils.split_config_str(self.config.slides['github_repo'], 2) self._repo = {r[0]: r[1] for r in repo}
def _update_header_links(self): items = utils.split_config_str(self.config.html['header_links'], 3) sphinx_links = '' for tk in items: if tk: sphinx_links += "('%s', '%s', True, '%s')," % (tk[0], tk[1], tk[2]) self._update_pyconf('header_links', sphinx_links)
def parse_repo_lib(repo_str, lib_str, version): repo = utils.split_config_str(repo_str) if len(repo) == 1 and len(repo[0]) == 1: repos = {None: repo[0]} libs = {None: utils.split_config_str(lib_str, 2)} else: repo = utils.split_config_str(repo_str, 2) repos = {r[0]: r[1] for r in repo} libs_list = utils.split_config_str(lib_str, 3) libs = {} for tab, pkg, install in libs_list: if tab in libs: libs[tab].append([pkg, install]) else: libs[tab] = [[pkg, install]] for tab in libs: for i, l in enumerate(libs[tab]): if '==RELEASE' in l[1]: libs[tab][i][1] = l[1].replace('==RELEASE', f'=={version}') return repos, libs
def __init__(self, config): self._valid = config.sagemaker and config.sagemaker['github_repo'] self.config = config.sagemaker self._repo, self._libs = colab.parse_repo_lib( self.config['github_repo'], self.config['libs'], config.library["version"]) kernel_str = self.config['kernel'] if ',' not in kernel_str: self._kernel = {None: kernel_str} else: kernel = utils.split_config_str(kernel_str, 2) self._kernel = {k[0]: k[1] for k in kernel}
def update_svg_urls(notebook, pattern, filename, root_dir): orgin_url, new_url = split_config_str(pattern, 2)[0] svg_re = re.compile('!\[.*\]\(([\.-_\w\d]+\.svg)\)') for cell in notebook.cells: if cell.cell_type == 'markdown': lines = cell.source.split('\n') for i, l in enumerate(lines): m = svg_re.search(l) if not m: continue path = os.path.relpath(os.path.realpath(os.path.join( root_dir, os.path.basename(filename), m[1])), root_dir) if not path.startswith(orgin_url): logging.warning("%s in %s does not start with %s" "specified by replace_svg_url"%( path, filename, orgin_url)) else: url = new_url + path[len(orgin_url):] lines[i] = l.replace(m[1], url) cell.source = '\n'.join(lines)
def get_installation_cell(notebook, libs): """Return a cell for installing the additional libs""" lib_dict = dict(split_config_str(libs, 2)) lib1_re = re.compile('from ([_\w\d]+) import') lib2_re = re.compile('import ([_\w\d]+)') find_libs = [] for cell in notebook.cells: if cell.cell_type == 'code': lines = cell.source.split('\n') for l in lines: if l.strip().startswith('#'): # it's a comment continue m = lib1_re.search(l) if not m: m = lib2_re.search(l) if m and m[1] in lib_dict: find_libs.append(m[1]) if not find_libs: return None install_str = '' for lib in set(find_libs): install_str += '!pip install ' + lib_dict[lib] + '\n' return nbformat.v4.new_code_cell(source=install_str)