Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to convert CSAcademy Graph Editor Markup to TikZ
import json
before = """\\begin{center}
\\begin{tikzpicture}[every node/.style={draw,circle,minimum size=25pt}]
\\begin{scope}[yscale=-1]
\\draw[thick]"""
after = """;
\\end{scope}
\\end{tikzpicture}
\\end{center}"""
markup = input("Enter markup (https://csacademy.com/app/graph_editor/): ")
# Parse markup
nodes = markup.split("nodes=")[1].split("edges=")[0].strip()
edges = markup.split("edges=")[1].split("/>")[0].strip()
# JSONify
nodes = '{"nodes":' + nodes[1:].replace('label:', '"label":').replace('center:', '"center":').replace('x:', '"x":').replace('y:', '"y":')
edges = '{"edges":' + edges[1:].replace('source:', '"source":').replace('target:', '"target":')
nodes = json.loads(nodes)
edges = json.loads(edges)
# Convert to TikZ
tikz = before
for i,node in enumerate(nodes["nodes"]):
tikz += f"""
({node['center']['x']}pt,{node['center']['y']}pt)node({i}){{{node['label']}}}"""
for edge in edges["edges"]:
tikz += """
({source}) -- ({target})""".format(**edge)
tikz += after
print(tikz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment