spanner-graph-notebook 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. spanner_graph_notebook-1.0.0/CONTRIBUTING.md +46 -0
  2. spanner_graph_notebook-1.0.0/LICENSE +202 -0
  3. spanner_graph_notebook-1.0.0/MANIFEST.in +8 -0
  4. spanner_graph_notebook-1.0.0/PKG-INFO +169 -0
  5. spanner_graph_notebook-1.0.0/README.md +156 -0
  6. spanner_graph_notebook-1.0.0/assets/authentication.png +0 -0
  7. spanner_graph_notebook-1.0.0/assets/full_viz.png +0 -0
  8. spanner_graph_notebook-1.0.0/assets/hero.png +0 -0
  9. spanner_graph_notebook-1.0.0/assets/hero_with_properties.png +0 -0
  10. spanner_graph_notebook-1.0.0/assets/jupyter-spin-up.png +0 -0
  11. spanner_graph_notebook-1.0.0/assets/load_ext.png +0 -0
  12. spanner_graph_notebook-1.0.0/assets/mock_data_result.png +0 -0
  13. spanner_graph_notebook-1.0.0/assets/new_notebook.png +0 -0
  14. spanner_graph_notebook-1.0.0/assets/notebook_package_load.png +0 -0
  15. spanner_graph_notebook-1.0.0/assets/query_graph.png +0 -0
  16. spanner_graph_notebook-1.0.0/assets/sample_jupyter.png +0 -0
  17. spanner_graph_notebook-1.0.0/sample.ipynb +118 -0
  18. spanner_graph_notebook-1.0.0/setup.cfg +4 -0
  19. spanner_graph_notebook-1.0.0/setup.py +58 -0
  20. spanner_graph_notebook-1.0.0/spanner_graph_notebook.egg-info/PKG-INFO +169 -0
  21. spanner_graph_notebook-1.0.0/spanner_graph_notebook.egg-info/SOURCES.txt +88 -0
  22. spanner_graph_notebook-1.0.0/spanner_graph_notebook.egg-info/dependency_links.txt +1 -0
  23. spanner_graph_notebook-1.0.0/spanner_graph_notebook.egg-info/requires.txt +6 -0
  24. spanner_graph_notebook-1.0.0/spanner_graph_notebook.egg-info/top_level.txt +2 -0
  25. spanner_graph_notebook-1.0.0/spanner_graphs/__init__.py +19 -0
  26. spanner_graph_notebook-1.0.0/spanner_graphs/conversion.py +299 -0
  27. spanner_graph_notebook-1.0.0/spanner_graphs/database.py +199 -0
  28. spanner_graph_notebook-1.0.0/spanner_graphs/graph_entities.py +405 -0
  29. spanner_graph_notebook-1.0.0/spanner_graphs/graph_mock_data.csv +51 -0
  30. spanner_graph_notebook-1.0.0/spanner_graphs/graph_mock_schema.json +681 -0
  31. spanner_graph_notebook-1.0.0/spanner_graphs/magics.py +211 -0
  32. spanner_graph_notebook-1.0.0/spanner_graphs/schema_manager.py +60 -0
  33. spanner_graph_notebook-1.0.0/templates/assets/images/graph-bg.svg +4 -0
  34. spanner_graph_notebook-1.0.0/templates/spanner-graph/models/edge.js +77 -0
  35. spanner_graph_notebook-1.0.0/templates/spanner-graph/models/graph-object.js +64 -0
  36. spanner_graph_notebook-1.0.0/templates/spanner-graph/models/node.js +77 -0
  37. spanner_graph_notebook-1.0.0/templates/spanner-graph/models/schema.js +327 -0
  38. spanner_graph_notebook-1.0.0/templates/spanner-graph/spanner-config.js +304 -0
  39. spanner_graph_notebook-1.0.0/templates/spanner-graph/spanner-store.js +382 -0
  40. spanner_graph_notebook-1.0.0/templates/spanner-graph/visualization/spanner-forcegraph.js +1380 -0
  41. spanner_graph_notebook-1.0.0/templates/spanner-graph/visualization/spanner-sidebar.js +904 -0
  42. spanner_graph_notebook-1.0.0/templates/template-spannergraph.html +210 -0
  43. spanner_graph_notebook-1.0.0/tests/__init__.py +13 -0
  44. spanner_graph_notebook-1.0.0/tests/conversion_test.py +163 -0
  45. spanner_graph_notebook-1.0.0/tests/database_test.py +62 -0
  46. spanner_graph_notebook-1.0.0/tests/graph_entities_test.py +124 -0
  47. spanner_graph_notebook-1.0.0/tests/schema_manager_test.py +115 -0
  48. spanner_graph_notebook-1.0.0/tests/test_notebook.json +23 -0
