def export_notebook(nb, name, templating="test.tplx", debug=False): shutil.copyfile(pkg_resources.resource_filename(__name__, templating), "test.tplx") pdf_exporter = PDFExporter() pdf_exporter.template_file = "test.tplx" print("Attempting to compile LaTeX") try: pdf_output = pdf_exporter.from_notebook_node(nb) with open("%s.pdf" % name, "wb") as output_file: output_file.write(pdf_output[0]) print("Finished generating PDF") except nbconvert.pdf.LatexFailed as error: print("There was an error generating your LaTeX") output = error.output if debug: print("Showing full error message from PDFTex") else: print("Showing concise error message") output = "\n".join(error.output.split("\n")[-15:]) print("=" * 30) print(output) print("=" * 30) return None return "%s.pdf" % name
def has_error(nb): pdf_exporter = PDFExporter() try: pdf_exporter.from_notebook_node(nb) return None except nbconvert.pdf.LatexFailed as error: return "\n".join(error.output.split("\n")[-15:])
def ipython_to_pdf(raw_executed_ipynb: str, report_title: str) -> AnyStr: pdf_exporter = PDFExporter(Config()) resources = ResourcesDict() resources["metadata"] = ResourcesDict() resources["metadata"]["name"] = report_title pdf, _ = pdf_exporter.from_notebook_node(nbformat.reads( raw_executed_ipynb, as_version=nbformat.v4.nbformat), resources=resources) return pdf
def to_pdf(self): pdf = PDFExporter(config={ "NbConvertBase": { "display_data_priority": self.DATA_DISPLAY_PRIORITY } }) resources = {"metadata": {"path": str(self.nb_home)}} pdf_data, _ = pdf.from_notebook_node(self.nb, resources) fn = self.path.with_suffix(".pdf") with open(fn, mode="wb") as f: f.write(pdf_data)
def _create_report(self, report_kernel_name: str, report_export_html: bool, report_export_pdf: bool): """Creates report from notebook-template and stores it in different formats all figures. - Jupyter Notebook - HTML - PDF """ assert not self.mode_ipython, 'Create report is only possible when not in ipython mode' filepath_template = dirname( imageatm.notebooks.__file__) + '/evaluation_template.ipynb' filepath_notebook = self.evaluation_dir / 'evaluation_report.ipynb' filepath_html = self.evaluation_dir / 'evaluation_report.html' filepath_pdf = self.evaluation_dir / 'evaluation_report.pdf' pm.execute_notebook(str(filepath_template), str(filepath_notebook), parameters=dict(image_dir=str(self.image_dir), job_dir=str(self.job_dir)), kernel_name=report_kernel_name) with open(filepath_notebook) as f: nb = nbformat.read(f, as_version=4) if report_export_html: self.logger.info('\n****** Create HTML ******\n') with open(filepath_notebook) as f: nb = nbformat.read(f, as_version=4) html_exporter = HTMLExporter() html_data, resources = html_exporter.from_notebook_node(nb) with open(filepath_html, 'w') as f: f.write(html_data) f.close() if report_export_pdf: self.logger.info('\n****** Create PDF ******\n') pdf_exporter = PDFExporter() pdf_exporter.template_file = dirname( imageatm.notebooks.__file__ ) + '/tex_templates/evaluation_report.tplx' pdf_data, resources = pdf_exporter.from_notebook_node( nb, resources={'metadata': { 'name': 'Evaluation Report' }}) with open(filepath_pdf, 'wb') as f: f.write(pdf_data) f.close()
def __init__(self, output): assert output in SUPPORTED_FORMATS, f"supported formats are {SUPPORTED_FORMATS}" self.read_encoding = "utf-8" self.write_encoding = "utf-8" self.format = output if self.format == "pdf": pdf = PDFExporter() pdf.exclude_output_prompt = True pdf.exclude_input = True self.exporter = pdf elif self.format == "rst": self.exporter = RSTExporter() else: self.exporter = MarkdownExporter()
def pdf_convertor(in_name, out_name, current_dir, new_dir): #notebook_filename = "telstra-exploring-data.ipynb" with open(in_name + ".ipynb") as f: print 'jupyter notebook is taken from dir:', current_dir nb = nbformat.read(f, as_version=4) ep = ExecutePreprocessor(timeout=600, kernel_name='python2') #python3 ep.preprocess(nb, {'metadata': {'path': new_dir}}) pdf_exporter = PDFExporter() pdf_data, resources = pdf_exporter.from_notebook_node(nb) with open(out_name + ".pdf", "wb") as f: print 'pdf report is saved in dir:', new_dir f.write(pdf_data) f.close()
def ipython_to_pdf(raw_executed_ipynb: str, report_title: str, hide_code: bool = False) -> AnyStr: c = Config() c.PDFExporter.exclude_input = hide_code c.PDFExporter.exclude_output_prompt = hide_code c.HTMLExporter.template_file = pkg_resources.resource_filename( __name__, "../nbtemplates/notebooker_pdf_output.tplx") pdf_exporter = PDFExporter(c) resources = ResourcesDict() resources["metadata"] = ResourcesDict() resources["metadata"]["name"] = report_title pdf, _ = pdf_exporter.from_notebook_node(nbformat.reads( raw_executed_ipynb, as_version=nbformat.v4.nbformat), resources=resources) return pdf
def make_pdf(out, fname): """Make a PDF out of an ipynb.""" # Now output the notebook to pdf if report: c = Config() c.TemplateExporter.exclude_input_prompt = True c.TemplateExporter.exclude_output_prompt = True c.TemplateExporter.exclude_input = True exporter = PDFExporter(config=c) body, resources = exporter.from_filename(out / fname) with open(out / fname.with_suffix(".pdf"), "wb") as fl: fl.write(body) console.print(f"Saved PDF to '{out / fname.with_suffix('.pdf')}'")
def to_pdf(nb: NotebookNode, kernel: str, fpath: Path) -> Tuple[Any, Dict]: """ Compile final notebook into a single PDF while executing it. :param nb: The compiled notebook object with all notebook cells. :param kernel: String name of the kernel to output. :param fpath: The path to write hte notebook to. """ ep = ExecutePreprocessor(timeout=600, kernel_name=kernel) ep.preprocess(nb) strip_execution_count(nb) pdf_exporter = PDFExporter() body, resources = pdf_exporter.from_notebook_node(nb) with open(fpath, "wb") as f: f.write(body)
def to_pdf(self): if self.first: td = TemporaryDirectory() preprocessors = self.get_preprocessors('pdf', td=td) self.preprocess(preprocessors) pdf = PDFExporter(config={ 'NbConvertBase': { 'display_data_priority': self.DISPLAY_DATA_PRIORITY } }) pdf_data, self.resources = pdf.from_notebook_node( self.nb, self.resources) fn = self.final_nb_home / (self.document_name + '.pdf') with open(fn, mode='wb') as f: f.write(pdf_data)
def to_pdf_latex(self): if 'outputs' in self.resources: # remove outputs from MarkdownExporter if used self.resources.pop('outputs') # must download html images for latex to use them from ._preprocessors import MarkdownHTTPPreprocessor temp_dir = Path(self.td.name) self.resources['temp_dir'] = temp_dir MarkdownHTTPPreprocessor().preprocess(self.nb, self.resources) for filename, image_data in self.resources['image_data_dict'].items(): fn_pieces = filename.split('_') cell_idx = int(fn_pieces[1]) ext = fn_pieces[-1].split('.')[-1] new_filename = str(temp_dir / filename) # extract first image from gif and use as png for latex pdf if ext == 'gif': buffer = io.BytesIO(image_data) arr = mimage.imread(buffer, format='gif') new_filename = filename.split('.')[0] + '.png' new_filename = str(temp_dir / new_filename) mimage.imsave(new_filename, arr) else: with open(new_filename, 'wb') as f: f.write(image_data) cell = self.nb.cells[cell_idx] cell['source'] = cell['source'].replace(filename, new_filename) pdf = PDFExporter(config={ 'NbConvertBase': { 'display_data_priority': self.DISPLAY_DATA_PRIORITY } }) pdf_data, self.resources = pdf.from_notebook_node( self.nb, self.resources) self.return_data['pdf_data'] = pdf_data if not self.web_app: fn = self.final_nb_home / (self.document_name + '.pdf') with open(fn, mode='wb') as f: f.write(pdf_data)
def __init__(self, builder): self.pdfdir = builder.outdir + "/pdf" #pdf directory self.texdir = builder.outdir + "/executed" #latex directory self.texbookdir = builder.outdir + "/texbook" # tex files for book pdf for path in [self.pdfdir, self.texdir]: ensuredir(path) self.pdf_exporter = PDFExporter() self.tex_exporter = LatexExporter() self.index_book = builder.config['tojupyter_pdf_book_index']
def __init__(self, output_format, destination_mode): assert output_format in SUPPORTED_FORMATS, f"supported formats are {SUPPORTED_FORMATS}" assert ( destination_mode in SUPPORTED_DESTINATIONS_MODES ), f"supported destination modes are {SUPPORTED_DESTINATIONS_MODES}" self.read_encoding = "utf-8" self.write_encoding = "utf-8" self.format = output_format self.destination_mode = destination_mode if self.format == "pdf": pdf = PDFExporter() pdf.exclude_output_prompt = True pdf.exclude_input = True self.exporter = pdf elif self.format == "rst": self.exporter = RSTExporter() else: self.exporter = MarkdownExporter()
def convert_notebook(report_in_path, report_out_path, **kwargs): curdir = os.path.abspath(os.getcwd()) indir, _ = os.path.split(report_in_path) outdir, _ = os.path.split(report_out_path) os.makedirs(outdir, exist_ok=True) config = { "ExecutePreprocessor": { "enabled": True, "timeout": -1 }, "TemplateExporter": { "exclude_output_prompt": True, "exclude_input": True, "exclude_input_prompt": True }, } nb = nbformat.read(open(report_in_path), as_version=4) html_exporter = HTMLExporter(config=config) # no exectute for PDFs config["ExecutePreprocessor"]["enabled"] = False pdf_exporter = PDFExporter(config=config) # change dir to notebook dir, to execute notebook os.chdir(indir) body, resources = (html_exporter.from_notebook_node(nb)) pdf_body, pdf_resources = (pdf_exporter.from_notebook_node(nb)) # change back to original directory os.chdir(curdir) with open(report_out_path.replace(".pdf", ".html"), 'w') as fh: fh.write(body) with open(report_out_path.replace(".html", ".pdf"), 'wb') as fh: fh.write(pdf_body)
def export_notebook(nb, pdf_path, template="test.tplx", debug=False): """Write notebook as PDF to pdf_path. Return True on success or False on error.""" shutil.copyfile(pkg_resources.resource_filename(__name__, template), "test.tplx") pdf_exporter = PDFExporter() pdf_exporter.template_file = "test.tplx" try: pdf_output = pdf_exporter.from_notebook_node(nb) with open(pdf_path, "wb") as out: out.write(pdf_output[0]) print("Saved", pdf_path) return True except nbconvert.pdf.LatexFailed as error: print("There was an error generating your LaTeX") output = error.output if not debug: print("To see the full error message, run with debug=True") output = "\n".join(error.output.split("\n")[-15:]) print("=" * 30) print(output) print("=" * 30) return False
def save_notebook(notebook_filename): now = str(datetime.datetime.now()).replace(" ", "").replace(".", "-").replace( ":", "-") with open(notebook_filename) as f: nb = nbformat.read(f, as_version=4) # ep = nbconvert.ExecutePreprocessor(timeout=600, kernel_name='python3') # ep.preprocess(nb, {'metadata': {'path': './'}}) pdf_exporter = PDFExporter() pdf_data, resources = pdf_exporter.from_notebook_node(nb) file_result_name = "results/" + notebook_filename.replace(".ipynb", now) + ".pdf" with open(file_result_name, "wb") as f: f.write(pdf_data) f.close() return file_result_name
def run_notebook_convert(dir_experiment_run: Path = None) -> Path: """ Run and convert the notebook to html and pdf. """ # Copy the experiment_vis jupyter notebook to the experiment dir notebook_path = Path(__file__).parent.parent / 'experiment_vis/experiment_vis.ipynb' dest_notebook_path = dir_experiment_run / 'experiment_vis.ipynb' # copy notebook to experiment run shutil.copyfile(notebook_path, dest_notebook_path) log.info(f'Executing experiment vis notebook {dest_notebook_path}.') with open(dest_notebook_path) as f: nb = nbformat.read(f, as_version=4) ep = ExecutePreprocessor(timeout=600, kernel_name='python3') ep.preprocess(nb, {'metadata': {'path': str(dest_notebook_path.parent)}}) nbconvert_path = dest_notebook_path.with_suffix('.nbconvert.ipynb') with open(nbconvert_path, 'w', encoding='utf-8') as f: nbformat.write(nb, f) log.info('Converting notebook to html.') html_path = nbconvert_path.with_suffix('.html') html_exporter = HTMLExporter() html_exporter.template_name = 'classic' (body, resources) = html_exporter.from_notebook_node(nb) with open(html_path, 'w') as f: f.write(body) log.info('Converting notebook to pdf.') pdf_path = nbconvert_path.with_suffix('.pdf') pdf_exporter = PDFExporter() pdf_exporter.template_name = 'classic' (body, resources) = pdf_exporter.from_notebook_node(nb) pdf_path.write_bytes(body) return pdf_path
def pdf_from_latex(pdf=None, verbose=False): '''Export the notebook to PDF via LaTeX. This is not fast because you have to install texlive. verbose is not used right now. ''' if not shutil.which('xelatex'): aptinstall('texlive-xetex') fname, fid = current_notebook() ipynb = notebook_string(fid) exporter = PDFExporter() nb = nbformat.reads(ipynb, as_version=4) body, resources = exporter.from_notebook_node(nb) if pdf is None: pdf = fname.replace(".ipynb", ".pdf") tmpdirname = tempfile.TemporaryDirectory().name if not os.path.isdir(tmpdirname): os.mkdir(tmpdirname) apdf = os.path.join(tmpdirname, pdf) if os.path.exists(apdf): os.unlink(apdf) with open(apdf, 'wb') as f: f.write(body) if os.path.exists(apdf): files.download(apdf) else: print(f'{apdf} not found')
def to_pdf(self): return PDFExporter().from_notebook_node(self._content)[0]
import sys, os import nbformat from nbconvert.preprocessors import ExecutePreprocessor from nbconvert import PDFExporter dir = os.path.split(os.getcwd())[0] if dir not in sys.path: sys.path.append(dir) notebook_filename = "C:\Users\Usuario\Desktop\Universidad\9 semestre\Conmutacionpython\Notebook\central.ipynb" with open(notebook_filename) as f: nb = nbformat.read(f, as_version=4) ep = ExecutePreprocessor(timeout=600, kernel_name='python2') ep.preprocess( nb, { 'metadata': { 'path': 'C:\Users\Usuario\Desktop\Universidad\9 semestre\Conmutacionpython\Notebook/' } }) pdf_exporter = PDFExporter() pdf_data, resources = pdf_exporter.from_notebook_node(nb) with open("notebook.pdf", "wb") as f: f.write(pdf_data) f.close()
def get(self, format: str, path: str): """Handle the GET method call.""" if format != 'pdf': self.log.exception('format must be pdf') raise web.HTTPError(500, 'format must be pdf') self.config.PDFExporter.preprocessors = [thermohw.ExtractAttachmentsPreprocessor] self.config.PDFExporter.template_file = os.path.join(thermohw_dir, 'homework.tpl') self.config.PDFExporter.filters = {'convert_div': thermohw.convert_div, 'convert_raw_html': thermohw.convert_raw_html} self.config.PDFExporter.latex_count = 1 exporter = PDFExporter(config=self.config, log=self.log) exporter.writer.build_directory = '.' pdfs = [] path = path.strip('/').strip() paths = path.split('.ipynb') for path in paths: if not path: continue path += '.ipynb' # If the notebook relates to a real file (default contents manager), # give its path to nbconvert. ext_resources_dir: Union[str, None] basename: str os_path: str if hasattr(self.contents_manager, '_get_os_path'): os_path = self.contents_manager._get_os_path(path) ext_resources_dir, basename = os.path.split(os_path) else: ext_resources_dir = None model: Dict[str, str] = self.contents_manager.get(path=path) name: str = model['name'] if model['type'] != 'notebook': # not a notebook, redirect to files return FilesRedirectHandler.redirect_to_files(self, path) nb = model['content'] self.set_header('Last-Modified', model['last_modified']) # create resources dictionary mod_date: str = model['last_modified'].strftime(text.date_format) nb_title: str = os.path.splitext(name)[0] config_dir: str = self.application.settings['config_dir'] resource_dict: Dict[str, str] = { "metadata": { "name": nb_title, "modified_date": mod_date }, "config_dir": config_dir, } if ext_resources_dir: resource_dict['metadata']['path'] = ext_resources_dir output: bytes try: output, _ = exporter.from_notebook_node( nb, resources=resource_dict ) except Exception as e: self.log.exception("nbconvert failed: %s", e) raise web.HTTPError(500, "nbconvert failed: %s" % e) pdfs.append(io.BytesIO(output)) writer = PdfWriter() for pdf in pdfs: writer.addpages(PdfReader(pdf).pages) bio = io.BytesIO() writer.write(bio) bio.seek(0) output = bio.read() bio.close() # Force download if requested if self.get_argument('download', 'false').lower() == 'true': filename = 'final_output.pdf' self.set_header('Content-Disposition', 'attachment; filename="{}"'.format(filename)) # MIME type if exporter.output_mimetype: self.set_header('Content-Type', '{}; charset=utf-8'.format(exporter.output_mimetype)) self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') self.finish(output)
def package(parser, context, args): ld = arg_date(parser, args) eld_admin = '_private/' + ld eld_solutions = '_private/%s/solutions' % ld eld_notebook = '%s/exam-%s.ipynb' % (eld_solutions, ld) target_student = get_target_student(ld) target_student_pdf = '%s/%s' % (get_target_student(ld), get_exam_text_filename(ld, 'pdf')) # no pdf as hiding cells is too boring, have still # to properly review cells filtering https://github.com/DavidLeoni/jupman/issues/4 # target_student_pdf = target_student + '/' + 'exam-' + ld + '.pdf' target_student_zip = "%s/server/%s-%s-exam" % (eld_admin, jm.filename, ld ) # without '.zip' target_server_zip = "%s/%s-%s-server" % (eld_admin, jm.filename, ld ) # without '.zip' if not os.path.exists(jm.build): fatal("%s WAS NOT BUILT !" % jm.build) if not os.path.exists(eld_solutions): fatal("MISSING SOURCE SOLUTION EXERCISES: " + eld_solutions) if os.path.exists(target_student): fatal("TARGET STUDENT EXERCISES DIRECTORY ALREADY EXISTS: " + target_student) try: dir_names = os.listdir(jm.build) except Exception as e: fatal("ERROR WITH DIR %s" % jm.build, ex=e) if len(dir_names) == 0: fatal("SITE DIRECTORY AT %s WAS NOT BUILT !" % jm.build) server_jupman = "%s/server/%s" % (eld_admin, jm.filename) if os.path.exists(server_jupman): jmt.delete_tree(server_jupman, "_private/%s/server" % ld) info("Copying built website ...") shutil.copytree(jm.build, server_jupman) info("Building pdf ..") import nbformat import nbconvert from nbconvert import PDFExporter pdf_exporter = PDFExporter() #Dav dic 2019: couldn't make it work, keeps complaining about missing files #pdf_exporter.template_file = '_templates/classic.tplx # as a result we have stupid extra date and worse extra numbering in headers from nbconvert.preprocessors import ExecutePreprocessor with open(eld_notebook) as f: nb = nbformat.read(f, as_version=4) old_title = jmt._replace_title(nb, eld_notebook, "").strip('#') nb.cells = [cell for cell in nb.cells \ if not ('nbsphinx' in cell.metadata \ and cell.metadata['nbsphinx'] == 'hidden')] (body, resources) = pdf_exporter.from_notebook_node( nb, resources={'metadata': { 'name': old_title }}) if not os.path.exists(target_student): os.makedirs(target_student) #ep = ExecutePreprocessor(timeout=600, kernel_name='python3') #ep.preprocess(nb, {'metadata': {'path': './'}}) with open(target_student_pdf, 'wb') as pdf_f: #print("resources = %s" % resources) pdf_f.write(body) info("Copying exercises to " + str(target_student)) jm.copy_code(eld_solutions, target_student, copy_solutions=False) info("Creating student exercises zip: %s.zip" % target_student_zip) def mysub(fname): if fname.startswith('_private/'): return fname[len('_private/YYYY-MM-DD/student-zip/'):] else: return '/%s/%s' % (jm.get_exam_student_folder(ld), fname) jm.zip_paths([target_student] + jm.chapter_files, target_student_zip, mysub) #shutil.make_archive(target_student_zip, 'zip', target_student_zip) info("Creating server zip: %s.zip" % target_server_zip) shutil.make_archive(target_server_zip, 'zip', eld_admin + "/server") print("") info("You can now browse the website at: %s" % (os.path.abspath(eld_admin + "/server/" + jm.filename + "/html/index.html"))) print("")
def package(parser, context, args): parser.add_argument('date', help="date in format 'yyyy-mm-dd'") parser.add_argument('-t', '--site', action='store_true', help="zips the site") parser.add_argument('-r', '--server', action='store_true', help="zips the server") vs = vars(parser.parse_args(args)) ld = jmt.parse_date_str(vs['date']) zip_site = vs['site'] zip_server = vs['server'] eld_admin = '_private/' + ld eld_solutions = '_private/%s/solutions' % ld eld_notebook = '%s/exam-%s.ipynb' % (eld_solutions, ld) target_student = get_target_student(ld) target_student_pdf = '%s/%s' % (get_target_student(ld), get_exam_text_filename(ld, 'pdf')) target_student_zip = "%s/server/%s-%s-exam" % (eld_admin, jm.filename, ld ) # without '.zip' target_server_zip = "%s/%s-%s-server" % (eld_admin, jm.filename, ld ) # without '.zip' if not os.path.exists(jm.build): fatal("%s WAS NOT BUILT !" % jm.build) if not os.path.exists(eld_solutions): fatal("MISSING SOURCE SOLUTION EXERCISES: " + eld_solutions) if os.path.exists(target_student): fatal("TARGET STUDENT EXERCISES DIRECTORY ALREADY EXISTS: " + target_student) try: dir_names = os.listdir(jm.build) except Exception as e: fatal("ERROR WITH DIR %s" % jm.build, ex=e) if len(dir_names) == 0: fatal("SITE DIRECTORY AT %s WAS NOT BUILT !" % jm.build) server_jupman = "%s/server/%s" % (eld_admin, jm.filename) if os.path.exists(server_jupman): jmt.delete_tree(server_jupman, "_private/%s/server" % ld) if zip_site: info("Copying built website ...") shutil.copytree(jm.build, server_jupman) if not os.path.exists(target_student): os.makedirs(target_student) info("Copying exercises to " + str(target_student)) jm.copy_code(eld_solutions, target_student, copy_solutions=False) info("Building pdf ..") import nbformat import nbconvert from nbconvert import PDFExporter pdf_exporter = PDFExporter() #Dav dic 2019: couldn't make it work, keeps complaining about missing files #pdf_exporter.template_file = '_templates/classic.tplx # as a result we have stupid extra date and worse extra numbering in headers with open('%s/exam-%s.ipynb' % (target_student, ld)) as f: ex_nb = nbformat.read(f, as_version=4) old_title = jmt._replace_title(ex_nb, eld_notebook, "").strip('#').strip(jm.ipynb_exercises) ex_nb.cells = [cell for cell in ex_nb.cells \ if not ('nbsphinx' in cell.metadata \ and cell.metadata['nbsphinx'] == 'hidden')] (body, resources) = pdf_exporter.from_notebook_node( ex_nb, resources={'metadata': { 'name': old_title }}) with open(target_student_pdf, 'wb') as pdf_f: pdf_f.write(body) info("Creating student exercises zip: %s.zip" % target_student_zip) def remap(fname): if fname.startswith('_private/'): return fname[len('_private/YYYY-MM-DD/student-zip/'):] else: return '/%s/%s' % (jm.get_exam_student_folder(ld), fname) deglobbed_common_files, deglobbed_common_files_patterns = jm._common_files_maps( target_student_zip) jm.zip_paths([target_student] + deglobbed_common_files, target_student_zip, patterns=deglobbed_common_files_patterns, remap=remap) if zip_server: info("Creating server zip: %s.zip" % target_server_zip) shutil.make_archive(target_server_zip, 'zip', eld_admin + "/server") if zip_server and zip_site: print("") info("You can now browse the website at: %s" % (os.path.abspath(eld_admin + "/server/" + jm.filename + "/html/index.html"))) print("")
def generate_report_notebook(config, job_type, data_dir, processed_data_dir, genome_dirpath, filenames, report_filename): file_loader = FileSystemLoader(os.path.join(ROOT_DIR, 'IDSort/src')) env = Environment(loader=file_loader) report_template = env.get_template('genome_report_template.ipynb') if job_type == 'sort': # convert given genomes to h5 files, and convert given inp files to # genomes and then to h5 files inp_to_genome_config = { 'analysis': False, 'readable': False, 'create_genome': True, 'id_filename': config['process_genome']['id_filename'], 'magnets_filename': config['process_genome']['magnets_filename'], 'id_template': config['process_genome']['id_template'] } genome_to_h5_config = { 'analysis': True, 'readable': True, 'create_genome': False, 'id_filename': config['process_genome']['id_filename'], 'magnets_filename': config['process_genome']['magnets_filename'], 'id_template': config['process_genome']['id_template'] } genome_h5_filepaths = [] for filename in filenames: if filename.endswith('.genome'): filepath = os.path.join(genome_dirpath, filename) run_process_genome(genome_to_h5_config, filepath, processed_data_dir) genome_h5_filepaths.append( os.path.join(processed_data_dir, filename + '.h5')) elif filename.endswith('.inp'): filepath = os.path.join(processed_data_dir, filename) run_process_genome(inp_to_genome_config, filepath, genome_dirpath) new_genome_filename = filename + '.genome' new_genome_filepath = os.path.join(genome_dirpath, new_genome_filename) run_process_genome(genome_to_h5_config, new_genome_filepath, processed_data_dir) genome_h5_filepaths.append( os.path.join(processed_data_dir, new_genome_filename + '.h5')) elif job_type == 'shim': genome_h5_filepaths = [ os.path.join(genome_dirpath, filename) for filename in filenames ] report_output = report_template.render( job_type=job_type, genome_h5_filepaths=genome_h5_filepaths) notebook_name = 'genome_report.ipynb' notebook_path = os.path.join(data_dir, notebook_name) with open(notebook_path, 'w') as notebook: notebook.write(report_output) # create dir for genome reports genome_report_dir = 'genome_reports/' genome_report_dirpath = os.path.join(data_dir, genome_report_dir) if not os.path.exists(genome_report_dirpath): os.makedirs(genome_report_dirpath) # execute notebook with open(notebook_path, 'r') as notebook: nb = nbformat.read(notebook, as_version=4) ep = ExecutePreprocessor() ep.preprocess(nb, {'metadata': {'path': data_dir}}) pdf_exporter = PDFExporter() pdf_exporter.exclude_output_prompt = True pdf_exporter.exclude_input = True pdf_data, resources = pdf_exporter.from_notebook_node(nb) # check the name of the report to see if a specific name has been given or # not if report_filename == 'genome_report.pdf': # concatenate all genome/inp files using "_" as a separator to form the # filename report_filename = '_'.join(filenames) + '.pdf' report_filepath = os.path.join(genome_report_dirpath, report_filename) with open(report_filepath, 'wb') as report: report.write(pdf_data)