Skip to main content

streamlit-markdown

react-markdown with streaming support for streamlit webapp.

  • streaming rendering of markdown text
  • support for latex math, mermaid diagrams, code highlighting
  • support for tables, images, links

Have a try at https://streaming-markdown.streamlit.app/

Installation

pip install streamlit-markdown

Usage

static content:

from streamlit_markdown import st_markdown

markdown_text = "$ y = f(x)$"
st_markdown(markdown_text)

streaming content:

from streamlit_markdown import st_streaming_markdown

markdown_text = "$ y = f(x)$"
def token_stream():
    for token in markdown_text:
        yeild token
st_streaming_markdown(token_stream, key="token_stream") # key must be set to prevent re-rendering

combined streaming content:

from streamlit_markdown import st_streaming_markdown

markdown_text = "$ y = f(x)$"
def token_stream():
    import random
    for token in markdown_text:
        if random.rand() > 0.5:
            yeild token
        else:
            def callable_token():
                return token
            yeild callable_token
st_streaming_markdown(token_stream, key="token_stream") # key must be set to prevent re-rendering

run example:

streamlit run example.py

img.png img.png

Advanced Usage

There are 3 ways for Style Customization!

Select another theme_color

GLOBAL_THEME_COLOR = Literal["blue", "orange", "green", "red", "purple", "pink", "indigo", "yellow", "teal", "cyan", "gray", "slate", "dark", "light", "null", "custom"]
MERMAID_THEME = Literal["default", "forest", "dark", "neutral", "base"]

I have annotated the type. You should be able to see a type hint if you are using vscode+pylance:

Let theme_color == "custom" and specific custom_color

In this way, you can control the color of border and text separately.

The value follows the color naming of Tailwind CSS.

custom_color = {
    "bg": "bg-gray-100",
    "border": "border-gray-300",
    "text": "text-green-900",
    "hover_bg": "hover:bg-gray-200",
    "hover_text": "hover:text-gray-900",
}
final_content = st_markdown(
    content,
    theme_color="custom",
    mermaid_theme=mermaid_theme,
    mermaid_theme_CSS=None,
    custom_color=custom_color,
    custom_css=None,
    key="content",
)

Specific custom_css

By this way, the value of custom_css dict should be empty string (at least, not None). If the value is empty string, it will be ignored and theme_color system will be used when rendering the type of markdown block html. Otherwise, the custom css value will replace all the class style of the html element. Use it at your own risk!

The inner code is like:

<a className={
custom_css.a_class.length > 0 ? custom_css.a_class : classNames(
    classNameByTheme(theme_color, ["text", "hover_text"], custom_color),
)
} />
custom_css = {
    "a_class": "text-green-900 border-grey-900 hover:text-gray-900",
    "h1_class": "",
    "h2_class": "",
    "h3_class": "",
    "h4_class": "",
    "h5_class": "",
    "h6_class": "",
    "p_class": "",
    "strong_class": "",
    "em_class": "",
    "code_class": "",
    "code_button_class": "",
    "code_latex_class": "",
    "code_mermaid_class": "",
    "pre_class": "",
    "ul_class": "",
    "ol_class": "",
    "li_class": "",
    "table_class": "",
    "thead_class": "",
    "th_class": "",
    "td_class": "",
    "blockquote_class": "",
}
final_content = st_markdown(
    content,
    theme_color="custom",
    mermaid_theme=mermaid_theme,
    mermaid_theme_CSS=None,
    custom_color=None,
    custom_css=custom_css,
    key="content",
)

Building from source

Prerequisites

  • nodejs >= 18.x
  • yarn >= 1.22.x
  • poetry >= 1.2.x
  • python >= 3.8.x

Building

./build.sh

Publishing

poetry publish

License

This project is licensed under the MIT License - see the LICENSE file for details

Metadata

Release files for streamlit-markdown 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-markdown 1.1.0
File Size Uploaded Provenance Actions
streamlit_markdown-1.1.0.tar.gz 4.0 MB Without provenance Details for streamlit_markdown-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-markdown 1.1.0
File Interpreter ABI Platform Actions
streamlit_markdown-1.1.0-py3-none-any.whl Python 3 none any Details for streamlit_markdown-1.1.0-py3-none-any.whl

Total release size: 6.3 MB

Release files / streamlit_markdown-1.1.0.tar.gz

Download URL streamlit_markdown-1.1.0.tar.gz
Size 4.0 MB
Tags Source
SHA-256 checksum
How to use checksums
c73b2b27ab00ca4109ddc1d4f60905be69b6e2a94c31557f072727588409ee11
BLAKE2b-256 checksum
How to use checksums
9118dbf98b6e07a8b164a354fab278b58a31167ef99a26dedf13ad5d047dab91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.14 Darwin/24.1.0

Release files / streamlit_markdown-1.1.0-py3-none-any.whl

Download URL streamlit_markdown-1.1.0-py3-none-any.whl
Size 2.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
6f382c1d9e6da0c74e3d45044927434f13c9eaf816234c16e0d89c87586b09f8
BLAKE2b-256 checksum
How to use checksums
56c45ee06b15a8c8a113d0e2014674c292caa4e217f83b9aff07f59be431f107
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.14 Darwin/24.1.0

Security for streamlit-markdown 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page