简体   繁体   English

iPython 笔记本植物扩展

[英]iPython notebook plantuml extension

How can we use plantuml UML tool in iPython notebook?我们如何在 iPython notebook 中使用 plantuml UML 工具? It should helpful for us since UML figure is frequently used during paper work.它应该对我们有帮助,因为在文书工作中经常使用 UML 图。

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.在互联网上搜索了一些谷歌之后,我找到了一个在 iPython notebook 中使用渐近线的优秀参考,然后我为 iPython notebook 创建了一个 Plantuml 扩展。 Below is detail steps:下面是详细步骤:

  • Start iPython notebook from my working directory.eg:$HOME/workshop.从我的工作目录启动 iPython notebook。例如:$HOME/workshop。

     # cd $HOME/workshop # ipython notebook --pylab inline
  • Create a extension script at $HOME/workshop.eg:plantuml.py在 $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)
  • Download plantuml.jar from official website to $HOME/workshop.官网下载plantuml.jar到$HOME/workshop。

  • Create a new iPython notebook,run below cell to load extension and use the extension:创建一个新的 iPython 笔记本,在单元格下方运行以加载扩展并使用扩展:

     %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

Then,everything from plantuml will work within iPython notebook.然后,plantuml 中的所有内容都将在 iPython notebook 中运行。

Some questions are:一些问题是:

  • The error output of plantuml is NOT displayed in iPython notebook if any syntax wrong in plantuml code.it will be great if the SVG generation failure, then output the error text, othwise output the SVG file to notebook.如果 Plantuml 代码中有任何语法错误,则 Plantuml 的错误输出不会在 iPython notebook 中显示。如果 SVG 生成失败,则输出错误文本,否则将 SVG 文件输出到 notebook 中。
  • The extension is using SVG format, Not sure if it's possible to use PDF or PNG format.I wish to extension TiKz also but pdflatex always output pdf file format.I have to use a 3rd-party tool to covert it to SVG format firstly.it's a bit time consuming.扩展使用的是 SVG 格式,不确定是否可以使用 PDF 或 PNG 格式。我也希望扩展 TiKz,但 pdflatex 总是输出 pdf 文件格式。我必须首先使用第三方工具将其转换为 SVG 格式。这有点耗时。

Plantuml UML tool in iPython notebook is a great idea! iPython notebook 中的 Plantuml UML 工具是个好主意!

Instead of adding the jar, you can also use the web service.除了添加 jar,您还可以使用 Web 服务。 You can get the error message this way.您可以通过这种方式获取错误消息。

Based on the javascript API , I wrote a small python encoder to send strings to the plantUML server.基于javascript API ,我写了一个小的python编码器来将字符串发送到plantUML服务器。

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)

To use other image formats you can change the URL, and the image code.要使用其他图像格式,您可以更改 URL 和图像代码。 For example : This extension produces png例如:此扩展生成 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)

There is a PlantUML cell magic package.有一个 PlantUML 单元魔法包。 Please refer to iPlantUML@PyPi请参考iPlantUML@PyPi

After installation ( pip install iplantuml ) follow package introduction, you can create plantUML code in jupyterlab as follow:安装后( pip install iplantuml )按照包介绍,你可以在jupyterlab中创建plantUML代码如下:

Import package first,先导入包,

import iplantuml

use cell magic:使用细胞魔法:

%%plantuml 

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

then show a diagram in cell output as:然后在单元格输出中显示一个图表:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM