# ! pip install -u gremlinpython graphistry
# ! pip install -u pandas
# see https://rapids.ai/ if trying GPU dataframes

"""
Explanation: Graphistry Neptune Gremlin identity graph demo
PyGraphistry helps connect to graph data sources, wrangle them with Python dataframe tools, and visualize them with Graphistry. It's often used in notebooks, data apps, and dashboards.
This notebook uses PyGraphistry to quickly:
* Connect to Neptune
* Run Gremlin queries via built-in bindings over gremlin_python
* Convert to dataframes for data wrangling: CPU via Pandas and GPU via RAPIDS cuDF
* Visualize by automatically generating rich, interactive, & GPU-accelerated Graphistry graph visualization sessions
* Share & embed your beautiful results
For any API used below, run help(graphistry.the_method) for a quick view of its docs
The demo is on AWS Neptune's identity graph data sample from our joint graph-app-kit tutorial. If you have your own dataset, including non-identity data, the example queries should still work.
Setup
Optional - Quicklaunch via graph-app-kit for Neptune:
* Neptune: It is tested on Neptune's identity graph database sample kit, and you can swap in your own
* Graphistry: Use your own, get a free Hub account, or launch in AWS alongside Neptune's VPC and public subnet
* Notebook: Use your own, or launch in AWS alongside Neptune's VPC and public subnet
If you hit gremlinpython event runtime bugs, try this gist for solving them
Install
Already provided in graphistry envs
End of explanation
"""


! pip show gremlinpython graphistry | grep 'Name\|Version'

import graphistry
graphistry.__version__

"""
Explanation: Imports
End of explanation
"""


# To specify Graphistry account & server, use:
# graphistry.register(api=3, username='...', password='...', protocol='https', server='hub.graphistry.com')
# For more options, see https://github.com/graphistry/pygraphistry#configure

NEPTUNE_READER_PROTOCOL='wss'
NEPTUNE_READER_HOST='neptunedbcluster-abc.cluster-ro-xyz.us-east-1.neptune.amazonaws.com'
NEPTUNE_READER_PORT='8182'

endpoint = f'{NEPTUNE_READER_PROTOCOL}://{NEPTUNE_READER_HOST}:{NEPTUNE_READER_PORT}/gremlin'
endpoint

#import logging
#logging.basicConfig(level=logging.DEBUG)

"""
Explanation: Configure
End of explanation
"""


graphistry.register(**GRAPHISTRY_CFG)

g = graphistry.neptune(endpoint=endpoint)

g._gremlin_client

"""
Explanation: Connect
End of explanation
"""


%%time

g2 = g.gremlin('g.E().limit(10000)')


print('NODES:')
g2._nodes.info()
g2._nodes.sample(3)

print('EDGES:')
print(g2._edges.info())

g2._edges.sample(3)

%%time

# Enrich nodes dataframe with any available server property data

g3 = g2.fetch_nodes()

print(g3._nodes.info())

g3._nodes.sample(3)

%%time

g3.plot()

"""
Explanation: Query & plot

PyGraphistry automatically converts gremlin results into node/edge dataframes
Edge queries typically only return node IDs; call fetch_nodes() to enrich your g._nodes dataframe
PyGraphistry plots dataframes
End of explanation
"""


%%time

g4 = (g3
      
      # Add node column 'type' based on gremlin-provided column 'label'
      # The legend auto-detects this column and appears
      .nodes(lambda g: g._nodes.assign(type=g._nodes['label']))
      
      .encode_point_color('type', categorical_mapping={
          'website': 'blue',
          'transientId': 'green'
      })

      .encode_point_icon('type', categorical_mapping ={
          'website': 'link',
          'transientId': 'barcode'
      })

      .addStyle(bg={'color': '#eee'}, page={'title': 'My Graph'})

      # More: https://hub.graphistry.com/docs/api/1/rest/url/
      .settings(url_params={'play': 2000})
)

g4.plot()

"""
Explanation: Customize your visuals & Embed
Graphistry visualizes data with smart defaults: community-based coloring, degree-based sizing, force-directed layout, auto-zoom, and built-in visual analytics. However, it often helps to configure your visuals ahead of time.
Example:
* Enable legend on new column 'type'
* Color nodes by node column 'type'
* Pick icons based on node type
* Set background color to match notebook
* Use a tighter layout 
See further examples at the PyGraphistry github repo
End of explanation
"""


%%time

url = g4.plot(render=False)

url

"""
Explanation: Generate URL for other systems
End of explanation
"""


