def from_notebook_node(self, nb, resources=None, **kw): latex, resources = super(PDFExporter, self).from_notebook_node(nb, resources=resources, **kw) with TemporaryWorkingDirectory() as td: notebook_name = "notebook" tex_file = self.writer.write(latex, resources, notebook_name=notebook_name) self.log.info("Building PDF") rc = self.run_latex(tex_file) if not rc: rc = self.run_bib(tex_file) if not rc: rc = self.run_latex(tex_file) pdf_file = notebook_name + '.pdf' if not os.path.isfile(pdf_file): raise RuntimeError("PDF creating failed") self.log.info('PDF successfully created') with open(pdf_file, 'rb') as f: pdf_data = f.read() # convert output extension to pdf # the writer above required it to be tex resources['output_extension'] = '.pdf' return pdf_data, resources
def create_temp_cwd(self, copy_filenames=None): temp_dir = TemporaryWorkingDirectory() #Copy the files if requested. if copy_filenames is not None: self.copy_files_to(copy_filenames, dest=temp_dir.name) #Return directory handler return temp_dir
def test_port_bind_failure_raises(request): cfg = Config() with TemporaryWorkingDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyKernelApp(config=cfg, connection_file=cf) request.addfinalizer(app.close) app.initialize() with patch.object(app, '_try_bind_socket') as mock_try_bind: mock_try_bind.side_effect = zmq.ZMQError(-100, "fails for unknown error types") with pytest.raises(zmq.ZMQError): app.init_sockets() assert mock_try_bind.call_count == 1
def test_find_connection_file_local(): with TemporaryWorkingDirectory() as d: cf = 'test.json' abs_cf = os.path.abspath(cf) with open(cf, 'w') as f: f.write('{}') for query in ( 'test.json', 'test', abs_cf, os.path.join('.', 'test.json'), ): assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
def test_find_connection_file_relative(): with TemporaryWorkingDirectory() as d: cf = 'test.json' os.mkdir('subdir') cf = os.path.join('subdir', 'test.json') abs_cf = os.path.abspath(cf) with open(cf, 'w') as f: f.write('{}') for query in ( os.path.join('.', 'subdir', 'test.json'), os.path.join('subdir', 'test.json'), abs_cf, ): assert connect.find_connection_file(query, path=['.', jupyter_runtime_dir()]) == abs_cf
def from_notebook_node(self, nb, resources=None, **kw): """ Generate a PDF from a given parsed notebook node """ output, resources = super(BrowserPDFExporter, self).from_notebook_node( nb, resources=resources, **kw ) with TemporaryWorkingDirectory() as td: for path, res in resources.get("outputs", {}).items(): dest = os.path.join(td, os.path.basename(path)) shutil.copyfile(path, dest) index_html = os.path.join(td, "index.html") with open(index_html, "w+") as fp: fp.write(output) ipynb = "notebook.ipynb" with open(os.path.join(td, ipynb), "w") as fp: nbformat.write(nb, fp) self.log.info("Building PDF...") subprocess.check_call([ sys.executable, "-m", "nbbrowserpdf.exporters.pdf_capture", td ] + self.pdf_capture_args()) pdf_file = "notebook.pdf" if not os.path.isfile(pdf_file): raise IOError("PDF creating failed") self.log.info("PDF successfully created") with open(pdf_file, 'rb') as f: pdf_data = f.read() # convert output extension to pdf # the writer above required it to be tex resources['output_extension'] = '.pdf' # clear figure outputs, extracted by pdf export, # so we don't claim to be a multi-file export. resources.pop('outputs', None) return pdf_data, resources
def test_get_connection_file(): cfg = Config() with TemporaryWorkingDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyKernelApp(config=cfg, connection_file=cf) app.initialize() profile_cf = os.path.join(app.connection_dir, cf) assert profile_cf == app.abs_connection_file with open(profile_cf, 'w') as f: f.write("{}") assert os.path.exists(profile_cf) assert connect.get_connection_file(app) == profile_cf app.connection_file = cf assert connect.get_connection_file(app) == profile_cf
def from_notebook_node(self, nb, resources=None, **kw): latex, resources = super(PDFExporter, self).from_notebook_node(nb, resources=resources, **kw) # set texinputs directory, so that local files will be found if resources and resources.get('metadata', {}).get('path'): self.texinputs = resources['metadata']['path'] else: self.texinputs = os.getcwd() self._captured_outputs = [] with TemporaryWorkingDirectory() as td: notebook_name = 'notebook' tex_file = self.writer.write(latex, resources, notebook_name=notebook_name) self.log.info("Building PDF") rc = self.run_latex(tex_file) if rc: rc = self.run_bib(tex_file) if rc: rc = self.run_latex(tex_file) pdf_file = notebook_name + '.pdf' if not os.path.isfile(pdf_file): raise LatexFailed('\n'.join(self._captured_output)) self.log.info('PDF successfully created') with open(pdf_file, 'rb') as f: pdf_data = f.read() # convert output extension to pdf # the writer above required it to be tex resources['output_extension'] = '.pdf' # clear figure outputs, extracted by latex export, # so we don't claim to be a multi-file export. resources.pop('outputs', None) return pdf_data, resources
def test_port_bind_failure_recovery(request): try: errno.WSAEADDRINUSE except AttributeError: # Fake windows address in-use code p = patch.object(errno, 'WSAEADDRINUSE', 12345, create=True) p.start() request.addfinalizer(p.stop) cfg = Config() with TemporaryWorkingDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyKernelApp(config=cfg, connection_file=cf) request.addfinalizer(app.close) app.initialize() with patch.object(app, '_try_bind_socket') as mock_try_bind: mock_try_bind.side_effect = [ zmq.ZMQError(errno.EADDRINUSE, "fails for non-bind unix"), zmq.ZMQError(errno.WSAEADDRINUSE, "fails for non-bind windows") ] + [0] * 100 # Shouldn't raise anything as retries will kick in app.init_sockets()