-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquarto-live.qmd
54 lines (45 loc) · 2.27 KB
/
quarto-live.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
format: live-html
pyodide:
packages:
- numpy
- pandas
- networkx
- pyvis
- pyodide_unix_timezones
- https://kuzu-lab.netlify.app/wheels/pyarrow-17.0.0-cp312-cp312-pyodide_2024_0_wasm32.whl
- kuzu_wasm==0.0.7
---
```{pyodide}
import networkx as nx,pandas as pd,pyarrow as pa,kuzu_wasm,js
from pyvis.network import Network
from kuzu_wasm.utils import *
# init Kuzu database
kuzu = await kuzu_wasm.init()
db = await kuzu.Database()
conn = await kuzu.Connection(db)
# get remote csv to wasm filesystem
kuzu.FS.writeFile("/follows.csv",await (await js.fetch("https://raw.githubusercontent.com/kuzudb/kuzu/master/dataset/demo-db/csv/follows.csv")).text())
kuzu.FS.writeFile("/city.csv",await (await js.fetch("https://raw.githubusercontent.com/kuzudb/kuzu/master/dataset/demo-db/csv/city.csv")).text())
kuzu.FS.writeFile("/lives-in.csv",await (await js.fetch("https://raw.githubusercontent.com/kuzudb/kuzu/master/dataset/demo-db/csv/lives-in.csv")).text())
kuzu.FS.writeFile("/user.csv",await (await js.fetch("https://raw.githubusercontent.com/kuzudb/kuzu/master/dataset/demo-db/csv/user.csv")).text())
# create schema and import data
await conn.execute("CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name))")
await conn.execute("CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY (name))")
await conn.execute("CREATE REL TABLE Follows(FROM User TO User, since INT64)")
await conn.execute("CREATE REL TABLE LivesIn(FROM User TO City)")
await conn.execute('COPY User FROM "/user.csv";')
await conn.execute('COPY City FROM "/city.csv";')
await conn.execute('COPY Follows FROM "/follows.csv";')
await conn.execute('COPY LivesIn FROM "/lives_in.csv";')
#cypher query
res = await conn.execute("MATCH (a:User)-[f:Follows]->(b:User)RETURN a,f,b")
# display the graph
G = toNetworkx(res,directed = True)
g = Network(height="480px",notebook=True, cdn_resources='remote',directed = True,neighborhood_highlight = True)
g.from_nx(G)
# change the display name
for i in range(len(g.nodes)): g.nodes[i]["label"] = g.nodes[i]["name"]
def display(g, width="100%", height="500px"): print(f'<iframe srcdoc="{g.generate_html(notebook=True).replace(chr(34), """).replace(chr(39), "'")}" style="width: {width}; height: {height}; border: none;"></iframe>')
display(g)
```