Skip to content

Instantly share code, notes, and snippets.

@dtranhuusm
Created September 28, 2018 20:01
Show Gist options
  • Save dtranhuusm/b967f9a10280966ef84170df4a55e100 to your computer and use it in GitHub Desktop.
Save dtranhuusm/b967f9a10280966ef84170df4a55e100 to your computer and use it in GitHub Desktop.
Jupyter notebook magic for plantuml using web service
"""Save the file where you can import it in your jupyter notebook.
In a cell, execute:
import plantumlmagic
%reload_ext plantumlmagic
Then you can use the magic in another cell:
%%plantumlmagic localhost:1234
@startuml
Bob -> Alice : hello
@enduml
Assuming you have a plantuml service running on localhost port 1234.
If you don't provide localhost:1234 it will use the service on plantuml.com.
It also requires plantuml package.
You can easily setup a plantuml service using a docker container:
docker run -d --name plantuml -p 1234:8080 plantuml/plantuml-server:tomcat
"""
import plantuml
from IPython.core.magic import magics_class, cell_magic, Magics
from IPython.display import Image, SVG
@magics_class
class Plantumlmagic(Magics):
@cell_magic
def plantumlmagic(self, line, cell):
if line:
pu = plantuml.PlantUML(url="http://{0}/svg/".format(line))
else:
pu = plantuml.PlantUML(url="http://www.plantuml.com/plantuml/svg/")
self.code = ""
for line in cell.split('\n'):
newline = line.strip()
if newline:
self.code += newline + '\n'
return SVG(pu.processes(self.code))
def load_ipython_extension(ipython):
ipython.register_magics(Plantumlmagic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment