Mind Map with Python

I always wanted a program to generate mind maps. This is the first attempt to do it with Python.

This is an example of the output we will get.

Install Graphviz (eventually with chocolatey)

To make a mind map with Python you need to install Graphviz at this web page.

You could use also chocolatey to install Graphviz.

Chocoloatey installs Graphviz. Go to chocolatey and install it.

Watch the video to see how I did it.

Install pydot

Now we’re ready to install pydot in the cmd (command line) or powershell:

pip install pydot

Ready for the code

You should also go in the environmental variable settings (watch the video) and insert the path to Graphviz in it.

The path is also to paste into the code. In fact after you imported pydot (and os), you need to add this line of code:

import pydot
import os
os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin'

Rememeber that the environ path is the one on my computer. You gotta put the one on your that could be the same of mine, but it is not sure, you could have installed somewhere else.

Now we are ready.

Risultati immagini per gif ready

Create a Dot istance (let’s call it graph)

graph = pydot.Dot(graph_type="digraph", rankdir="UD")

The graph type could have been also graph. The UD means Up down, for the vertical oriented maps. If you want a left to right map, use LR instead.

Now we create the root node, the master, the topic of whatever you want to call the keyword that is the start of the map. Let’s say the topic is Pydot!

root = "Pydot"

Now let’s create adn Edge object with the name of the root and his child as arguments. We will add the edge to graph with the add_edge method. Then we save the image of the map and show it.

import pydot
import os
os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin'

graph = pydot.Dot(graph_type="graph", rankdir="UD")

root = "Pydot"
edge = pydot.Edge(root, "How to install it")
graph.add_edge(edge)

graph.write_png("Hello.png")
os.startfile("Hello.png")

We could stop here and save an image of the map:

Create 2 children of a child

Now, to add two children to How to install it you can add

edge = pydot.Edge("How to install it", "Way 1")
graph.add_edge(edge)

edge = pydot.Edge("How to install it", "Way 2")
graph.add_edge(edge)

The whole code being:

import pydot
import os
os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin'

graph = pydot.Dot(graph_type="graph", rankdir="UD")

root = "Pydot"
edge = pydot.Edge(root, "How to install it")
graph.add_edge(edge)

edge = pydot.Edge("How to install it", "Way 1")
graph.add_edge(edge)

edge = pydot.Edge("How to install it", "Way 2")
graph.add_edge(edge)

graph.write_png("Hello.png")
os.startfile("Hello.png")

The output would be:

As you can see, it is easy, but a bit tedious. If you watch the video you will see me trying to abstract a little bit the code, so that it is less annoying to add the childrens… there will be for sure better ways, but I leave them to your imagination or to future articles…

A nice example (the one you see in the video)

import pydot
import os

os.environ["PATH"] += os.pathsep + "C:\\Program Files (x86)\\Graphviz2.38\\bin\\"

graph = pydot.Dot(graph_type="graph", rankdir="UD")

def add_edge(root, child):
    edge = pydot.Edge(root, child)
    graph.add_edge(edge)

def add(root, children):
    if children.splitlines()[0] == "":
        children = children.splitlines()[1:]
    else:
        children = children.splitlines()
    for child in children:
        add_edge(root, child)

# ======================= 1 Python\Children
add("Pydot",
"""Install
Examples
Info
""")
# ===================== 2 Pydot\Examples\Children
add("Examples", """
Example 1
Example 2
""")
# ======================= level 2: Pydot\Info\Children
add("Info", """
Author
""")
# ====================== Lev. 3 Pydot\Info\author\hello

add("Author", "Hello")

graph.write_png("map.png")
os.startfile("map.png")

And now…

The live coding video of Pydot to make Mind Maps and Concepts Maps with Python


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.