Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please allow cypher to be selected for code highlighting in notebooks #275

Open
na-wapf opened this issue Oct 25, 2024 · 2 comments
Open

Comments

@na-wapf
Copy link

na-wapf commented Oct 25, 2024

Hey there :)!

If possible may we please have cypher as a selectable language option in VS Code notebooks (.ipynb files).

We can already run cypher in notebook cells (see further below to see how!),
It is just that the code syntax highlighting is not available!

VS Code lets you manually set the language by clicking here
image

But currently cypher does not show up in the options list;
image

I have been setting up a custom cell magic as follows:


First notebook cell:

from IPython.core.magic import register_cell_magic
from neo4j import Neo4jDriver
import pandas as pd


def read_pandas(cypher:str) -> pd.DataFrame:
    # ... see https://neo4j.com/docs/python-manual/current/transformers/#_transform_to_pandas_dataframe

_ = None

@register_cell_magic
def cypher(line, cell):
    global _
    _ = read_pandas(cell) 
    display(_)

Second notebook cell
This extension could be adjusted so that this next cell can have proper cypher syntax highlighting in vs code!

%%cypher
RETURN "Hello Neo4j!" as result
         result
0  Hello Neo4j!

Third Notebook Cell

print("The first result was "+_.iloc[0,0])
The first result was Hello Neo4j!
@ncordon
Copy link
Collaborator

ncordon commented Nov 8, 2024

Hello @na-wapf! This is a great request. Let us look into it and come back to you.

If you think this could be contributed to the plugin easily, we are also very happy to receive contributions 😄

@na-wapf
Copy link
Author

na-wapf commented Nov 26, 2024

Hello @na-wapf! This is a great request. Let us look into it and come back to you.

If you think this could be contributed to the plugin easily, we are also very happy to receive contributions 😄

Thanks @ncordon :)

I had another look at vs code... It seems like with the current VS Code extension API and I didn't see a way to contribute this functionality?

Looks like you actually need to implement a kernel and install a kernel spec... the terminal command jupyter kernelspec list should list it as an available option, and the kernelspec json should have "language":"cypher" or match the language identifier contributed by this extension.

I think in practice implementing a wrapper kernel that can be installed using pip is fairly easy... see here
The tricky bits are in handling authentication and parameter passing.


Back on my current work-around i found a way to pass python variables from the local scope into cypher
image

# con.py
from neo4j_keycloak_helper import Neo4jManager # custom wrapper
from neo4j.exceptions import CypherSyntaxError
from IPython.core.magic import register_cell_magic
from IPython.display import display
from IPython.core.getipython import get_ipython

@register_cell_magic
def cypher(line, cell):
    ip = get_ipython()
    if ip is None:
        return
    scope = ip.get_local_scope(2)
    params = {key:scope.get(key) for key in line.split(" ")}
    try:
        _ = manager.read_pandas("\n"+cell, parameters=None if params=={} else params)
        ip.push({"_":_})
    except CypherSyntaxError as e:
        print(e.message)
        return
    display(_)


manager = Neo4jManager(
    neo4j_url      = "neo4j+s://...:7687",
    neo4j_database = "neo4j",
    auth           = ... 
)

Then from my notebooks;

# test.ipynb [cell 1]

# import the manager object... don't need to use it though since the cell magic is installed
from con import manager
# test.ipynb [cell 2]
%%cypher
MATCH ... RETURN ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants