Mind map generation with Python

Step 1

2019-07-15 18:38:00 +0000


While working on an assignment with Coggle 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

And again as a .mm file:

<map version="0.9.0">
    <node TEXT="Master Node" FOLDED="false" POSITION="right" ID="5d2d02b1a315dd0879f48c1c" X_COGGLE_POSX="0" X_COGGLE_POSY="0">
        <edge COLOR="#b4b4b4"/>
        <font NAME="Helvetica" SIZE="17"/>
        <node TEXT="Child branch" FOLDED="false" POSITION="right" ID="f72704969525d2a0333dd635">
            <edge COLOR="#7aa3e5"/>
            <font NAME="Helvetica" SIZE="15"/>
            <node TEXT="Children 1" FOLDED="false" POSITION="right" ID="c83826af506cae6e55761d5c">
                <edge COLOR="#7ea7e5"/>
                <font NAME="Helvetica" SIZE="13"/>
            </node>
            <node TEXT="Children 2" FOLDED="false" POSITION="right" ID="47723a4d0fb766863f70d204">
                <edge COLOR="#82aae7"/>
                <font NAME="Helvetica" SIZE="13"/>
            </node>
        </node>
    </node>
</map>

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.

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

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

json.dump(mind_map, open(args.file + ".json", "w"))

The whole script (with comments) can be found on my GitHub account.

The output

Running the .mm file from above through the script gives:

[
    {
        "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-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. If you are looking to learn more about the tool, I recommend taking a few minute to read their post.