-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstreamlit_app.py
153 lines (118 loc) · 4.59 KB
/
streamlit_app.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from typing import Dict
import streamlit as st
from htbuilder import a, div, styles
from htbuilder.funcs import var
from htbuilder.units import rem
from notion_client import Client
from notion_client.helpers import get_id
notion = Client(auth=st.secrets.notion.token)
def get_page_blocks():
blocks = notion.blocks.children.list(get_id(st.secrets.notion.page_url))
return blocks
def get_pure_text_from_text_dict(text):
return "".join(token["text"]["content"] for token in text)
def get_markdown_from_text_dict(text):
out = []
for token in text:
markdown = None
if token["type"] == "text":
markdown = token["text"]["content"]
annots = token["annotations"]
if token["text"]["link"]:
markdown = f'[{markdown}]({token["text"]["link"]["url"]})'
if annots["bold"]:
markdown = f"**{markdown}**"
if annots["italic"]:
markdown = f"_{markdown}_"
if annots["strikethrough"]:
markdown = f"~~{markdown}~~"
elif token["type"] == "mention":
mention = token["mention"]
if mention["type"] == "date":
date = mention["date"]
if "end" in date and date["end"] is not None:
markdown = f"{date['start']}–{date['end']}"
else:
markdown = date["start"]
elif mention["type"] == "user":
user = mention["user"]
markdown = f"@{user['name']}"
if markdown:
out.append(markdown)
return "".join(out)
def handle_block(block: Dict):
if block["type"] == "heading_1":
md = get_markdown_from_text_dict(block["heading_1"]["rich_text"])
st.write(f"# {md}")
elif block["type"] == "heading_2":
md = get_markdown_from_text_dict(block["heading_2"]["rich_text"])
st.write(f"## {md}")
elif block["type"] == "heading_3":
md = get_markdown_from_text_dict(block["heading_3"]["rich_text"])
st.write(f"### {md}")
elif block["type"] == "paragraph":
md = get_markdown_from_text_dict(block["paragraph"]["rich_text"])
st.write(md)
elif block["type"] == "image":
md = get_markdown_from_text_dict(block["image"]["caption"])
st.image(block["image"]["file"]["url"], md)
elif block["type"] == "code":
txt = get_pure_text_from_text_dict(block["code"]["rich_text"])
# Treat Python codeblocks differently
if block["code"]["language"] == "python":
exec(txt, globals())
else:
st.code(txt, language=block["code"]["language"])
elif block["type"] == "toggle":
md = get_markdown_from_text_dict(block["toggle"]["rich_text"]).strip()
content_blocks = notion.blocks.children.list(block["id"])
if (
(len(md) == 0 or md.lower() == "code") # Title is empty or equal to "Code"
and content_blocks
and len(content_blocks["results"])
== 1 # Only execute if there's a single code block
):
child_block = content_blocks["results"][0]
if (
child_block["type"] == "code"
and child_block["code"]["language"] == "python"
):
txt = get_pure_text_from_text_dict(child_block["code"]["rich_text"])
exec(txt, globals())
else:
with st.expander(md):
draw_blocks(content_blocks)
elif block["type"] == "divider":
st.write("---")
elif block["type"] == "bulleted_list_item":
md = get_markdown_from_text_dict(block["bulleted_list_item"]["rich_text"])
st.write(f"* {md}")
elif block["type"] == "numbered_list_item":
md = get_markdown_from_text_dict(block["numbered_list_item"]["rich_text"])
st.write(f"1. {md}")
def draw_blocks(blocks):
for block in blocks["results"]:
try:
handle_block(block)
except Exception as e:
st.exception(e)
st.expander("Show problematic block").write(block)
st.write(
str(
div(style=styles(position="fixed", top=rem(1), left=rem(1), z_index=2147483647,))(
a(
href=st.secrets.notion.page_url,
target="_blank",
style=styles(
color=var("--text-color"),
text_decoration="none",
),
)("✏️ Edit")
)
),
unsafe_allow_html=True,
)
blocks = get_page_blocks()
draw_blocks(blocks)
# with st.expander("Notionlit debug info"):
# blocks