Skip to content

Instantly share code, notes, and snippets.

@andfanilo
Created February 2, 2023 05:46
Show Gist options
  • Save andfanilo/fbac9d974a882d764296bd48a18956a1 to your computer and use it in GitHub Desktop.
Save andfanilo/fbac9d974a882d764296bd48a18956a1 to your computer and use it in GitHub Desktop.
Streamlit Monkeypatching example (from https://www.youtube.com/watch?v=6f4yYwsqQD8)
import streamlit as st
from streamlit.delta_generator import DeltaGenerator
from streamlit.proto.Markdown_pb2 import Markdown as MarkdownProto
from streamlit.string_util import clean_text
def markdown_but_yelling(body, unsafe_allow_html=False):
body = f"# {body.upper()}!!!!"
markdown_proto = MarkdownProto()
markdown_proto.body = clean_text(body)
markdown_proto.allow_html = unsafe_allow_html
return DeltaGenerator()._enqueue("markdown", markdown_proto)
st.markdown = markdown_but_yelling
st.title("Hello world")
st.markdown("Coucou")
@andfanilo
Copy link
Author

Doing random tests:

import streamlit as st
from streamlit.delta_generator import DeltaGenerator
from streamlit.proto.Markdown_pb2 import Markdown as MarkdownProto
from streamlit.string_util import clean_text

a = st.markdown("Coucou")
st.markdown("maman")

a._enqueue(
    "markdown", 
    MarkdownProto(body=clean_text("YOLO"), allow_html=False)
)

the Yolo part is correctly updated 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment