Skip to content

Instantly share code, notes, and snippets.

@dlaertius
Last active March 15, 2018 02:07
Show Gist options
  • Save dlaertius/5ec354207c3196b274083461db48089e to your computer and use it in GitHub Desktop.
Save dlaertius/5ec354207c3196b274083461db48089e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import graphviz as gv
from graphviz import Digraph
#Just if you want do add in another way more tech.
def addNodes(graph, nodes):
for n in nodes:
if isinstance(n,tuple):
graph,node(n[0], **n[1])
else:
graph.node(n)
return graph
def add_edges(graph, edges):
for e in edges:
if isinstance(e[0], tuple):
graph.edge(*e[0], **e[1])
else:
graph.edge(*e)
return graph
#Applying styles in graphs.
def apply_styles(graph, styles):
graph.graph_attr.update(
('graph' in styles and styles['graph']) or {}
)
graph.node_attr.update(
('nodes' in styles and styles['nodes']) or {}
)
graph.edge_attr.update(
('edges' in styles and styles['edges']) or {}
)
return graph
styles = {
'graph': {
#'label': 'Grafo de Interações para o Modelo NK.',
'fontsize': '16',
'fontcolor': 'black',
'bgcolor': '#ffffff',
'rankdir': 'BT',
},
'nodes': {
'fontname': 'Helvetica',
'shape': 'circle',
'fontcolor': 'white',
'color': 'white',
'style': 'filled',
'fillcolor': '#006699',
},
'edges': {
'style': 'dashed',
'color': 'black',
'arrowhead': 'open',
'fontname': 'Courier',
'fontsize': '12',
'fontcolor': 'white',
}
}
dot = Digraph(format='pdf')
dot.node('1')
with open("teste2.txt",'r+') as uf:
lines = uf.readlines()
file_data = ''.join(lines)
file_data = file_data.replace(",,","")
file_data = file_data.split("#")
del(file_data[0])
recipient_node = 1
for node in file_data:
vertx = node.split("|")
for directed_node in vertx[1]: #sempre o segundo valor do array que é o grafo.
dot.edge(recipient_node,directed_node)
recipient_node += 1
dot = apply_styles(dot, styles)
dot.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment