Python Generated Sequence Diagrams


120 points | by lipeltgm 11 days ago


  • bobthepanda 10 days ago


    What text->diagram tools do you guys prefer? I am partial to PlantUML:

  • motohagiography 10 days ago

    As a security guy, this python seq diagram view would be super valuable as essentially a markup language for BAN logic for documenting encryption and key management schemes.

    Oddly, I've never heard this called "security protocol notation" before, but apparently that's a thing as well.

    • jianyuan 10 days ago
      • rgoulter 10 days ago

        While it's good to see the results you can get from familiarity with imagemagick drawing commands and a small script,

        for more sophisticated scenarios (or customisation), you can still get "rendered from text on my computer" using tools like PlantUML. (Or GraphViz for graphs, etc.).

        Embedding the resulting diagrams in the text editor (like Emacs' org-babel allows for) makes for a nice diagramming experience.

        • lipeltgm 8 days ago

          Agreed and for a few years now that's what I have done. But for detailed, log/debug driven sequence diagrams that often required parsing a log, generating a compliant text-input to the tools followed by submitting to the tool and exporting the graphic. This 30-minute project is a step in the direction to fully automate the process.

          • smitty1e 10 days ago

            That's an excellent point.

            While we should reach for prior art, something like this bit of python is good for the hip pocket for those moments when PlantUML might not support some zany requirement.

          • karlicoss 10 days ago

            Nice! I did something similar for my personal infrastructure map [0]. I tried different tools, and each of them was too limited so landed on graphviz + a python DSL [1] to generate the diagram.

            It turned out to be extremely convenient:

            - python makes it easy to spot errors/typos (you can use mypy even before running the genrator)

            - keyword arguments and dictionaries map naturally to node/edge properties

            - you can reuse boilerplate by using functions, variables and control structures

            - you can always inject graphviz string directly as a last resort hacking measure

            - thanks to python's dynamic features, you can even give the nodes/clusters same names as their corresponding python objects, which results in less boilerplate and makes easy to debug



            • ris 10 days ago

              When needing to do something similar (with our pseudo-microservice project's increasingly incomprehensible JSON logs) I also wanted the diagram to be interactive so it's possible to "dig" when showing larger amounts of data, so I learned d3.js and was very pleasantly surprised by the power of the abstraction it introduces, despite the unavoidable javascript.


              (ended up having to turn it into a custom WebExtension just so it could integrate "easily" with kibana by monkeypatching & intercepting its xhr requests, let's not get into that...)

              • cgranier 10 days ago

                Wow... I'm really enjoying reading about promnesia, roam integration, etc. Thanks for sharing.

              • wegs 10 days ago


                What I really wish for is integration, or at least discovery.

                I run into posts like this all the time, see neat things, and then can never find them again.

                I like all-in-one libraries like pylab/matplotlib where I know where to look. The corollary is that if this could be folded into something, it'd probably see more use. But I'm not sure what.

                Or perhaps an alternative might be to have all libraries like this categorized in some way. I'm not quite sure how, though.

                • seesawtron 9 days ago

                  Is anyone else struggling to run it? I keep getting the error at line 67:

                  ValueError: invalid literal for int() with base 10: ''

                  Changed it to float before int casting but still the same error.

                  • lipeltgm 8 days ago

                    What value are you encountering for list 'D' when you get the error? As you can see from the code, not really production-grade coding and certainly not heavily tested, proof-of-concept more than anything else.

                    • seesawtron 8 days ago

                      I tried running it with jupyterlab. I had issues with executing imagemagick commands which is why the temp.jpg file didn't get created causing the error (list D is empty).

                  • th5 9 days ago


                    Not text to diagram, but creating diagrams in text that I check-in to the comments at the top of my code.

                    • bbrik 9 days ago

                      Very cool. Why all those semi-colons in your code?

                      • lipeltgm 8 days ago

                        I doubt it'll be a popular response, but despite Python not requiring semicolons while many do (or most languages I use), I simply tend to end all statements with semicolons as a personal preference.