{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pegraph-evcxr allows you to display petgraph graphs in jupyter\n",
"---------------------------------------------------------------\n",
"\n",
"[github](https://github.com/timthelion/petgraph-evcxr) [author Timothy Hobbs](https://timothy.hobbs.cz) [crates.io](https://crates.io/crates/petgraph-evcxr)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
":dep petgraph = \"*\"\n",
":dep petgraph-evcxr = \"*\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "Error",
"evalue": "unresolved import `petgraph_evcxr::draw_graph_with_attr_getters`",
"output_type": "error",
"traceback": [
"use petgraph_evcxr::{draw_graph, draw_graph_with_attr_getters, draw_dot};",
"\u001b[91m ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m \u001b[94mno `draw_graph_with_attr_getters` in the root\u001b[0m",
"unresolved import `petgraph_evcxr::draw_graph_with_attr_getters`"
]
}
],
"source": [
"extern crate petgraph;\n",
"use petgraph::graph::Graph;\n",
"use petgraph::dot::Dot;\n",
"use petgraph_evcxr::{draw_graph, draw_graph_with_attr_getters, draw_dot};"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You draw graphs using the `draw_graph` function."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "Error",
"evalue": "failed to resolve: use of undeclared type or module `Graph`",
"output_type": "error",
"traceback": [
"let mut g : Graph<&str, &str> = Graph::new();",
"\u001b[91m ^^^^^\u001b[0m \u001b[94muse of undeclared type or module `Graph`\u001b[0m",
"failed to resolve: use of undeclared type or module `Graph`"
]
},
{
"ename": "Error",
"evalue": "cannot find type `Graph` in this scope",
"output_type": "error",
"traceback": [
"let mut g : Graph<&str, &str> = Graph::new();",
"\u001b[91m ^^^^^\u001b[0m \u001b[94mnot found in this scope\u001b[0m",
"cannot find type `Graph` in this scope"
]
},
{
"ename": "Error",
"evalue": "cannot find function `draw_graph` in this scope",
"output_type": "error",
"traceback": [
"draw_graph(&g);",
"\u001b[91m^^^^^^^^^^\u001b[0m \u001b[94mnot found in this scope\u001b[0m",
"cannot find function `draw_graph` in this scope"
]
}
],
"source": [
"let mut g : Graph<&str, &str> = Graph::new();\n",
"let a = g.add_node(\"a\");\n",
"let b = g.add_node(\"b\");\n",
"g.add_edge(a, b, \"a to b\");\n",
"draw_graph(&g);"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "Error",
"evalue": "cannot find function `draw_graph_with_attr_getters` in this scope",
"output_type": "error",
"traceback": [
"draw_graph_with_attr_getters(",
"\u001b[91m^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m \u001b[94mnot found in this scope\u001b[0m",
"cannot find function `draw_graph_with_attr_getters` in this scope"
]
},
{
"ename": "Error",
"evalue": "cannot find value `g` in this scope",
"output_type": "error",
"traceback": [
" &g,",
"\u001b[91m ^\u001b[0m \u001b[94mnot found in this scope\u001b[0m",
"cannot find value `g` in this scope"
]
},
{
"ename": "Error",
"evalue": "expected value, found module `self`",
"output_type": "error",
"traceback": [
" (if nr.id() == self.state {",
"\u001b[91m ^^^^\u001b[0m \u001b[94m`self` value is a keyword only available in methods with a `self` parameter\u001b[0m",
" &|_, nr| {",
" (if nr.id() == self.state {",
" \"shape = circle style = filled fillcolor = red\"",
" } else {",
" \"shape = circle\"",
" }).to_string()",
" },",
"\u001b[91m \u001b[0m \u001b[94mthis function doesn't have a `self` parameter\u001b[0m",
"expected value, found module `self`"
]
}
],
"source": [
"draw_graph_with_attr_getters(\n",
" &g,\n",
" &[],\n",
" &|_, _| \"\".to_string(),\n",
" &|_, nr| {\n",
" (if nr.id() == self.state {\n",
" \"shape = circle style = filled fillcolor = red\"\n",
" } else {\n",
" \"shape = circle\"\n",
" }).to_string()\n",
" },\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also draw dot code directly using the `draw_dot` function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "Error",
"evalue": "cannot find function `draw_dot` in this scope",
"output_type": "error",
"traceback": [
"draw_dot(dot);",
"\u001b[91m^^^^^^^^\u001b[0m \u001b[94mnot found in this scope\u001b[0m",
"cannot find function `draw_dot` in this scope"
]
}
],
"source": [
"let dot = \"digraph {\\\n",
" 0 [label=\\\"a\\\"]\\\n",
" 1 [label=\\\"b\\\"]\\\n",
" 0 -> 1 [label=\\\"a → b\\\"]\\\n",
"}\";\n",
"draw_dot(dot);"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Rust",
"language": "rust",
"name": "rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "Rust",
"pygment_lexer": "rust",
"version": ""
}
},
"nbformat": 4,
"nbformat_minor": 2
}