@@ -0,0 +1,46 @@
1
+ # How to contribute
2
+
3
+ This project has limited capacity for reviewing and accepting open-source
4
+ contributions. Please see "Contribution process" below.
5
+
6
+ ## Before you begin
7
+
8
+ ### Sign our Contributor License Agreement
9
+
10
+ Contributions to this project must be accompanied by a
11
+ [Contributor License Agreement](https://cla.developers.google.com/about) (CLA).
12
+ You (or your employer) retain the copyright to your contribution; this simply
13
+ gives us permission to use and redistribute your contributions as part of the
14
+ project.
15
+
16
+ If you or your current employer have already signed the Google CLA (even if it
17
+ was for a different project), you probably don't need to do it again.
18
+
19
+ Visit <https://cla.developers.google.com/> to see your current agreements or to
20
+ sign a new one.
21
+
22
+ ### Review our community guidelines
23
+
24
+ This project follows
25
+ [Google's Open Source Community Guidelines](https://opensource.google/conduct/).
26
+
27
+ ## Contribution process
28
+
29
+ ### Design reviews
30
+
31
+ Prior to developing any change, please file a
32
+ [GitHub Issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/about-issues)
33
+ proposing the change. The
34
+ proposal should describe the proposed new API as well as any major technical
35
+ considerations for the underlying design. Additional information or changes to
36
+ the design may be requested based on the review.
37
+
38
+ In general, the goal of this project is to solve one collection of use cases
39
+ really well. We're looking for contributions that support that goal.
40
+
41
+ ### Code reviews
42
+
43
+ All submissions, including submissions by project members, require review. We
44
+ use GitHub pull requests for this purpose. Consult
45
+ [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
46
+ information on using pull requests.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ recursive-include templates *
2
+ recursive-include assets *
3
+ include README.md
4
+ include sample.ipynb
5
+ include CONTRIBUTING.md
6
+ include spanner_graphs/*.csv
7
+ include spanner_graphs/*.json
8
+ include tests/*.json
@@ -0,0 +1,169 @@
1
+ Metadata-Version: 2.1
2
+ Name: spanner-graph-notebook
3
+ Version: 1.0.0
4
+ Summary: Visually query Spanner Graph data in notebooks.
5
+ Description-Content-Type: text/markdown
6
+ License-File: LICENSE
7
+ Requires-Dist: networkx
8
+ Requires-Dist: numpy
9
+ Requires-Dist: google-cloud-spanner
10
+ Requires-Dist: ipython
11
+ Requires-Dist: ipywidgets
12
+ Requires-Dist: notebook
13
+
14
+ # Spanner Graph Notebook: Explore Your Data Visually
15
+
16
+
17
+ The Spanner Graph Notebook tool provides an efficient way for you to query [Spanner Graph](https://cloud.google.com/spanner/docs/graph/overview) visually in a notebook environment (e.g. [Jupyter Notebook](https://jupyter.org/) and [Google Colab](https://colab.google/)). Using [GQL](https://cloud.google.com/spanner/docs/reference/standard-sql/graph-intro) query syntax, you can can extract graph insights and relationship patterns, including node and edge properties, neighbor analysis, schema metadata, and diverse layout topologies.
18
+
19
+ <img src="./assets/full_viz.png" width="800"/>
20
+
21
+ ## Install dependencies
22
+
23
+ All dependencies below must be installed before using the tool.
24
+
25
+ ### Install the `gcloud` CLI
26
+
27
+ `gcloud` CLI is needed for authentication. Follow the installation [manual](https://cloud.google.com/sdk/docs/install) to install.
28
+
29
+ ### Install dependencies
30
+
31
+ Download this package and install its dependencies. Python dependencies should ideally be installed in a managed Python environment using tools like `conda` or `virtualenv`.
32
+
33
+ When in the root directory of the package, follow the command below to create a managed Python environment (example based on `virtualenv`) and install dependencies.
34
+
35
+ ```shell
36
+ # Create the virtualenv `viz`.
37
+ python3 -m venv viz
38
+
39
+ # Activate the virtualenv.
40
+ source viz/bin/activate
41
+ ```
42
+
43
+ ```shell
44
+ # Install dependencies.
45
+ pip install .
46
+ ```
47
+
48
+ ## Using
49
+
50
+ ### Launch notebook and follow steps in `sample.ipynb`
51
+
52
+ When in the root directory of the package, run `jupyter notebook` to launch Jupyter Notebook.
53
+
54
+ ```shell
55
+ jupyter notebook
56
+ ```
57
+
58
+ As Jupyter local server runs, it will open up a web portal. You can open open the `sample.ipynb` to step through an example.
59
+
60
+ <img src="./assets/sample_jupyter.png" width="600"/>
61
+
62
+ You must run `%load_ext spanner_graphs` to load this package. `sample.ipynb` contains this cell already.
63
+
64
+ <img src="./assets/load_ext.png" width="600"/>
65
+
66
+ ### `%%spanner_graph` magic command
67
+
68
+ This package provides a `%%spanner_graph` magic command, which visualizes graph data in the notebook output cell.
69
+
70
+ ### Query and visualize a local dataset with `--mock` flag
71
+
72
+ Using the `--mock` flag for `%%spanner_graph`, you can visualize local graph datasets without connecting to a live Cloud Spanner database. The mock
73
+ dataset renders a network of `person` and `account` nodes, along with edges in between. There is an inserted cell in `sample.ipynb` for the code below.
74
+
75
+ ```
76
+ %%spanner_graph --mock
77
+
78
+ ```
79
+
80
+ <img src="./assets/mock_data_result.png" width="600"/>
81
+
82
+
83
+ ### Visualize graph queries from your Spanner Graph database
84
+
85
+ #### Authentication
86
+
87
+ To connect to your Spanner database, you need to first authenticate via `gcloud auth`. `gcloud` CLI is needed to use the tool. Follow the installation [manual](https://cloud.google.com/sdk/docs/install) to install.
88
+
89
+ `sample.ipynb` has inserted a code cell for authentication below. Running this opens a new browser tab for the Google Cloud authentication process.
90
+
91
+ ```shell
92
+ !gcloud auth application-default login
93
+ ```
94
+
95
+ #### Query and visualize Spanner Graph data
96
+
97
+ Using the `%%spanner_graph` magic command, you can visualize graph query results from your Spanner database.
98
+
99
+ The magic command needs to be used with your GCP resource info and a query string:
100
+ - a GCP project id for `--project` option
101
+ - a Spanner instance id for `--instance` option
102
+ - a Spanner Graph database id for `--database` option
103
+ - a [GQL](https://cloud.google.com/spanner/docs/graph/queries-overview) query string
104
+
105
+ <img src="./assets/hero_with_properties.png" width="800">
106
+
107
+ ## Query Requirements
108
+
109
+ ### Use `TO_JSON` function to return graph elements
110
+
111
+ Graph queries **must use** `TO_JSON` function in the `RETURN` statement to visualize paths, nodes and edges. We recommend visualizing **paths** for data completeness and ease of use.
112
+
113
+ ```sql
114
+ 👍 Good example returning a path as JSON.
115
+
116
+
117
+ GRAPH FinGraph
118
+ MATCH query_path = (person:Person {id: 5})-[owns:Owns]->(accnt:Account)
119
+ RETURN TO_JSON(query_path) AS path_json
120
+ ```
121
+
122
+ ```sql
123
+ 👍 Good example returning a path as JSON in a multiple-hop query.
124
+
125
+ GRAPH FinGraph
126
+ MATCH query_path = (src:Account {id: 9})-[edge]->{1,3}(dst:Account)
127
+ RETURN TO_JSON(query_path) as path_json
128
+ ```
129
+
130
+ ```sql
131
+ 👍 Good example returning multiple paths as JSON.
132
+
133
+ GRAPH FinGraph
134
+ MATCH path_1 = (person:Person {id: 5})-[:Owns]->(accnt:Account),
135
+ path_2 = (src:Account {id: 9})-[:Transfers]->(dst:Account)
136
+ RETURN TO_JSON(path_1) as path_1,
137
+ TO_JSON(path_2) as path_2
138
+ ```
139
+
140
+ ```
141
+ 👎 Anti-example returning nodes properties rather than JSON format graph elements.
142
+ Scalar results other than JSON format graph elements cannot be visualized.
143
+
144
+ GRAPH FinGraph
145
+ MATCH (person:Person {id: 5})-[owns:Owns]->(accnt:Account)
146
+ RETURN person.id AS person,
147
+ owns.amount AS owns,
148
+ accnt.id AS accnt;
149
+ ```
150
+
151
+ ```sql
152
+ 👎 Anti-example returning each node and edges in JSON format verbosely. This will
153
+ work but not as easy as returning a path directly.
154
+
155
+ GRAPH FinGraph
156
+ MATCH (person:Person {id: 5})-[owns:Owns]->(accnt:Account)
157
+ RETURN TO_JSON(person) AS person_json,
158
+ TO_JSON(owns) AS owns_json,
159
+ TO_JSON(accnt) AS accnt_json,
160
+ ```
161
+
162
+ ## Testing changes
163
+
164
+ After adding new changes, please run unit and integration tests with the command below:
165
+
166
+
167
+ ```shell
168
+ cd spanner_graphs && python -m unittest discover -s tests -p "*_test.py"
169
+ ```
@@ -0,0 +1,156 @@
1
+ # Spanner Graph Notebook: Explore Your Data Visually
2
+
3
+
4
+ The Spanner Graph Notebook tool provides an efficient way for you to query [Spanner Graph](https://cloud.google.com/spanner/docs/graph/overview) visually in a notebook environment (e.g. [Jupyter Notebook](https://jupyter.org/) and [Google Colab](https://colab.google/)). Using [GQL](https://cloud.google.com/spanner/docs/reference/standard-sql/graph-intro) query syntax, you can can extract graph insights and relationship patterns, including node and edge properties, neighbor analysis, schema metadata, and diverse layout topologies.
5
+
6
+ <img src="./assets/full_viz.png" width="800"/>
7
+
8
+ ## Install dependencies
9
+
10
+ All dependencies below must be installed before using the tool.
11
+
12
+ ### Install the `gcloud` CLI
13
+
14
+ `gcloud` CLI is needed for authentication. Follow the installation [manual](https://cloud.google.com/sdk/docs/install) to install.
15
+
16
+ ### Install dependencies
17
+
18
+ Download this package and install its dependencies. Python dependencies should ideally be installed in a managed Python environment using tools like `conda` or `virtualenv`.
19
+
20
+ When in the root directory of the package, follow the command below to create a managed Python environment (example based on `virtualenv`) and install dependencies.
21
+
22
+ ```shell
23
+ # Create the virtualenv `viz`.
24
+ python3 -m venv viz
25
+
26
+ # Activate the virtualenv.
27
+ source viz/bin/activate
28
+ ```
29
+
30
+ ```shell
31
+ # Install dependencies.
32
+ pip install .
33
+ ```
34
+
35
+ ## Using
36
+
37
+ ### Launch notebook and follow steps in `sample.ipynb`
38
+
39
+ When in the root directory of the package, run `jupyter notebook` to launch Jupyter Notebook.
40
+
41
+ ```shell
42
+ jupyter notebook
43
+ ```
44
+
45
+ As Jupyter local server runs, it will open up a web portal. You can open open the `sample.ipynb` to step through an example.
46
+
47
+ <img src="./assets/sample_jupyter.png" width="600"/>
48
+
49
+ You must run `%load_ext spanner_graphs` to load this package. `sample.ipynb` contains this cell already.
50
+
51
+ <img src="./assets/load_ext.png" width="600"/>
52
+
53
+ ### `%%spanner_graph` magic command
54
+
55
+ This package provides a `%%spanner_graph` magic command, which visualizes graph data in the notebook output cell.
56
+
57
+ ### Query and visualize a local dataset with `--mock` flag
58
+
59
+ Using the `--mock` flag for `%%spanner_graph`, you can visualize local graph datasets without connecting to a live Cloud Spanner database. The mock
60
+ dataset renders a network of `person` and `account` nodes, along with edges in between. There is an inserted cell in `sample.ipynb` for the code below.
61
+
62
+ ```
63
+ %%spanner_graph --mock
64
+
65
+ ```
66
+
67
+ <img src="./assets/mock_data_result.png" width="600"/>
68
+
69
+
70
+ ### Visualize graph queries from your Spanner Graph database
71
+
72
+ #### Authentication
73
+
74
+ To connect to your Spanner database, you need to first authenticate via `gcloud auth`. `gcloud` CLI is needed to use the tool. Follow the installation [manual](https://cloud.google.com/sdk/docs/install) to install.
75
+
76
+ `sample.ipynb` has inserted a code cell for authentication below. Running this opens a new browser tab for the Google Cloud authentication process.
77
+
78
+ ```shell
79
+ !gcloud auth application-default login
80
+ ```
81
+
82
+ #### Query and visualize Spanner Graph data
83
+
84
+ Using the `%%spanner_graph` magic command, you can visualize graph query results from your Spanner database.
85
+
86
+ The magic command needs to be used with your GCP resource info and a query string:
87
+ - a GCP project id for `--project` option
88
+ - a Spanner instance id for `--instance` option
89
+ - a Spanner Graph database id for `--database` option
90
+ - a [GQL](https://cloud.google.com/spanner/docs/graph/queries-overview) query string
91
+
92
+ <img src="./assets/hero_with_properties.png" width="800">
93
+
94
+ ## Query Requirements
95
+
96
+ ### Use `TO_JSON` function to return graph elements
97
+
98
+ Graph queries **must use** `TO_JSON` function in the `RETURN` statement to visualize paths, nodes and edges. We recommend visualizing **paths** for data completeness and ease of use.
99
+
100
+ ```sql
101
+ 👍 Good example returning a path as JSON.
102
+
103
+
104
+ GRAPH FinGraph
105
+ MATCH query_path = (person:Person {id: 5})-[owns:Owns]->(accnt:Account)
106
+ RETURN TO_JSON(query_path) AS path_json
107
+ ```
108
+
109
+ ```sql
110
+ 👍 Good example returning a path as JSON in a multiple-hop query.
111
+
112
+ GRAPH FinGraph
113
+ MATCH query_path = (src:Account {id: 9})-[edge]->{1,3}(dst:Account)
114
+ RETURN TO_JSON(query_path) as path_json
115
+ ```
116
+
117
+ ```sql
118
+ 👍 Good example returning multiple paths as JSON.
119
+
120
+ GRAPH FinGraph
121
+ MATCH path_1 = (person:Person {id: 5})-[:Owns]->(accnt:Account),
122
+ path_2 = (src:Account {id: 9})-[:Transfers]->(dst:Account)
123
+ RETURN TO_JSON(path_1) as path_1,
124
+ TO_JSON(path_2) as path_2
125
+ ```
126
+
127
+ ```
128
+ 👎 Anti-example returning nodes properties rather than JSON format graph elements.
129
+ Scalar results other than JSON format graph elements cannot be visualized.
130
+
131
+ GRAPH FinGraph
132
+ MATCH (person:Person {id: 5})-[owns:Owns]->(accnt:Account)
133
+ RETURN person.id AS person,
134
+ owns.amount AS owns,
135
+ accnt.id AS accnt;
136
+ ```
137
+
138
+ ```sql
139
+ 👎 Anti-example returning each node and edges in JSON format verbosely. This will
140
+ work but not as easy as returning a path directly.
141
+
142
+ GRAPH FinGraph
143
+ MATCH (person:Person {id: 5})-[owns:Owns]->(accnt:Account)
144
+ RETURN TO_JSON(person) AS person_json,
145
+ TO_JSON(owns) AS owns_json,
146
+ TO_JSON(accnt) AS accnt_json,
147
+ ```
148
+
149
+ ## Testing changes
150
+
151
+ After adding new changes, please run unit and integration tests with the command below:
152
+
153
+
154
+ ```shell
155
+ cd spanner_graphs && python -m unittest discover -s tests -p "*_test.py"
156
+ ```