File size: 2,499 Bytes
695410c
 
 
 
 
 
 
 
c72a371
695410c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as st
from notion_client import Client
import requests
import json
import os


notion = Client(auth=os.environ["NOTION_TOKEN"])
PAGE_ID = "90e3293e833e46539d9e64ff4f4fe562"  #90e3293e833e46539d9e64ff4f4fe562



def get_block_content(block):
    """Extract content from a block based on its type."""
    block_type = block['type']
    if block_type == 'paragraph':
        return ''.join(text['plain_text'] for text in block['paragraph']['rich_text'])
    elif block_type == 'heading_1':
        return '# ' + ''.join(text['plain_text'] for text in block['heading_1']['rich_text'])
    elif block_type == 'heading_2':
        return '## ' + ''.join(text['plain_text'] for text in block['heading_2']['rich_text'])
    elif block_type == 'heading_3':
        return '### ' + ''.join(text['plain_text'] for text in block['heading_3']['rich_text'])
    elif block_type == 'bulleted_list_item':
        return '• ' + ''.join(text['plain_text'] for text in block['bulleted_list_item']['rich_text'])
    elif block_type == 'numbered_list_item':
        return '1. ' + ''.join(text['plain_text'] for text in block['numbered_list_item']['rich_text'])
    elif block_type == 'to_do':
        checkbox = '☑' if block['to_do']['checked'] else '☐'
        return checkbox + ' ' + ''.join(text['plain_text'] for text in block['to_do']['rich_text'])
    elif block_type == 'toggle':
        return '▼ ' + ''.join(text['plain_text'] for text in block['toggle']['rich_text'])
    elif block_type == 'code':
        return f"```{block['code']['language']}\n{block['code']['rich_text'][0]['plain_text']}\n```"
    else:
        return f"Unsupported block type: {block_type}"

def fetch_notion_page(page_id):
    """Fetch the content of a Notion page."""
    page = notion.pages.retrieve(page_id)
    blocks = notion.blocks.children.list(page_id)
    
    # Extract the title
    title = page['properties']['title']['title'][0]['plain_text']
    
    # Extract the content
    content = []
    for block in blocks['results']:
        content.append(get_block_content(block))
    
    return title, '\n\n'.join(content)

def main():
    st.set_page_config(page_title="Notion Page Viewer", page_icon="📘")
    
    st.title("Notion Page Viewer")
    
    try:
        title, content = fetch_notion_page(PAGE_ID)
        
        st.header(title)
        st.markdown(content)
    
    except Exception as e:
        st.error(f"An error occurred: {str(e)}")


if __name__ == "__main__":
    main()