Skip to main content

Basic Node/Edge model for SQLAlchemy

Project description

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)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graph-alchemy-0.1.0.tar.gz (12.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page