Spaces:
Sleeping
Sleeping
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" # Extract this from your Notion page URL | |
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() |