iPython 笔记本植物扩展

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 中运行。

一些问题是:

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

Luc*_*eau 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)
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)