iPython Notebook Plantuml扩展 [英] iPython notebook plantuml extension

查看:115
本文介绍了iPython Notebook Plantuml扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在iPython Notebook中使用plantuml UML工具?由于UML图形经常在书面工作中使用,因此对我们应该有帮助.

How can we use plantuml UML tool in iPython notebook? It should helpful for us since UML figure is frequently used during paper work.

从互联网上搜索了一些Google之后,我找到了在iPython笔记本中使用渐近线,然后为iPython笔记本创建了plantuml扩展.下面是详细步骤:

After some google from internet, I have found one excellent reference for Using Asymptote in iPython notebook, then I have created a plantuml extension for iPython notebook. Below is detail steps:

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

  • Start iPython notebook from my working directory.e.g:$HOME/workshop.

# cd $HOME/workshop
# ipython notebook --pylab inline

  • 在$ HOME/workshop.e.g:plantuml.py

  • Create a extension script at $HOME/workshop.e.g: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到$主页/车间.

  • Download plantuml.jar from official website to $HOME/workshop.

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

    Create a new iPython notebook,run below cell to load extension and use the extension:

    %install_ext plantuml.py
    %reload_ext plantuml
    

  • 创建一个植物单元格以测试结果.

  • Create a plantuml cell to test the result.

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

  • 然后,plantuml中的所有内容都将在iPython笔记本中运行.

    Then,everything from plantuml will work within iPython notebook.

    一些问题是:

    • 如果在Plantuml代码中语法有错误,plantuml的错误输出将不会显示在iPython笔记本中.如果SVG生成失败,那么输出错误文本,然后将SVG文件输出到笔记本将是非常好的.
    • 扩展名使用SVG格式,不确定是否可以使用PDF或PNG格式.我也希望扩展TiKz,但pdflatex始终输出pdf文件格式.我必须使用第3方工具将其转换为SVG首先进行格式化.这很耗时.

    推荐答案

    iPython Notebook中的Plantuml UML工具是个好主意!

    Plantuml UML tool in iPython notebook is a great idea!

    除了添加jar之外,您还可以使用Web服务.您可以通过这种方式获得错误消息.

    Instead of adding the jar, you can also use the web service. You can get the error message this way.

    基于 javascript API ,我编写了一个小型python编码器,用于将字符串发送到plantUML服务器.

    Based on the javascript API, I wrote a small python encoder to send strings to the plantUML server.

    现在,扩展名看起来像这样

    Now, the extension looks like this

    
    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

    To use other image formats you can change the URL, and the image code. For example : This extension produces 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)

    这篇关于iPython Notebook Plantuml扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