-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_of_contents.py
30 lines (22 loc) · 1.02 KB
/
table_of_contents.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
import streamlit as st
class Toc:
# TOC class made by https://discuss.streamlit.io/u/synode
# Taken from https://discuss.streamlit.io/t/table-of-contents-widget/3470/7
def __init__(self):
self._items = []
self._placeholder = None
def title(self, text):
self._markdown(text, "h1")
def header(self, text):
self._markdown(text, "h2", " " * 2)
def subheader(self, text):
self._markdown(text, "h3", " " * 4)
def placeholder(self, sidebar=False):
self._placeholder = st.sidebar.empty() if sidebar else st.empty()
def generate(self):
if self._placeholder:
self._placeholder.markdown("\n".join(self._items), unsafe_allow_html=True)
def _markdown(self, text, level, space=""):
key = "".join(text.replace(' - ','-').replace(' & ','-').replace('.','-').replace(' ','-')).lower()
st.markdown(f"<{level} id='{key}'>{text}</{level}>", unsafe_allow_html=True)
self._items.append(f"{space}* <a href='#{key}'>{text}</a>")