Last active
          June 25, 2022 14:10 
        
      - 
      
- 
        Save spewil/e8e89cc831f82356445e770324474684 to your computer and use it in GitHub Desktop. 
    Flowcharts from Notion DBs
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | # WHAT IS IT | |
| # This is a hacky little script to generate Mermaid flowcharts from Notion page relations across databases | |
| # TO USE: | |
| # - Grab your Notion API key and stick it in "token" | |
| # - Make sure you've allowed your databases access via "Share" (for each database!) | |
| # - Run the script, copypasta from command line into a code block | |
| # TO DO: | |
| # - Add filtering of relations (a kind of config) | |
| # - Add links to the chart to make things clickable | |
| # - Clean this up, make some more functions --> get_pages, make_chart(relations), etc. | |
| # - Make the output diagram a little prettier | |
| # - Think about how subgraphs might be implemented (https://mermaid-js.github.io/mermaid/#/flowchart?id=subgraphs) | |
| # - Figure out if you can automate dumping the diagram onto a Notion page | |
| token = "secret_XXXXXXXXXXXXXXXXXXXXXX" | |
| headers = { | |
| "Authorization": "Bearer " + token, | |
| "Notion-Version": "2022-02-22", | |
| "Content-Type": "application/json" | |
| } | |
| def query_database(database_id, headers): | |
| url = f"https://api.notion.com/v1/databases/{database_id}/query" | |
| return requests.request("POST", url, headers=headers).json() | |
| def get_page(page_id): | |
| url = f"https://api.notion.com/v1/pages/{page_id}" | |
| return requests.request("GET", url, headers=headers).json() | |
| def get_page_name(page_id): | |
| try: | |
| return get_page(page_id)["properties"]["Name"]["title"][0]["plain_text"] | |
| except: | |
| return "???" | |
| # for every db, get pages (and their names) | |
| # for every page, get relation ids (and relation names) | |
| # for every related page, grab the name | |
| line_end = "\n" | |
| diagram = "graph LR;\n" | |
| for db_id in db_ids: | |
| db = query_database(db_id, headers) | |
| # grab pages | |
| pages_in_db = [r for r in db["results"] if r["object"] == "page"] | |
| for p in pages_in_db: | |
| pname = get_page_name(p["id"]).replace(" ", "") | |
| # get properties of page in db | |
| for propname, prop in p["properties"].items(): | |
| relation_ids = [] | |
| # for the relation properties, get the the related ids | |
| if prop["type"] == "relation": | |
| relation_ids = [i["id"] for i in prop["relation"]] | |
| for rid in relation_ids: | |
| rname = get_page_name(rid).replace(" ", "") | |
| diagram += f"{pname}-- {propname} -->{rname}{line_end}" | |
| print(diagram) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment