Created
November 17, 2016 05:11
-
-
Save christabor/7bc3d36f19eacc784e01b5496b36d428 to your computer and use it in GitHub Desktop.
graph_api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.restless import APIManager | |
import models | |
app = Flask(__name__) | |
app.config['DEBUG'] = True | |
manager = APIManager(app, session=models.session) | |
manager.create_api(models.Edge, methods=['GET', 'POST', 'PUT', 'DELETE']) | |
manager.create_api(models.Node, methods=['GET', 'POST', 'PUT', 'DELETE']) | |
if __name__ == '__main__': | |
models.Base.metadata.create_all(models.engine) | |
app.run(debug=True, port=5003) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm.session import sessionmaker | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import relationship, backref | |
from sqlalchemy.schema import Table | |
from sqlalchemy import ( | |
Boolean, | |
Column, | |
ForeignKey, | |
Integer, | |
String, | |
) | |
Session = sessionmaker() | |
engine = create_engine('sqlite:///graph.db', echo=True) | |
Session.configure(bind=engine) | |
session = Session() | |
Base = declarative_base() | |
association_table = Table( | |
'association', | |
Base.metadata, | |
Column('node_id', Integer, ForeignKey('node.id')), | |
Column('edge_id', Integer, ForeignKey('edge.id')) | |
) | |
class Node(Base): | |
__tablename__ = 'node' | |
id = Column(Integer, primary_key=True) | |
label = Column(String) | |
meta = Column(String(10000)) | |
edges = relationship('Edge', | |
secondary=association_table, | |
back_populates='nodes') | |
class Edge(Base): | |
__tablename__ = 'edge' | |
id = Column(Integer, primary_key=True) | |
label = Column(String) | |
meta = Column(String(10000)) | |
nodes = relationship('Node', | |
secondary=association_table, | |
back_populates='edges') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment