Package: ipython-ngql
https://github.com/wey-gu/ipython-ngql/assets/1651790/10135264-77b5-4d3c-b68f-c5810257feebipython-ngqlis a Python package for connecting to NebulaGraph in Jupyter Notebook or iPython. It simplifies creating, debugging, and sharing Jupyter Notebooks with NebulaGraph interactions for better collaboration.Inspired byipython-sqlbyCatherine Devlin.Get StartedTry it out inGoogle Colab.Installationipython-ngqlcould be installed either via pip or from this git repo itself.Install via pippipinstallipython-ngqlInstall inside the repogitclonegit@github.com:wey-gu/ipython-ngql.gitcdipython-ngql
pythonsetup.pyinstallLoad it in Jupyter Notebook or iPython%load_extngqlConnect to NebulaGraphArguments as below are needed to connect a NebulaGraph DB instance:ArgumentDescription--addressor-addrIP address of the NebulaGraph Instance--portor-PPort number of the NebulaGraph Instance--useror-uUser name--passwordor-pPasswordBelow is an exmple on connecting to127.0.0.1:9669with username: "user" and password: "password".%ngql--address127.0.0.1--port9669--useruser--passwordpasswordMake QueriesNow two kind of iPtython Magics are supported:Option 1: The one line stype with%ngql:%ngqlUSEbasketballplayer;%ngqlMATCH(v:player{name:"Tim Duncan"})-->(v2:player)RETURNv2.player.nameASName;Option 2: The multiple lines stype with%%ngql%%ngqlSHOWTAGS;SHOWHOSTS;There will be other options in future, i.e. from a.ngqlfile.Query String with Variablesipython-ngqlsupports taking variables from the local namespace, with the help ofJinja2template framework, it's supported to have queries like the below example.The actual query string should beGO FROM "Sue" OVER owns_pokemon ..., and"{{ trainer }}"was renderred as"Sue"by consuming the local variabletrainer:In[8]:vid="player100"In[9]:%%ngql...:MATCH(v)<-[e:follow]-(v2)-[e2:serve]->(v3)...:WHEREid(v)=="{{ vid }}"...:RETURNv2.player.nameASFriendOf,v3.team.nameASTeamLIMIT3;Out[9]:RETURNv2.player.nameASFriendOf,v3.team.nameASTeamLIMIT3;FriendOfTeam0LaMarcusAldridgeTrailBlazers1LaMarcusAldridgeSpurs2MarcoBelinelliWarriorsDraw query resultsJust call%ng_drawafter queries with graph data.# one query%ngqlGETSUBGRAPH2STEPSFROM"player101"YIELDVERTICESASnodes,EDGESASrelationships;%ng_draw# another query%ngqlmatchp=(:player)-[]->()returnpLIMIT5%ng_drawDraw Graph Schema%ng_draw_schemaConfigurengql_result_styleBy default,ipython-ngqlwill use pandas dataframe as output style to enable more human-readable output, while it's supported to use the raw thrift data format that comes from thenebula3-pythonitself.This can be done ad-hoc with below one line:%configIPythonNGQL.ngql_result_style="raw"After the above line is executed, the output will be like this:ResultSet(ExecutionResponse(error_code=0,latency_in_us=2844,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'))The result are always stored in variable_in Jupyter Notebook, thus, to tweak the result, just refer a new var to it like:In[1]:%configIPythonNGQL.ngql_result_style="raw"In[2]:%%ngqlUSEpokemon_club;...:GOFROM"Tom"OVERowns_pokemonYIELDowns_pokemon._dstaspokemon_id...:|GOFROM$-.pokemon_idOVERowns_pokemonREVERSELYYIELDowns_pokemon._dstASTrainer_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'Get HelpDon't remember anything or even relying on the cheatsheet here, oen takeaway for you: the help!In[1]:%ngqlhelpExamplesJupyter NotebookPlease refer here:https://github.com/wey-gu/ipython-ngql/blob/main/examples/get_started.ipynbiPythonIn[1]:%load_extngqlIn[2]:%ngql--address192.168.8.128--port9669--userroot--passwordnebulaConnectionPoolCreatedOut[2]:Name0basketballplayer1demo_movie_recommendation2k8s3testIn[3]:%ngqlUSEbasketballplayer;...:%ngqlMATCH(v:player{name:"Tim Duncan"})-->(v2:player)RETURNv2.player.nameASName;Out[3]:Name0TonyParker1ManuGinobili