def run(self): # check if raw html is supported if not self.state.document.settings.raw_enabled: raise self.warning('"%s" directive disabled.' % self.name) cwd = os.getcwd() tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) rst_file = self.state_machine.document.attributes['source'] rst_dir = os.path.abspath(os.path.dirname(rst_file)) image_dir, image_rel_dir = make_image_dir(setup, rst_dir) # Construct notebook from cell content content = "\n".join(self.content) with open("temp.py", "w") as f: f.write(content) convert_to_ipynb('temp.py', 'temp.ipynb') skip_exceptions = 'skip_exceptions' in self.options evaluated_text, resources = evaluate_notebook( 'temp.ipynb', skip_exceptions=skip_exceptions) evaluated_text = write_notebook_output(resources, image_dir, image_rel_dir, evaluated_text) # create notebook node attributes = {'format': 'html', 'source': 'nb_path'} nb_node = notebook_node('', evaluated_text, **attributes) (nb_node.source, nb_node.line) = \ self.state_machine.get_source_and_line(self.lineno) # clean up os.chdir(cwd) shutil.rmtree(tmpdir, True) return [nb_node]
def run(self): cwd = os.getcwd() tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) rst_file = self.state_machine.document.attributes['source'] rst_dir = os.path.abspath(os.path.dirname(rst_file)) image_dir, image_rel_dir = make_image_dir(setup, rst_dir) # Construct script from cell content content = "\n".join(self.content) with open("temp.py", "w") as f: f.write(content) # Use sphinx logger? print("") print(content) print("") subprocess.call(['python', 'temp.py']) text = '' for im in sorted(glob.glob("*.png")): text += get_image_tag(im, image_dir, image_rel_dir) code = content literal = nodes.literal_block(code, code) literal['language'] = 'python' attributes = {'format': 'html'} img_node = nodes.raw('', text, **attributes) # clean up os.chdir(cwd) shutil.rmtree(tmpdir, True) return [literal, img_node]
def run(self): # check if raw html is supported if not self.state.document.settings.raw_enabled: raise self.warning('"%s" directive disabled.' % self.name) cwd = os.getcwd() tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) rst_file = self.state_machine.document.attributes["source"] rst_dir = os.path.abspath(os.path.dirname(rst_file)) image_dir, image_rel_dir = make_image_dir(setup, rst_dir) # Construct notebook from cell content content = "\n".join(self.content) with open("temp.py", "w") as f: f.write(content) convert_to_ipynb("temp.py", "temp.ipynb") skip_exceptions = "skip_exceptions" in self.options evaluated_text, resources = evaluate_notebook("temp.ipynb", skip_exceptions=skip_exceptions) evaluated_text = write_notebook_output(resources, image_dir, image_rel_dir, evaluated_text) # create notebook node attributes = {"format": "html", "source": "nb_path"} nb_node = notebook_node("", evaluated_text, **attributes) (nb_node.source, nb_node.line) = self.state_machine.get_source_and_line(self.lineno) # clean up os.chdir(cwd) shutil.rmtree(tmpdir, True) return [nb_node]