0

I am trying to display Neo4J graph data in my Django template. For querying I am using neomodel, and Cytoscape.js for graph visualization. I am able to convert nodes data in the form of JSON that cytoscape.js requires, but for edges I don't have idea how to do it.

This are my Models

class Player(StructuredNode):
    name = StringProperty(required=True)
    age = IntegerProperty(required=True)
    height = IntegerProperty(required=True)
    number = IntegerProperty(required=True)
    weight = IntegerProperty(required=True)
    team = RelationshipTo('Team', 'PLAYS_FOR', model=PlayerTeamRel)

    @property
    def serialize(self):
        return {
            'data': {
                'id': self.id,
                'name': self.name,
                'age': self.age,
                'height': self.height,
                'number': self.number,
                'weight': self.weight,
            }
        }

class Team(StructuredNode):
    name = StringProperty(required=True)
    players = RelationshipFrom('Player', 'PLAYS_FOR')
    coach = RelationshipFrom('Coach', 'COACHES')

    @property
    def serialize(self):
        return {
            'data': {
                'id': self.id,
                'name': self.name,
                'players': self.players,
                'coach': self.coach
            }
        }

class Coach(StructuredNode):
    name = StringProperty(required=True)
    coach = RelationshipTo('Team', 'COACHES')

    @property
    def serialize(self):
        return {
            'data': {
                'id': self.id,
                'name': self.name,
                'coach': self.coach
            }
        }

serialize() method is used get JSON format.

That relationship attribute(team) is of type ZeroToOne when I try to print it in the console. I want the JSON in the below format, specifically relationship(edges) data as I am able to serialize and display node data.

{ data: { id: 'a' } },
  { data: { id: 'b' } },
  { data: { id: 'c' } },
  { data: { id: 'd' } },
  { data: { id: 'e' } },
  { data: { id: 'f' } },
  // edges
  {
    data: {
      id: 'ab',
      source: 'a',
      target: 'b'
    }
  },
  {
    data: {
      id: 'cd',
      source: 'c',
      target: 'd'
    }
  },
  {
    data: {
      id: 'ef',
      source: 'e',
      target: 'f'
    }
  },
  {
    data: {
      id: 'ac',
      source: 'a',
      target: 'c'
    }
  },
  {
    data: {
      id: 'be',
      source: 'b',
      target: 'e'
    }
  }

I didn't find any proper integration between cytoscape.js and neomodel data, couldn't find a way to retrieve relationship data either. Can anyone suggest me a way to do this.

shrikant02
  • 15
  • 6

0 Answers0