Skip to main content

A Streamlit component to display markdown, which supports table, code switch, LaTeX, mermaid, and more.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_markdown-1.1.0.tar.gz (4.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_markdown-1.1.0-py3-none-any.whl (2.3 MB view details)

Uploaded Python 3

File details

Details for the file streamlit_markdown-1.1.0.tar.gz.

File metadata

  • Download URL: streamlit_markdown-1.1.0.tar.gz
  • Upload date:
  • Size: 4.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Darwin/24.1.0

File hashes

Hashes for streamlit_markdown-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c73b2b27ab00ca4109ddc1d4f60905be69b6e2a94c31557f072727588409ee11
MD5 3229c6427ae9b61e13bdaa0eea241cb3
BLAKE2b-256 9118dbf98b6e07a8b164a354fab278b58a31167ef99a26dedf13ad5d047dab91

See more details on using hashes here.

File details

Details for the file streamlit_markdown-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: streamlit_markdown-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Darwin/24.1.0

File hashes

Hashes for streamlit_markdown-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f382c1d9e6da0c74e3d45044927434f13c9eaf816234c16e0d89c87586b09f8
MD5 cf6d4f5d291f8c2cb0718998887f5804
BLAKE2b-256 56c45ee06b15a8c8a113d0e2014674c292caa4e217f83b9aff07f59be431f107

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page