Giter VIP home page Giter VIP logo

jupyter_nebulagraph's Introduction

for NebulaGraph Jupyter Docker Image Docker Extension GitHub release (latest by date) pypi-version Open In Colab Documentation

jupyter_demo_shorten.mp4

jupyter_nebulagraph, formerly ipython-ngql, is a Python package that simplifies the process of connecting to NebulaGraph from Jupyter Notebooks or iPython environments. It enhances the user experience by streamlining the creation, debugging, and sharing of Jupyter Notebooks. With jupyter_nebulagraph, users can effortlessly connect to NebulaGraph, load data, execute queries, visualize results, and fine-tune query outputs, thereby boosting collaborative efforts and productivity.

Getting Started

Explore the capabilities of jupyter_nebulagraph by trying it on Google Colab, and the equivalent Jupyter Notebook is available in Docs here.

For a comprehensive guide, visit the official documentation.

Feature Cheat Sheet Example Command Documentation
Connect %ngql --address 127.0.0.1 --port 9669 --user user --password password Connect %ngql
Load Data from CSV %ng_load --source actor.csv --tag player --vid 0 --props 1:name,2:age --space basketballplayer Load Data %ng_load
Query Execution %ngql MATCH p=(v:player{name:"Tim Duncan"})-->(v2:player) RETURN p; Query Execution %ngql or %%ngql(multi-line)
Result Visualization %ng_draw Draw Graph %ng_draw
Draw Schema %ng_draw_schema Draw Schema %ng_draw_schema
Tweak Query Result df = _ to get last query result as pd.dataframe or ResultSet Tweak Result Configure ngql_result_style
Click to see more!

Installation

jupyter_nebulagraph could be installed either via pip or from this git repo itself.

Install via pip

pip install jupyter_nebulagraph

Install inside the repo

git clone [email protected]:wey-gu/jupyter_nebulagraph.git
cd jupyter_nebulagraph
python setup.py install

Load it in Jupyter Notebook or iPython

%load_ext ngql

Connect to NebulaGraph

Arguments as below are needed to connect a NebulaGraph DB instance:

Argument Description
--address or -addr IP address of the NebulaGraph Instance
--port or -P Port number of the NebulaGraph Instance
--user or -u User name
--password or -p Password

Below is an exmple on connecting to 127.0.0.1:9669 with username: "user" and password: "password".

%ngql --address 127.0.0.1 --port 9669 --user user --password password

Make Queries

Now two kind of iPtython Magics are supported:

Option 1: The one line stype with %ngql:

%ngql USE basketballplayer;
%ngql MATCH (v:player{name:"Tim Duncan"})-->(v2:player) RETURN v2.player.name AS Name;

Option 2: The multiple lines stype with %%ngql

%%ngql
SHOW TAGS;
SHOW HOSTS;

Query String with Variables

jupyter_nebulagraph supports taking variables from the local namespace, with the help of Jinja2 template framework, it's supported to have queries like the below example.

The actual query string should be GO FROM "Sue" OVER owns_pokemon ..., and "{{ trainer }}" was renderred as "Sue" by consuming the local variable trainer:

In [8]: vid = "player100"

In [9]: %%ngql
   ...: MATCH (v)<-[e:follow]- (v2)-[e2:serve]->(v3)
   ...:   WHERE id(v) == "{{ vid }}"
   ...: RETURN v2.player.name AS FriendOf, v3.team.name AS Team LIMIT 3;
Out[9]:   RETURN v2.player.name AS FriendOf, v3.team.name AS Team LIMIT 3;
FriendOf	Team
0	LaMarcus Aldridge	Trail Blazers
1	LaMarcus Aldridge	Spurs
2	Marco Belinelli	Warriors

Draw query results

Just call %ng_draw after queries with graph data.

# 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

Draw Graph Schema

%ng_draw_schema

Load Data from CSV

It's supported to load data from a CSV file into NebulaGraph with the help of ng_load_csv magic.

For example, to load data from a CSV file actor.csv into a space basketballplayer with tag player and vid in column 0, and props in column 1 and 2:

"player999","Tom Hanks",30
"player1000","Tom Cruise",40
"player1001","Jimmy X",33

Just run the below line:

%ng_load --source actor.csv --tag player --vid 0 --props 1:name,2:age --space basketballplayer

Some other examples:

# load CSV from a URL
%ng_load --source https://github.com/wey-gu/jupyter_nebulagraph/raw/main/examples/actor.csv --tag player --vid 0 --props 1:name,2:age --space demo_basketballplayer
# with rank column
%ng_load --source follow_with_rank.csv --edge follow --src 0 --dst 1 --props 2:degree --rank 3 --space basketballplayer
# without rank column
%ng_load --source follow.csv --edge follow --src 0 --dst 1 --props 2:degree --space basketballplayer

Tweak Query Result

By default, the query result is a Pandas Dataframe, and we could access that by read from variable _.

In [1]: %ngql MATCH (v:player{name:"Tim Duncan"})-->(v2:player) RETURN v2.player.name AS Name;

In [2]: df = _

It's also configurable to have the result in raw ResultSet, to enable handy NebulaGraph Python App Development.

See more via Docs: Result Handling

CheatSheet

If you find yourself forgetting commands or not wanting to rely solely on the cheat sheet, remember this one thing: seek help through the help command!

%ngql help

Acknowledgments ♥️

jupyter_nebulagraph's People

Contributors

wey-gu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

jupyter_nebulagraph's Issues

%ng_draw is with issues on JupyterLabs

使用jupyter lab时 查询语句在当前单元格返回结果正常显示为dataframe,但是在下一个单元格使用_ 查看数据类型为str,导致使使用%ng_draw 数据类型错误

jupyter lab 版本 3.0.14

%ng_draw


--------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-93-ef87c9714868> in <module>
----> 1 get_ipython().run_line_magic('ng_draw', '')

~/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2342                 kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2343             with self.builtin_trap:
-> 2344                 result = fn(*args, **kwargs)
   2345             return result
   2346 

~/anaconda3/lib/python3.8/site-packages/decorator.py in fun(*args, **kw)
    230             if not kwsyntax:
    231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
    233     fun.__name__ = func.__name__
    234     fun.__doc__ = func.__doc__

~/anaconda3/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/anaconda3/lib/python3.8/site-packages/ngql/magic.py in ng_draw(self, line, cell, local_ns)
    280             return "No result found, please execute a query first."
    281         result_df = local_ns[variable_name]
--> 282         assert isinstance(result_df, pd.DataFrame), "Result is not in Pandas DataFrame Style"
    283 
    284         # Create a graph

AssertionError: Result is not in Pandas DataFrame Style

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.