iPython notebook plantuml extension

前端 未结 2 1807
眼角桃花
眼角桃花 2021-02-01 11:15

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

After some google from internet, I

2条回答
  •  攒了一身酷
    2021-02-01 11:32

    Plantuml UML tool in iPython notebook is a great idea!

    Instead of adding the jar, you can also use the web service. 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.

    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. 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)

提交回复
热议问题