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)

        # 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

        try:
            evaluated_text = \
                evaluate_notebook('temp.ipynb', skip_exceptions=skip_exceptions)
        except:
            # bail
            return []

        # 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
        files = glob.glob("*.png") + ['temp.py', 'temp.ipynb']
        for file in files:
            os.remove(file)

        return [nb_node]
示例#2
0
    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):
        # 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]