1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Used for displaying petgraph graphs in jupyter using the evcxr rust engine.
extern crate petgraph;
use std::fmt::{self};
use std::io::{Write};
use std::process::{Command, Stdio};

use base64;

use crate::petgraph::visit::{GraphProp};
use crate::petgraph::visit::{IntoEdgeReferences, IntoNodeReferences, NodeIndexable};
use petgraph::dot::{Dot, Config};

/// Draw the contents of a dot file.
/// ```rust
/// # use petgraph_evcxr::draw_dot;
/// let dot = "digraph {\
/// 0 [label=\"a\"]\
/// 1 [label=\"b\"]\
/// 0 -> 1 [label=\"a → b\"]\
/// }";
/// draw_dot(dot);
/// ```
pub fn draw_dot<D>(dot: D)
where
    D: fmt::Display,
{
    println!("EVCXR_BEGIN_CONTENT image/png");
    let mut child = Command::new("dot")
        .args(&["-Tpng"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Error running graphviz dot is graphviz installed?");
    child
        .stdin
        .as_mut()
        .unwrap()
        .write_fmt(format_args!("{}", dot))
        .expect("Writing failed.");
    let output = child
        .wait_with_output()
        .expect("Failed to run dot is graphviz installed?");
    println!("{}", base64::encode(&output.stdout[..]));
    println!("EVCXR_END_CONTENT");
}

/// Draw a petgraph graph
/// ```rust
/// # extern crate petgraph;
/// # use petgraph::graph::Graph;
/// # use petgraph::dot::Dot;
/// # use petgraph_evcxr::draw_graph;
/// let mut g : Graph<&str, &str> = Graph::new();
/// let a = g.add_node("a");
/// let b = g.add_node("b");
/// g.add_edge(a, b, "a to b");
/// draw_graph(&g);
/// ```
pub fn draw_graph<G>(g: G)
where
    G: NodeIndexable + IntoNodeReferences + IntoEdgeReferences,
    G: GraphProp,
    G::NodeWeight: fmt::Display,
    G::EdgeWeight: fmt::Display,
{
    draw_dot(Dot::new(g));
}


pub fn draw_graph_with_attr_getters<'a, G>(
    g: G,
    config: &'a [Config],
    get_edge_attributes: &'a dyn Fn(G, G::EdgeRef) -> String,
    get_node_attributes: &'a dyn Fn(G, G::NodeRef) -> String,
)
where
    G: NodeIndexable + IntoNodeReferences + IntoEdgeReferences,
    G: GraphProp,
    G::NodeWeight: fmt::Display,
    G::EdgeWeight: fmt::Display,
{
    draw_dot(Dot::with_attr_getters(g, config, get_edge_attributes, get_node_attributes));
}