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]
pythonscript_sphinxext.py 文件源码
python
阅读 16
收藏 0
点赞 0
评论 0
评论列表
文章目录