Most instructions suggest you can generate PlantUML diagrams with python using: python -m plantuml example_diagram.txt
. I wanted to create a python script that generates multiple PlantUML diagrams when it is created, such that I can instantly update a complete set of diagrams, without having to run multiple commands.
The following files are used to generate the PlantUML graphs:
example_flow.txt
with content:' http://tonyballantyne.com/graphs.html#orgheadline19
' http://graphviz.org/doc/info/shapes.html
' Indicate the direction of the flowchart
left to right direction
' Give a block the variable name 'first' and define starting point as (*)
(*) --> "The first block" as first
first --> "A" as A
' Give the block a variable name s.t. you can just use its variable name in the next blocks
--> "D" as D
first --> "B" as B
--> D
first --> "C" as C
--> D
' Define end point as (*)
D --> (*)
Graphviz_example.txt
with content:' http://tonyballantyne.com/graphs.html#orgheadline19
' http://graphviz.org/doc/info/shapes.html
digraph summary{
// initialize the variable blocks
start [label="Start with a Node"]
next [label="Choose your shape", shape=box]
warning [label="Don't go overboard", color=Blue, fontcolor=Red,fontsize=24,style=filled, fillcolor=green,shape=octagon]
end [label="Draw your graph!", shape=box, style=filled, fillcolor=yellow]
// Indicate the direction of the flowchart
rankdir=LR; // Rank direction left to right
// Create the connections
start->next
start->warning
next->end [label="Getting Better...", fontcolor=darkblue]
}
How can I create these diagrams from a single python script?
Create a python file named create_diagrams.py
with content:
from plantuml import PlantUML
from os.path import abspath
# create a server object to call for your computations
server = PlantUML(url='http://www.plantuml.com/plantuml/img/',
basic_auth={},
form_auth={}, http_opts={}, request_opts={})
# Send and compile your diagram files to/with the PlantUML server
server.processes_file(abspath('./example_flow.txt'))
server.processes_file(abspath('./Graphviz_example.txt'))
And run it, for example in anaconda with python 3.6 with command: python create_diagrams.py
.
One can also generate the diagrams for all .txt
files in a directory named Diagrams
with:
from plantuml import PlantUML
import os
from os.path import abspath
server = PlantUML(url='http://www.plantuml.com/plantuml/img/',
basic_auth={},
form_auth={}, http_opts={}, request_opts={})
# create subfolder name
diagram_dir = "./Diagrams"
# loop through all .txt files in the subfolder
for file in os.listdir(diagram_dir):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
# Call the PlantUML server on the .txt file
server.processes_file(abspath(f'./Diagrams/{filename}'))
This implementation requires an active internet connection because your asking the PlantUML server to do the graph generation for you. To compile locally, you can also download the .jar
file of the PlantUML software and call that from python to do your computation. A more detailed example of this is given in the question of this post.