Created
July 22, 2021 14:50
-
-
Save hanxiaomax/0eb7f1679edd30bc913dc9f8df224c7b to your computer and use it in GitHub Desktop.
Create graph from notion with official API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_token_v2="<token>" | |
database_id = "" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var container = document.getElementById('container'); | |
var data = { | |
nodes: nodes, | |
edges: edges | |
}; | |
var options = { | |
layout: { | |
randomSeed: 1 | |
}, | |
edges: { | |
width: 2, | |
arrows: { | |
to: { | |
enabled: true, | |
imageHeight: undefined, | |
imageWidth: undefined, | |
scaleFactor: 0.5, | |
src: undefined, | |
type: "arrow" | |
}, | |
}, | |
smooth: { | |
enabled: false, | |
}, | |
color: { | |
color:'#848484', | |
highlight:'#191970', | |
opacity:0.9 | |
} | |
}, | |
nodes: { | |
shape: "dot", | |
size: 20, | |
color: { | |
border: '#A9A9A9', | |
background: '#A9A9A9', | |
highlight: { | |
border: '#191970', | |
background: '#191970' | |
} | |
}, | |
font: { | |
color: '#A9A9A9', | |
size: 16 | |
} | |
}, | |
}; | |
var network = new vis.Network(container, data, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from notion_client import Client | |
from pprint import pprint | |
import json | |
from config import * | |
from parser import Parser | |
from render import render | |
node_set = set() | |
edge_set = [] | |
nodes = [] | |
edges = [] | |
meta_data = {} | |
def get_page(node_id): | |
return meta_data.get(node_id) | |
def get_parent(node_id): | |
page = get_page(node_id) | |
return page["properties"]["Parent"]["relation"] | |
def get_children(node_id): | |
page = get_page(node_id) | |
return page["properties"]["Children (z)"]["relation"] | |
def add_edge(source_id, target_id): | |
edge_set.append({source_id, target_id}) | |
# edges.append(dict({"from": source_id, "to": target_id})) | |
edge = { | |
"to":target_id, | |
"from":source_id, | |
} | |
edges.append(edge) | |
def is_lost_node(node_id): | |
return len(get_children(node_id))==0 and len(get_parent(node_id))==0 | |
def add_node(node_id, node_title): | |
if node_id in node_set: | |
return | |
node_set.add(node_id) | |
children_num = len(get_children(node_id)) | |
if(is_lost_node(node_id)): | |
color = "#a84432" | |
else: | |
color = "#0c36f0" | |
nodes.append(dict(id=node_id, label=node_title, size=5+2*children_num,color=color)) | |
def query_database(token_v2, database_id): | |
# notion = Client(auth=token_v2) | |
with open("data.json") as json_file: | |
database = json.load(json_file)#notion.databases.query("7f18cfa451df4fc49a12e6ed0026ea7f") | |
parse_database(database) | |
def get_graph(): | |
graph = dict(nodes=nodes, edges=edges) | |
return graph | |
def get_page_title(page): | |
return page["properties"]["Name"]["title"][0]["plain_text"] | |
def parse_database(database): | |
# parser = Parser(database) | |
pages = database["results"] | |
for page in pages: | |
_id = page['id'] | |
meta_data[_id] = page | |
for _id,page in meta_data.items(): | |
add_node(_id,get_page_title(page)) | |
for realtion in get_children(_id): | |
child_id = realtion['id'] | |
page = meta_data[child_id] | |
add_node(child_id,get_page_title(page)) | |
print("add node for children",get_page_title(page)) | |
add_edge(_id,child_id) | |
graph = get_graph() | |
render("zettle", graph) | |
pages = database["results"] | |
print(pages[20]) | |
print(len(pages)) | |
page = pages[20] | |
# print(page["properties"]["Name"]["title"][0]["plain_text"]) | |
if __name__ == '__main__': | |
query_database(my_token_v2, database_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jinja2 import Template | |
def render(title, graph): | |
with open("libs/template.html") as f: | |
template = f.read() | |
t = Template(template) | |
html = t.render(title=title, nodes=graph['nodes'], edges=graph['edges']) | |
f = open("graph_view.html", "w") | |
f.write(html) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment