Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from notion_client import Client
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
notion = Client(auth=os.environ["NOTION_TOKEN"])
|
9 |
+
PAGE_ID = "90e3293e833e46539d9e64ff4f4fe562" # Extract this from your Notion page URL
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
def get_block_content(block):
|
14 |
+
"""Extract content from a block based on its type."""
|
15 |
+
block_type = block['type']
|
16 |
+
if block_type == 'paragraph':
|
17 |
+
return ''.join(text['plain_text'] for text in block['paragraph']['rich_text'])
|
18 |
+
elif block_type == 'heading_1':
|
19 |
+
return '# ' + ''.join(text['plain_text'] for text in block['heading_1']['rich_text'])
|
20 |
+
elif block_type == 'heading_2':
|
21 |
+
return '## ' + ''.join(text['plain_text'] for text in block['heading_2']['rich_text'])
|
22 |
+
elif block_type == 'heading_3':
|
23 |
+
return '### ' + ''.join(text['plain_text'] for text in block['heading_3']['rich_text'])
|
24 |
+
elif block_type == 'bulleted_list_item':
|
25 |
+
return '• ' + ''.join(text['plain_text'] for text in block['bulleted_list_item']['rich_text'])
|
26 |
+
elif block_type == 'numbered_list_item':
|
27 |
+
return '1. ' + ''.join(text['plain_text'] for text in block['numbered_list_item']['rich_text'])
|
28 |
+
elif block_type == 'to_do':
|
29 |
+
checkbox = '☑' if block['to_do']['checked'] else '☐'
|
30 |
+
return checkbox + ' ' + ''.join(text['plain_text'] for text in block['to_do']['rich_text'])
|
31 |
+
elif block_type == 'toggle':
|
32 |
+
return '▼ ' + ''.join(text['plain_text'] for text in block['toggle']['rich_text'])
|
33 |
+
elif block_type == 'code':
|
34 |
+
return f"```{block['code']['language']}\n{block['code']['rich_text'][0]['plain_text']}\n```"
|
35 |
+
else:
|
36 |
+
return f"Unsupported block type: {block_type}"
|
37 |
+
|
38 |
+
def fetch_notion_page(page_id):
|
39 |
+
"""Fetch the content of a Notion page."""
|
40 |
+
page = notion.pages.retrieve(page_id)
|
41 |
+
blocks = notion.blocks.children.list(page_id)
|
42 |
+
|
43 |
+
# Extract the title
|
44 |
+
title = page['properties']['title']['title'][0]['plain_text']
|
45 |
+
|
46 |
+
# Extract the content
|
47 |
+
content = []
|
48 |
+
for block in blocks['results']:
|
49 |
+
content.append(get_block_content(block))
|
50 |
+
|
51 |
+
return title, '\n\n'.join(content)
|
52 |
+
|
53 |
+
def main():
|
54 |
+
st.set_page_config(page_title="Notion Page Viewer", page_icon="📘")
|
55 |
+
|
56 |
+
st.title("Notion Page Viewer")
|
57 |
+
|
58 |
+
try:
|
59 |
+
title, content = fetch_notion_page(PAGE_ID)
|
60 |
+
|
61 |
+
st.header(title)
|
62 |
+
st.markdown(content)
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
st.error(f"An error occurred: {str(e)}")
|
66 |
+
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
main()
|