NebulaGraph in Jupyter Notebook
Introduce the brand new
ipython-ngql
python package that enhances your ability to connect to NebulaGraph from your Jupyter Notebook or iPython. Now we can do%ngql MATCH p=(n:player)->() RETURN p
to query from Jupyter Notebook and%ng_draw
to render the result.
1 Installation
Just do %pip install ipython-ngql
from Jupyter Notebook and load it up via %load_ext ngql
:
%pip install ipython-ngql
%load_ext ngql
Then, connect the NebulaGraph with a line like this:
%ngql --address 127.0.0.1 --port 9669 --user root --password nebula
When connected, the cell will have an output of SHOW SPACES
.
💡 Note, you could install NebulaGraph dev env from Docker Desktop Extension Marketplace and literally have it ready with one click. Then, within the extension, go to “NebulaGraph AI” and click Install NX Mode to install NebulaGraph + Jupyter Notebook local dev env.
2 Query NebulaGraph
We could then do one-liner query with %ngql
or multi-line query with %%ngql
.
2.1 One-liner query
For instance:
%ngql USE basketballplayer;
%ngql MATCH (v:player{name:"Tim Duncan"})-->(v2:player) RETURN v2.player.name AS Name;
2.2 Multi-line query
For instance
%%ngql
ADD HOSTS "storaged3":9779,"storaged4":9779;
SHOW HOSTS;
3 Draw the result
After any query result, we could render it visually with %ng_draw
:
# one query
%ngql GET SUBGRAPH 2 STEPS FROM "player101" YIELD VERTICES AS nodes, EDGES AS relationships;
%ng_draw
# another query
%ngql match p=(:player)-[]->() return p LIMIT 5
%ng_draw
And it’ll look like:
And the renderred result will be in a single-file html, which could be embeded in web pages like:
4 Other functionality
We could query %ngql help
to know more details of options for ipython-ngql. Here also introudce you some small features.
4.1 Get the pandas df query result
The result could be read from _
like:
4.2 Play with ResultSet result instead
By default, the return result is pandas df, but we could configure it as raw
to enable debugging for Python NebulaGraph Application code on query result handling, like:
In [1] : %config IPythonNGQL.ngql_result_style="raw"
In [2] : %%ngql USE pokemon_club;
...: GO FROM "Tom" OVER owns_pokemon YIELD owns_pokemon._dst as pokemon_id
...: | GO FROM $-.pokemon_id OVER owns_pokemon REVERSELY YIELD owns_pokemon._dst AS Trainer_Name;
...:
...:
Out[3]:
ResultSet(ExecutionResponse(
error_code=0,
latency_in_us=3270,
data=DataSet(
column_names=[b'Trainer_Name'],
rows=[Row(
values=[Value(
sVal=b'Tom')]),
...
Row(
values=[Value(
sVal=b'Wey')])]),
space_name=b'pokemon_club'))
In [4]: r = _
In [5]: r.column_values(key='Trainer_Name')[0].cast()
Out[5]: 'Tom'
4.3 Query Template
Besides, I brought the template support in Jinja2, thus we could do varibales like {{ variable }}
:
5 Future
I am planning to add more options on the %ng_draw
in the future, and it’s always welcome for your help to contribute more from https://github.com/wey-gu/ipython-ngql.