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 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:
Start iPython notebook from my working directory.e.g:$HOME/workshop.
# cd $HOME/workshop # ipython notebook --pylab inline
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)
Download plantuml.jar from official website to $HOME/workshop.
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
Then,everything from plantuml will work within 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.
- 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.