--- layout: page title: "Mind map generation with Python" description: "Step 1" date: 2019-07-15 tags: projects frc alises: [https://retrylife.ca/blog/2019/07/15/mindmap] --- While working on an assignment with [Coggle](https://coggle.it) today, I noticed an interesting option in the save menu. *Download as .mm file*. Having rarely worked with mind maps before, and only doing it online, it never occured to me that someone would have a file format for it. So I took a look. ## What is a .mm file? It turns out, a `.mm` file is just some XML describing the mind map. Here is a simple mind map: ![Simple Mind Map](/images/posts/mindmap/mindmap-simple.png) And again as a `.mm` file: ```xml ``` Neat, right? ## What can we do with it? I have not done much research about this because I wanted to work all of this out on my own. But I know one thing as a fact: working with XML sucks (especially in Python). I decided that this would be much better if I could load `.mm` files as JSON. This would allow easy manipulation and some cool projects. ## My script Like everything I do, I made a script to play with these files. It's pretty simple. First, It loads a `.mm` file, then parses it into a `list` of `xml.etree.ElementTree.Element`. ```python raw_mm = "" with open(args.file, "r") as fp: raw_mm = fp.read() fp.close() xml = ET.fromstring(raw_mm) ``` The parsed `list` is then passed into a recursive function that constructs a `dict` ```python def xmlToDict(xml): output = [] for elem in list(xml): if "TEXT" not in elem.attrib: continue name = elem.attrib['TEXT'] json_element = {"name": name} try: json_element["children"] = xmlToDict(elem) except: continue # Detect node type if json_element["children"]: json_element["type"] = "branch" else: json_element["type"] = "leaf" del json_element["children"] output.append(json_element) return output ``` Finally, the `dict` is written to a file with `json.dump` ```python json.dump(mind_map, open(args.file + ".json", "w")) ``` The whole script (with comments) can be found on my [GitHub account](https://gist.github.com/Ewpratten/0d8f7c7371380c9ca8adcfc6502ccf84#file-parser-py). ## The output Running the `.mm` file from above through the script gives: ```json [ { "name":"Master Node", "children":[ { "name":"Child branch", "children":[ { "name":"Children 1", "type":"leaf" }, { "name":"Children 2", "type":"leaf" } ], "type":"branch" } ], "type":"branch" } ] ``` ## The next step This script just translates a `.mm` file to JSON. Nothing else. Next, I want to convert this to a library, and add a JSON to `.mm` function as well. This leads into my ultimate goal for this project. I want a script that I can drop in the root of any project to build a [Gource](https://gource.io/)-style visualization of the folder structure. This will give me a way to make cool visualizations for lessons on the robotics team. On top of the folder visualization, Coggle's new flowchart feature can be used to generate graphical representations of @frc5024's codebases. This could give me an interactive overview of the work being done by our team. ### Further learning crm.org has done a great writeup of [Coggle, and some of it's features](https://crm.org/news/free-flowin-mind-maps-with-coggle). If you are looking to learn more about the tool, I recommend taking a few minute to read their post.