def _write_css(self): fname = os.path.join(self.config.rst_dir, '_static', 'd2l.css') with open(fname, 'w') as f: for fname in utils.find_files(self.config.html['include_css'], self.config.src_dir): with open(fname, 'r') as fin: f.write(fin.read())
def generate_notebooks(config, eval_dir, colab_dir): """Add a colab setup code cell and then save to colab_dir""" if not config['github_repo']: return # copy notebook fron eval_dir to colab_dir run_cmd(['rm -rf', colab_dir]) run_cmd(['cp -r', eval_dir, colab_dir]) notebooks = find_files('**/*.ipynb', colab_dir) for fn in notebooks: with open(fn, 'r') as f: notebook = nbformat.read(f, as_version=4) # Use Python3 as the kernel update_notebook_kernel(notebook, "python3", "Python 3") # Check if GPU is needed use_gpu = False for cell in notebook.cells: if cell.cell_type == 'code': if config['gpu_pattern'] in cell.source: use_gpu = True break if use_gpu: notebook['metadata'].update({"accelerator": "GPU"}) logging.info('Use GPU for ' + fn) # Update SVG image URLs if config['replace_svg_url']: update_svg_urls(notebook, config['replace_svg_url'], fn, colab_dir) insert_additional_installation(notebook, config) with open(fn, 'w') as f: f.write(nbformat.writes(notebook))
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))
def _write_css(self): fname = os.path.join(self.config.rst_dir, '_static', 'd2l.css') d2l_css = template.hide_bibkey_css + template.copybutton_css with open(fname, 'w') as f: f.write(d2l_css) for fname in utils.find_files(self.config.html['include_css'], self.config.src_dir): with open(fname, 'r') as fin: f.write(fin.read())
def generate_notebooks(config, eval_dir, sagemaker_dir): if not config['github_repo']: return # copy notebook fron eval_dir to colab_dir run_cmd(['rm -rf', sagemaker_dir]) run_cmd(['cp -r', eval_dir, sagemaker_dir]) notebooks = find_files('**/*.ipynb', sagemaker_dir) for fn in notebooks: with open(fn, 'r') as f: notebook = nbformat.read(f, as_version=4) update_notebook_kernel(notebook, config['kernel']) insert_additional_installation(notebook, config) with open(fn, 'w') as f: f.write(nbformat.writes(notebook))
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))
def _write_js(self): d2l_js = template.shorten_sec_num + template.replace_qr g_id = 'google_analytics_tracking_id' if g_id in self.config.deploy: d2l_js += template.google_tracker.replace(g_id.upper(), self.config.deploy[g_id]) os.makedirs(os.path.join(self.config.rst_dir, '_static'), exist_ok=True) fname = os.path.join(self.config.rst_dir, '_static', 'd2l.js') logging.info('write into %s', fname) with open(fname, 'w') as f: f.write(d2l_js) for fname in utils.find_files(self.config.html['include_js'], self.config.src_dir): with open(fname, 'r') as fin: f.write(fin.read())
def add_button(config, html_dir): """Add an open colab button in HTML""" if not config['github_repo']: return files = find_files('**/*.html', html_dir, config['exclusions']) for fn in files: with open(fn, 'r') as f: html = f.read() if 'id="colab"' in html: continue colab_link = 'https://colab.research.google.com/github/%s/blob/master/%s'%( config['github_repo'], os.path.relpath(fn, html_dir).replace('.html', '.ipynb')) colab_html = '<a href="%s"> <button style="float:right", id="colab" class="mdl-button mdl-js-button mdl-button--primary mdl-js-ripple-effect"> <i class=" fas fa-external-link-alt"></i> Colab </button></a><div class="mdl-tooltip" data-mdl-for="colab"> Open notbook in Colab</div>' % (colab_link) html = html.replace('</h1>', colab_html+'</h1>') with open(fn, 'w') as f: f.write(html)
def generate_notebooks(config, eval_dir, colab_dir): """Add a colab setup code cell and then save to colab_dir""" if not config['github_repo']: return # copy notebook fron eval_dir to colab_dir run_cmd(['rm -rf', colab_dir]) run_cmd(['cp -r', eval_dir, colab_dir]) notebooks = find_files('**/*.ipynb', colab_dir) for fn in notebooks: with open(fn, 'r') as f: notebook = nbformat.read(f, as_version=4) # Use Python3 as the kernel notebook['metadata'].update({"kernelspec": { "name": "python3", "display_name": "Python 3" }}) # Check if GPU is needed use_gpu = False for cell in notebook.cells: if cell.cell_type == 'code': if config['gpu_pattern'] in cell.source: use_gpu = True break if use_gpu: notebook['metadata'].update({"accelerator": "GPU"}) logging.info('Use GPU for '+fn) # Update SVG image URLs if config['replace_svg_url']: update_svg_urls(notebook, config['replace_svg_url'], fn, colab_dir) # Add additional libraries if config['libs']: cell = get_installation_cell(notebook, config['libs']) if cell: notebook.cells.insert(0, cell) if config['libs_header']: notebook.cells.insert( 0, nbformat.v4.new_markdown_cell(source=config['libs_header'])) with open(fn, 'w') as f: f.write(nbformat.writes(notebook))
def add_button(self, html_dir): """Add an open colab button in HTML""" if not self._valid: return files = utils.find_files('**/*.html', html_dir, self.config['exclusions']) for fn in files: with open(fn, 'r') as f: html = f.read() if 'id="Colab' in html: continue url = os.path.relpath(fn, html_dir).replace('.html', '.ipynb') if self.tabs: colab_html = '' for tab in self.tabs: colab_tab = _get_colab_html(self._repo[tab], url, f'Colab [{tab}]') colab_html += f'<div class="d2l-tabs__tab">{colab_tab}</div>' colab_html = f'<div class="d2l-tabs" style="float:right">{colab_html}</div>' else: colab_html = _get_colab_html(self._repo[None], url, f'Colab') html = html.replace('</h1>', colab_html + '</h1>') with open(fn, 'w') as f: f.write(html)