pythonscript_sphinxext.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:yt_astro_analysis 作者: yt-project 项目源码 文件源码
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?
        uid = uuid.uuid4().hex[:8]
        print("")
        print(">> Contents of the script: %s" % uid)
        print(content)
        print("")

        start = time.time()
        subprocess.call(['python', 'temp.py'])
        print(">> The execution of the script %s took %f s" %
              (uid, time.time() - start))
        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]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号