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 + \ template.limit_output_length_css + template.tabbar_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(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 + template.copybutton_js + template.discourse_js + template.tabbar_js) 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') 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(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)