8

我们如何在 iPython notebook 中使用 Plantuml UML 工具?它应该对我们有所帮助,因为 UML 图形在文书工作中经常使用。

在互联网上搜索了一些谷歌之后,我找到了一个在 iPython notebook 中使用 Asymptote 的优秀参考,然后我为 iPython notebook 创建了一个 Plantuml 扩展。下面是详细步骤:

  • 从我的工作目录启动 iPython notebook。例如:$HOME/workshop。

    # cd $HOME/workshop
    # ipython notebook --pylab inline
    
  • 在 $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)
    
  • 从官网下载plantuml.jar到$HOME/workshop。

  • 创建一个新的 iPython 笔记本,在单元格下方运行以加载扩展并使用该扩展:

    %install_ext plantuml.py
    %reload_ext plantuml
    
  • 创建一个植物细胞来测试结果。

    %%plantuml figure1
    
    @startuml
    Alice -> Bob: Authentication Request
    Bob --> Alice: Authentication Response
    @enduml  
    

然后,来自 plantuml 的所有内容都将在 iPython notebook 中运行。

一些问题是:

  • 如果 Plantuml 代码中有任何语法错误,则 Plantuml 的错误输出不会显示在 iPython notebook 中。如果 SVG 生成失败会很好,然后输出错误文本,否则将 SVG 文件输出到 notebook。
  • 扩展使用的是 SVG 格式,不确定是否可以使用 PDF 或 PNG 格式。我也希望扩展 TiKz,但 pdflatex 总是输出 pdf 文件格式。我必须先使用 3rd-party 工具将其转换为 SVG 格式。这有点费时。
4

2 回答 2

6

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)

要使用其他图像格式,您可以更改 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)
于 2014-08-11T03:49:20.513 回答
3

有一个 PlantUML 细胞魔法包。请参考iPlantUML@PyPi

安装后(pip install iplantuml)按照包介绍,你可以在jupyterlab中创建plantUML代码如下:

先导入包,

import iplantuml

使用细胞魔法:

%%plantuml 

@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml 

然后在单元格输出中显示一个图表:

在此处输入图像描述

于 2020-05-01T03:46:57.120 回答