luc*_*928 8 python uml tikz ipython-notebook plantuml
我们如何在 iPython notebook 中使用 plantuml UML 工具?它应该对我们有帮助,因为在文书工作中经常使用 UML 图。
在从互联网上搜索了一些谷歌之后,我找到了一个在 iPython notebook 中使用渐近线的优秀参考,然后我为 iPython notebook 创建了一个 Plantuml 扩展。下面是详细步骤:
从我的工作目录启动 iPython notebook。例如:$HOME/workshop。
# cd $HOME/workshop
# ipython notebook --pylab inline
Run Code Online (Sandbox Code Playgroud)在 $HOME/workshop.eg:plantuml.py 创建扩展脚本
"""
An Plantuml extension for generating UML figures from within ipython notebook.
"""
import os
from IPython.core.magic import magics_class, cell_magic, Magics
from IPython.display import Image, SVG
@magics_class
class Plantuml(Magics):
@cell_magic
def plantuml(self, line, cell):
"""Generate and display a figure using Plantuml.
Usage:
%java -jar plantuml.jar -tsvg filname
"""
self.filename = line
self.code = cell
with open(self.filename + ".plt", "w") as file:
file.write(self.code)
os.system("java -jar plantuml.jar -tsvg %s.plt" % self.filename)
return SVG(filename=self.filename+".svg")
def load_ipython_extension(ipython):
ipython.register_magics(Plantuml)
Run Code Online (Sandbox Code Playgroud)从官网下载plantuml.jar到$HOME/workshop。
创建一个新的 iPython 笔记本,在单元格下方运行以加载扩展并使用扩展:
%install_ext plantuml.py
%reload_ext plantuml
Run Code Online (Sandbox Code Playgroud)创建一个植物单元格来测试结果。
%%plantuml figure1
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
Run Code Online (Sandbox Code Playgroud)然后,plantuml 中的所有内容都将在 iPython notebook 中运行。
一些问题是:
iPython notebook 中的 Plantuml UML 工具是个好主意!
除了添加 jar,您还可以使用 Web 服务。您可以通过这种方式获取错误消息。
基于javascript API,我写了一个小的python编码器来将字符串发送到plantUML服务器。
现在,扩展看起来像这样
import urllib
import plantumlencoder
from IPython.core.magic import magics_class, cell_magic, Magics
from IPython.display import Image, SVG
@magics_class
class Plantuml(Magics):
@cell_magic
def plantuml(self, line, cell):
self.filename = line
self.code = ""
for line in cell.split('\n'):
newline = line.strip()
if newline:
self.code += newline + '\n'
uri = "http://www.plantuml.com/plantuml/svg/" + plantumlencoder.compress(self.code)
urllib.urlretrieve(uri, self.filename)
return SVG(filename=self.filename)
def load_ipython_extension(ipython):
ipython.register_magics(Plantuml)
Run Code Online (Sandbox Code Playgroud)
要使用其他图像格式,您可以更改 URL 和图像代码。例如:此扩展生成 png
import urllib
import plantumlencoder
from IPython.core.magic import magics_class, cell_magic, Magics
from IPython.display import Image, PNG
@magics_class
class Plantuml(Magics):
@cell_magic
def plantuml(self, line, cell):
self.filename = line
self.code = ""
for line in cell.split('\n'):
newline = line.strip()
if newline:
self.code += newline + '\n'
uri = "http://www.plantuml.com/plantuml/png/" + plantumlencoder.compress(self.code)
urllib.urlretrieve(uri, self.filename)
return PNG(filename=self.filename)
def load_ipython_extension(ipython):
ipython.register_magics(Plantuml)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4170 次 |
| 最近记录: |