A graphing library for SQLAlchemy based on pydot (current 1.2.2) and Graphviz.
This code is based on a usage recipe by Ants Aasma on SQLAlchemy's wiki and subsequent work by Florian Schulze (and others) in the form of sqlalchemy_schemadisplay which is available on both GitHub and PyPI.
Because I know you're in a hurry, here's an example to generate a graph of your declarative SQLAlchemy model:
from sqla_graphs import ModelGrapher
from your.project.model import Base
grapher = ModelGrapher(
show_operations=True,
style={'node_table_header': {'bgcolor': '#000088'}})
graph = grapher.graph(Base.__subclasses__())
graph.write_png('model_graph.png')
You can also feed it the tables from the declarative base's metadata or tables reflected from a connection. That looks like this:
from sqla_graphs import TableGrapher
from your.project.model import Base
grapher = TableGrapher(
style={'node_table_header': {'bgcolor': '#000080'}})
graph = grapher.graph(tables=Base.metadata.tables.values())
graph.write_png('table_graph.png')
Included in the examples directory are two scripts that create model and table graphs based on an included example model, in case you don't have your own to bring to the table.
Lacking a PyPI distribution, the installation steps are below. They clone the repository, set up and activate a virtualenv and install the package into this fresh environment:
git clone https://github.com/edelooff/sqla-graphs.git
virtualenv env
source env/bin/activate
cd sqla-graphs
pip install -e .
The goal of this project is to continue and extend the usefulness of the original recipe. In more practical terms the design goals are these:
- Maintain compatibility with current versions of SQLAlchemy and pydot
- Follow PEP8 style in implementation and spirit
- Be easily extensible and configurable by others
Feature requests are welcome, though this mainly exists to scrath my own itch, so development may be slow. Pull requests are even better, of course.
Scripts that allow for creation of graphs based on a module:class argument or database connection string.