skip to navigation
skip to content

graph-alchemy 0.1.0

Basic Node/Edge model for SQLAlchemy

Graph node-edge relationships in SQLAlchemy (and soon more orms) + networkx integration.

The Goal

Set up a generalized abstraction/interface for a graph that can be used across various platforms and within various frameworks that is easy and simple to use, extensible, and that easily hooks into other graphing tools (like gephi networkx, etc.)

What’s here

A basic set of node/edge abstractions + many-to-one relationships for a graph represented in SQL with SQLAlchemy

Using this package

Documentation is available at

It’s very simple to use (and examples are to come). But for now the best thing to do is to go read the docs on SQLAlchemy

A very minimal example (using an SQLite database):

from graphalchemy.sqlmodels import create_base_classes, sqlite_connect
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
Node, Edge = create_base_classes(NodeClass="Node", EdgeClass="Edge", Base=Base)
engine, session = sqlite_connect("database.db", metadata=Base.metadata)

# be sure to use unicode!!
node1 = Node(label=u"First node!")
node2 = Node(label=u"Second node!")
node3 = Node(label=u"Third node!")
edge1 = Edge.connect_nodes(node1, node2)
edge2 = Edge.connect_nodes(node1, node3)

session.add_all([node1, node2, node3, edge1, edge2])

# now we can graph it

import networkx as nx
G = nx.Graph()
G.add_edges_from([edge1, edge2])

# now we can draw this! (if you had pylab, matplotlib, etc)

And you’d get a picture that looked something like this (clearly, we haven’t added all the traits and such in, but you get the idea):

Now, obviously this is a pretty minimal example, but it shows how you can take advantage of the power of SQL joins, queries, etc, but also very easily

What’s going to be here

  1. networkx integration
  2. testing for multiple sql databases and adapters
  3. abstractions for Google App Engine, mongoalchemy, and possibly Django ORM
  4. adapter between networkx and web service requests (maybe?)

Testing coverage

Basic test suite that gets 100% line coverage for SQLAlchemy models and base models (still missing a test for Flask-SQLAlchemy). I’ve only run it on SQLite so far, but presumably it should work with other SQL databases just fine (since it uses SQLAlchemy’s declarative base)

File Type Py Version Uploaded on Size
graph-alchemy-0.1.0.tar.gz (md5) Source 2012-08-08 11KB