iPython笔记本plantuml扩展

iPython笔记本plantuml扩展,python,uml,ipython-notebook,tikz,plantuml,Python,Uml,Ipython Notebook,Tikz,Plantuml,如何在iPython笔记本中使用plantuml工具?这对我们来说应该是有帮助的,因为UML图在书面工作中经常使用 在从internet上搜索了一些google之后,我找到了一个非常好的参考,然后我为iPython笔记本创建了一个plantuml扩展。具体步骤如下: 从我的工作目录启动iPython笔记本。例如:$HOME/workshop # cd $HOME/workshop # ipython notebook --pylab inline 在$HOME/workshop中创建扩展脚

如何在iPython笔记本中使用plantuml工具?这对我们来说应该是有帮助的,因为UML图在书面工作中经常使用

在从internet上搜索了一些google之后,我找到了一个非常好的参考,然后我为iPython笔记本创建了一个plantuml扩展。具体步骤如下:

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

    # cd $HOME/workshop
    # ipython notebook --pylab inline
    
  • 在$HOME/workshop中创建扩展脚本。例如: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单元来测试结果

    %%plantuml figure1
    
    @startuml
    Alice -> Bob: Authentication Request
    Bob --> Alice: Authentication Response
    @enduml  
    
然后,plantuml中的所有内容都将在iPython笔记本中工作

有些问题是:

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

iPython笔记本中的Plantuml工具是个好主意

您还可以使用web服务,而不是添加jar。您可以通过这种方式获得错误消息

基于,我编写了一个小型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)

有一个PlantUML单元魔术包。请参阅

安装后(
pip install iplantuml
)遵循软件包介绍,您可以在jupyterlab中创建plantUML代码,如下所示:

先导入包

import iplantuml
使用细胞魔法:

%%plantuml 

@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml 
然后在单元格输出中显示图表,如下所示:


最新的iPython在iPython/html/static/custom/custom.cssGood上有一个文档,但这意味着我们总是需要网络连接,因为一切都是在plantuml服务器上完成的!仅供参考。这是个好主意!你的github链接给出了一个404。在jupyter实验室不是这样吗(