11.2 GraphVisualizationを使用した基本的な例

JSONファイルのグラフ構成を使用して、Jupyter Notebookのグラフ・ビジュアライゼーションを設定および表示する方法について学習します。

sample_data.jsonファイルの次のサンプル・グラフ構成について考えてみます:

{
  "vertices": [
    {
      "id": "ACCOUNTS{\"ID\":1}",
      "properties": {
        "ID": 1,
        "NAME": "User1",
        "BALANCE": 10000
      },
      "labels": ["ACCOUNTS"]
    },
    {
      "id": "ACCOUNTS{\"ID\":2}",
      "properties": {
        "ID": 2,
        "NAME": "User2",
        "BALANCE": 20000
      },
      "labels": ["ACCOUNTS"]
    },
    {
      "id": "ACCOUNTS{\"ID\":3}",
      "properties": {
        "ID": 3,
        "NAME": "User3",
        "BALANCE": 30000
      },
      "labels": ["ACCOUNTS"]
    },
    {
      "id": "ACCOUNTS{\"ID\":4}",
      "properties": {
        "ID": 4,
        "NAME": "User4",
        "BALANCE": 40000
      },
      "labels": ["ACCOUNTS"]
    },
    {
      "id": "ACCOUNTS{\"ID\":5}",
      "properties": {
        "ID": 5,
        "NAME": "User5",
        "BALANCE": 50000
      },
      "labels": ["ACCOUNTS"]
    }
  ],
  "edges": [
    {
      "id": "TRANSFERS{\"TXN_ID\":1}",
      "source": "ACCOUNTS{\"ID\":1}",
      "target": "ACCOUNTS{\"ID\":2}",
      "properties": {
        "TXN_ID": 1,
        "AMOUNT": 1500
      },
      "labels": ["TRANSFERS"]
    },
    {
      "id": "TRANSFERS{\"TXN_ID\":2}",
      "source": "ACCOUNTS{\"ID\":2}",
      "target": "ACCOUNTS{\"ID\":4}",
      "properties": {
        "TXN_ID": 2,
        "AMOUNT": 700
      },
      "labels": ["TRANSFERS"]
    },
    {
      "id": "TRANSFERS{\"TXN_ID\":3}",
      "source": "ACCOUNTS{\"ID\":3}",
      "target": "ACCOUNTS{\"ID\":5}",
      "properties": {
        "TXN_ID": 3,
        "AMOUNT": 1200
      },
      "labels": ["TRANSFERS"]
    }
  ],
  "numResults": 10,
  "graphOwner": "GRAPHUSER",
  "graphName": "BANK_GRAPH",
  "isLastResultSet": true
}

それから、次に示すように、GraphVisualizationを使用して、Jupyter Notebookで基本的なグラフ・ビジュアライゼーションを作成できます:

from oraclegraph import GraphVisualization as Graph
import json

with open("sample_data.json", "r") as f:
    data = json.load(f)

# Option 1: Create widget and assign data
graph = Graph()
graph.data = data

# Option 2: Create widget with data in constructor
graph = Graph(data=data)

# Display height in pixels
graph.height = 600

# Display option 1
display(graph)

# Display option 2
graph

前述のコードに示すように、ノートブックのセルにgraphと入力すると、ビジュアライゼーションが表示されます。graph.heightを変更すれば、ビジュアル領域を調整できます。このコードでは、2つのグラフ・ビジュアライゼーションがoption1に1つとoption2に1つ生成されます:

図11-1 基本的なグラフ・ビジュアライゼーション



より大きなグラフ構成には、GitHubjupyter-notebooks/examples/dataフォルダにあるsimple_data.jsonファイルを使用することもできます。