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}* {text}")