2.4 Quick Start: Using the Python Client as a Module

This section describes how to use the Python client as a module in Python applications.

Embedded Server

You can use the python client as a module as illustrated in the following example.

Note:

For this mode, the Python client and the Graph Server RPM package must be installed on the same machine.
import os
os.environ["PGX_CLASSPATH"] = "/opt/oracle/graph/lib/*"
 
import pypgx
 
session = pypgx.get_session()
graph = session.create_graph_builder().add_edge(1, 2).add_edge(2, 3).build("my_graph")
analyst = session.create_analyst()
analyst.pagerank(graph)
rs = session.query_pgql("SELECT id(x), x.pagerank FROM MATCH (x) ON my_graph")
rs.print()

To execute, save the above program into a file named program.py and run the following command.

python3 program.py

You will see the following output:

+-----------------------------+
| id(x) | pagerank            |
+-----------------------------+
| 1     | 0.05000000000000001 |
| 2     | 0.09250000000000003 |
| 3     | 0.12862500000000002 |
+-----------------------------+

See Converting PGQL result set into pandas dataframe for more details on converting a PGQL result set into pandas dataframe.

Note:

To view the complete set of available Python APIs, see OPG4PY Python API Reference.

Remote Server

For this mode, all you need is the Python client to be installed. In your Python program, you must authenticate with the remote server before you can create a session as illustrated in the following example.

Note:

Replace the base_url, username, and password with values to match your environment details.
import pypgx as pgx
import opg4py.graph_server as graph_server
  
base_url = "https://localhost:7007"
username = "scott"
password = "<password-for-scott>"
 
instance = graph_server.get_instance(base_url, username, password)
session = instance.create_session("python_pgx_client")
print(session)

To execute, save the above program into a file named program.py and run the following command:

python3 program.py

After successful login, you'll see the following message indicating a PGX session was created:

PgxSession(id: 0bdd4828-c3cc-4cef-92c8-0fcd105416f0, name: python_pgx_client)

Note:

To view the complete set of available Python APIs, see OPG4PY Python API Reference.