Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active June 17, 2024 23:06
Show Gist options
  • Save korakot/328aaac51d78e589b4a176228e4bb06f to your computer and use it in GitHub Desktop.
Save korakot/328aaac51d78e589b4a176228e4bb06f to your computer and use it in GitHub Desktop.
Using Neo4j in Colab
# download 3.5.8 or neo4j-enterprise-4.0.0-alpha09mr02-unix
!curl https://neo4j.com/artifact.php?name=neo4j-community-3.5.8-unix.tar.gz -o neo4j.tar.gz
# decompress and rename
!tar -xf neo4j.tar.gz # or --strip-components=1
!mv neo4j-community-3.5.8 nj
# disable password, and start server
!sed -i '/#dbms.security.auth_enabled/s/^#//g' nj/conf/neo4j.conf
!nj/bin/neo4j start
!pip install py2neo -q
from py2neo import Graph
graph = Graph("bolt://localhost:7687")
from IPython.core.magic import register_cell_magic
@register_cell_magic
def nj(line, cell=None):
return graph.run(cell or line).to_table()
%%nj
MATCH (n) RETURN n
!pip install py2neo -q
from py2neo.data import Node, Relationship
a = Node("Person", name="Alice")
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
ab #(Alice)-[:KNOWS]->(Bob)
# now still floating, need to add to Graph
# download and config
!wget -P nj/plugins https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/3.5.0.4/apoc-3.5.0.4-all.jar
!sed -i '/dbms.security.procedures.unrestricted/s/^#//g' nj/conf/neo4j.conf
!sed -i 's/my.extensions.example,my.procedures/apoc/' nj/conf/neo4j.conf
!nj/bin/neo4j restart
%%cypher
RETURN apoc.version()
!pip install -q ipython-cypher
import cypher # for cypher.run
%load_ext cypher
%config CypherMagic.feedback=False
%cypher MATCH (a)-[]-(b) RETURN a, b
@patruff
Copy link

patruff commented May 11, 2024

The problem is that the URL is screwed up, use this code and it will work

# download 3.5.8 or neo4j-enterprise-4.0.0-alpha09mr02-unix
#!curl https://neo4j.com/artifact.php?name=neo4j-community-3.5.8-unix.tar.gz -o neo4j.tar.gz
!curl -O https://dist.neo4j.org/neo4j-community-3.5.30-unix.tar.gz neo4j.tar.gz

# decompress and rename
#!tar -xf neo4j.tar.gz  # or --strip-components=1
!tar -xf neo4j-community-3.5.30-unix.tar.gz
!mv neo4j-community-3.5.30 nj
# disable password, and start server
!sed -i '/#dbms.security.auth_enabled/s/^#//g' nj/conf/neo4j.conf
!nj/bin/neo4j start

@Protimapaul
Copy link

The problem is that the URL is screwed up, use this code and it will work

# download 3.5.8 or neo4j-enterprise-4.0.0-alpha09mr02-unix
#!curl https://neo4j.com/artifact.php?name=neo4j-community-3.5.8-unix.tar.gz -o neo4j.tar.gz
!curl -O https://dist.neo4j.org/neo4j-community-3.5.30-unix.tar.gz neo4j.tar.gz

# decompress and rename
#!tar -xf neo4j.tar.gz  # or --strip-components=1
!tar -xf neo4j-community-3.5.30-unix.tar.gz
!mv neo4j-community-3.5.30 nj
# disable password, and start server
!sed -i '/#dbms.security.auth_enabled/s/^#//g' nj/conf/neo4j.conf
!nj/bin/neo4j start

Thank you! but seeing the following error,


ImportError Traceback (most recent call last)
Cell In[1], line 34
32 # Install ipython-cypher for using Cypher in Jupyter Notebook
33 get_ipython().system('pip install -q ipython-cypher')
---> 34 import cypher # for cypher.run
35 get_ipython().run_line_magic('load_ext', 'cypher')
36 get_ipython().run_line_magic('config', 'CypherMagic.feedback=False')

File /opt/conda/lib/python3.10/site-packages/cypher/init.py:1
----> 1 from .magic import *

File /opt/conda/lib/python3.10/site-packages/cypher/magic.py:20
16 from IPython.utils.traitlets import Bool, Int, Unicode
18 from neo4jrestclient.exceptions import StatusException
---> 20 from cypher.connection import Connection
21 from cypher.parse import parse
22 from cypher.run import run

File /opt/conda/lib/python3.10/site-packages/cypher/connection.py:2
1 # -- coding: utf-8 --
----> 2 from neo4jrestclient.client import GraphDatabase
4 from cypher.utils import urlparse
7 class Connection(object):

File /opt/conda/lib/python3.10/site-packages/neo4jrestclient/client.py:30
17 from neo4jrestclient.constants import (
18 BREADTH_FIRST, DEPTH_FIRST,
19 STOP_AT_END_OF_GRAPH,
(...)
27 RETURN_ALL_NODES, RETURN_ALL_BUT_START_NODE
28 )
29 from neo4jrestclient.iterable import Iterable
---> 30 from neo4jrestclient.labels import NodeLabelsProxy, LabelsProxy
31 from neo4jrestclient.query import (
32 QuerySequence, FilterSequence, QueryTransaction, CypherException
33 )
34 from neo4jrestclient.request import Request

File /opt/conda/lib/python3.10/site-packages/neo4jrestclient/labels.py:8
6 from neo4jrestclient.request import Request
7 from neo4jrestclient.exceptions import StatusException
----> 8 from neo4jrestclient.query import FilterSequence
9 from neo4jrestclient.utils import smart_quote, text_type
12 class Label(object):

File /opt/conda/lib/python3.10/site-packages/neo4jrestclient/query.py:6
4 import json
5 import uuid
----> 6 from collections import Sequence
7 import warnings
9 from neo4jrestclient.constants import RAW

ImportError: cannot import name 'Sequence' from 'collections' (/opt/conda/lib/python3.10/collections/init.py)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment