【问题标题】:How to create multiple PlantUML diagrams from a python script?如何从 python 脚本创建多个 PlantUML 图?
【发布时间】:2020-07-19 13:24:41
【问题描述】:

场景

大多数说明建议您可以使用 python 生成 PlantUML 图:python -m plantuml example_diagram.txt。我想创建一个 python 脚本,它在创建时会生成多个 PlantUML 图,这样我就可以立即更新一组完整的图,而无需运行多个命令。

示例文件

以下文件用于生成 PlantUML 图:

  1. example_flow.txt 内容:
' 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 --> (*)
  1. Graphviz_example.txt 内容:
' 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]

}

问题

如何从单个 python 脚本创建这些图表?

【问题讨论】:

    标签: python diagram flowchart plantuml


    【解决方案1】:

    为同一目录下的2个文件生成

    创建一个名为create_diagrams.py的python文件,内容为:

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

    然后运行它,例如在带有 python 3.6 的 anaconda 中使用命令:python create_diagrams.py

    为目录中的所有文件生成

    还可以为名为@9​​87654326@ 的目录中的所有.txt 文件生成图表:

    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}'))
    

    注意事项

    此实现需要有效的 Internet 连接,因为您要求 PlantUML 服务器为您生成图形。要在本地编译,您还可以下载 PlantUML 软件的.jar 文件,然后从 python 中调用它来进行计算。 the question of this post 中给出了更详细的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-18
      • 2011-06-26
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2010-11-01
      相关资源
      最近更新 更多