Skip to content

Commit

Permalink
various changes for error handling, imports and pydocs
Browse files Browse the repository at this point in the history
  • Loading branch information
DataBoyTX committed Jan 16, 2025
1 parent 4e76820 commit e000018
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 92 deletions.
130 changes: 98 additions & 32 deletions demos/demos_databases_apis/spanner/google_spanner_finance_graph.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"outputs": [],
"source": [
"import graphistry\n",
"import json\n",
"graphistry.__version__"
]
},
Expand All @@ -16,7 +17,7 @@
"id": "1407509d-f079-4f66-bc5f-bd93c9510d09",
"metadata": {},
"source": [
"#### Settings "
"### settings"
]
},
{
Expand All @@ -26,24 +27,42 @@
"metadata": {},
"outputs": [],
"source": [
"PROJECT_ID = \"graphistrycloud-dev\" \n",
"INSTANCE_ID = \"tcook-test-90-day-trial\" \n",
"DATABASE_ID = \"finance-graph-db\" \n",
"LIMIT = \"limit 100\" \n",
"PROJECT_ID = \"my_project\" \n",
"INSTANCE_ID = \"my_instance\" \n",
"DATABASE_ID = \"finance-graph-db\" \n",
"\n",
"# optional setting to limit the number of records returned\n",
"LIMIT_CLAUSE = \"\"\n",
"# or use: \n",
"# LIMIT_CLAUSE = \"limit 1000\" \n",
"\n",
"SPANNER_CONF = { \"project_id\": PROJECT_ID, \n",
" \"instance_id\": INSTANCE_ID, \n",
" \"database_id\": DATABASE_ID }\n",
"\n",
"print(SPANNER_CONF)"
"# print(json.dumps(SPANNER_CONF, indent=4))"
]
},
{
"cell_type": "markdown",
"id": "97649200-c7ab-4041-bb19-f7ab6363ead3",
"metadata": {},
"source": [
"#### gcloud init "
"### graphistry register and gcloud init"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8ad0f91-19b6-4407-9c19-dcbf329354fb",
"metadata": {},
"outputs": [],
"source": [
"# graphistry register \n",
"\n",
"# To specify Graphistry account & server, use:\n",
"# graphistry.register(api=3, username='...', password='...', protocol='https', server='hub.graphistry.com')\n",
"# For more options, see https://pygraphistry.readthedocs.io/en/latest/server/register.html\n"
]
},
{
Expand All @@ -53,7 +72,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Set the project id\n",
"# Set the google project id\n",
"!gcloud config set project {PROJECT_ID}\n",
"%env GOOGLE_CLOUD_PROJECT={PROJECT_ID}"
]
Expand All @@ -65,32 +84,15 @@
"metadata": {},
"outputs": [],
"source": [
"!gcloud auth application-default login"
"#!gcloud auth application-default login"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8ad0f91-19b6-4407-9c19-dcbf329354fb",
"cell_type": "markdown",
"id": "56bc01a7-76ea-44f6-b1cc-c5488c5c5922",
"metadata": {},
"outputs": [],
"source": [
"# register \n",
"\n",
"import os\n",
"os.environ[\"GRAPHISTRY_SERVER\"] = 'hub.graphistry.com'\n",
"\n",
"print(f'server = {os.getenv(\"GRAPHISTRY_SERVER\")}')\n",
"print(f'protocol = {os.getenv(\"GRAPHISTRY_PROTOCOL\")}')\n",
"print(f'username = {os.getenv(\"GRAPHISTRY_USERNAME\")}')\n",
"\n",
"graphistry.register(api=3, \n",
" protocol = \"https\", \n",
" server = os.getenv(\"GRAPHISTRY_SERVER\"),\n",
" username = os.getenv(\"GRAPHISTRY_USERNAME\"), \n",
" password = os.getenv(\"GRAPHISTRY_PASSWORD\"),\n",
" spanner_config=SPANNER_CONF\n",
" ) \n"
"### Spanner GQL Query to Graphistry Visualization"
]
},
{
Expand All @@ -101,7 +103,7 @@
"outputs": [],
"source": [
"query=f'''GRAPH FinGraph\n",
"MATCH p = (a)-[b]->(c) where 1=1 {LIMIT} return TO_JSON(p) as path'''\n",
"MATCH p = (a)-[b]->(c) where 1=1 {LIMIT_CLAUSE} return TO_JSON(p) as path'''\n",
"\n",
"g = graphistry.spanner_query(query)"
]
Expand All @@ -112,14 +114,78 @@
"id": "aeead725-928a-44fe-b5b5-630c830502e0",
"metadata": {},
"outputs": [],
"source": [
"g.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f172d3e-108a-4a18-a88e-e012988013c5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "b40b6dae-0770-4839-a392-7cf16bee65d6",
"metadata": {},
"source": [
"#### inspect contents of graphistry graph (nodes and edges): "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fed46dd-2bb5-4563-9a21-920d299ba30d",
"metadata": {},
"outputs": [],
"source": [
"len(g._nodes), len(g._edges)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad33fde2-3aae-4da0-8532-41d489c6fff1",
"metadata": {},
"outputs": [],
"source": [
"g._nodes.head(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "279f4cb9-daa1-4c2b-af69-85a5b0a771fb",
"metadata": {},
"outputs": [],
"source": [
"g._edges.head(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a60ee00-055d-4cee-b286-c5e3d6d85f09",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "11e41457-303c-4d5e-ae0e-7015db33d9f7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv - spanner-test-5",
"display_name": "venv - spanner-test-1",
"language": "python",
"name": "spanner-test-5"
"name": "spanner-test-1"
},
"language_info": {
"codemirror_mode": {
Expand Down
24 changes: 15 additions & 9 deletions graphistry/PlotterBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2272,17 +2272,24 @@ def bolt(self, driver):
res = copy.copy(self)
res._bolt_driver = to_bolt_driver(driver)
return res


# TODO(tcook): add pydocs, typing
def spanner_init(self, spanner_config):
res = copy.copy(self)

project_id = spanner_config["project_id"]
instance_id = spanner_config["instance_id"]
database_id = spanner_config["database_id"]
# TODO(tcook): throw an exception when any are missing?

# check if valid
required_keys = ["project_id", "instance_id", "database_id"]
for key in required_keys:
value = spanner_config.get(key)
if not value: # checks for None or empty values
raise ValueError(f"Missing or invalid value for required Spanner configuration: '{key}'")

res._spannergraph = SpannerGraph(res, project_id, instance_id, database_id)
print(f'DEBUG: created SpannerGraph object: {res._spannergraph} type(res): {type(res)}')
logger.debug("Created SpannerGraph object: {res._spannergraph}")
return res

def infer_labels(self):
Expand Down Expand Up @@ -2481,7 +2488,7 @@ def spanner_query(self, query: str, params: Dict[str, Any] = {}) -> Plottable:
query google spanner graph database and return Plottable with nodes and edges populated
:param query: GQL query string
:type query: Str
:returns: Plottable
:returns: Plottable with the results of GQL query as a graph
:rtype: Plottable
**Example: calling spanner_query
Expand All @@ -2496,26 +2503,25 @@ def spanner_query(self, query: str, params: Dict[str, Any] = {}) -> Plottable:
graphistry.register(..., spanner_config=SPANNER_CONF)
g = graphistry.spanner_query("Graph MyGraph\nMATCH ()-[]->()" )
g.plot()
"""

# is this needed?
from .pygraphistry import PyGraphistry
print(f'DEBUG: PlotterBase.py spanner_query()')

res = copy.copy(self)

if res._spannergraph is None:
spanner_config = PyGraphistry._config["spanner"]
if spanner_config is not None:
print(f'DEBUG: Spanner Config: {spanner_config}')
logger.debug(f"Spanner Config: {spanner_config}")
else:
print(f'DEBUG: PyGraphistry._config["spanner"] is None')
logger.debug(f'PyGraphistry._config["spanner"] is None')

res = res.spanner_init(PyGraphistry._config["spanner"])
return res._spannergraph.gql_to_graph(query)
else:
print(f'DEBUG: res._spannergraph is NOT None')
return res._spannergraph.gql_to_graph(query)


Expand Down
Loading

0 comments on commit e000018

Please sign in to comment.