Skip to content

Add support for mermaid.js in Markdown component (as well as components like gr.Chatbot that use Markdown) #10854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 24, 2025

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Mar 21, 2025

I think this library is worth including in our Markdown component as it's a pretty popular way to generate diagrams & nice visualizations. Test with the new demo I added or with the code snippets below:

import gradio as gr

demo = gr.Blocks()
with demo:
    markdown = gr.Markdown(
        value="""  
        ```mermaid  
        graph TD  
            A[Start] --> B[Step 1]  
            B --> C[Step 2]  
            C --> D[End]  
        ```  
        """
    )
demo.launch()

or

import gradio as gr

demo = gr.Blocks()
with demo:
    markdown = gr.Chatbot(
        value=[("""  
```mermaid  
graph TD  
    A[Start] --> B[Step 1]  
    B --> C[Step 2]  
    C --> D[End]  
```  
        """, "")],
    )
demo.launch()

Closes: #10844
Closes: #9048

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 21, 2025

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detecting...

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/f3dc81d777f1f33567869e76a9dd11230c36d382/gradio-5.22.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@f3dc81d777f1f33567869e76a9dd11230c36d382#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/f3dc81d777f1f33567869e76a9dd11230c36d382/gradio-client-1.13.1.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/f3dc81d777f1f33567869e76a9dd11230c36d382/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 21, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/chatbot patch
@gradio/markdown patch
@gradio/markdown-code patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Add support for mermaid.js in Markdown component (as well as components like gr.Chatbot that use Markdown)

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs added the v: patch A change that requires a patch release label Mar 21, 2025
@abidlabs abidlabs marked this pull request as ready for review March 21, 2025 18:19
@abidlabs
Copy link
Member Author

abidlabs commented Mar 23, 2025

Actually the behavior isn't great when streaming (note the flickering & syntax error). Will work on a better solution:

import gradio as gr
import time

value = """  
abc
```mermaid  
graph TD  
    A[Start] --> B[Step 1]  
    B --> C[Step 2]  
    C --> D[End]  
``` 
defdefdefdefdefdefdefdef
defdefdefdefdefdefdefdef
defdefdefdefdefdefdefdef
"""

demo = gr.Blocks()

def update():
    for i in range(len(value)):
        time.sleep(0.03)
        yield [(value[:i+1], None)]

with demo:
    chatbot = gr.Chatbot(height=600)
    btn = gr.Button()

    btn.click(update, None, chatbot)
demo.launch()

@abidlabs abidlabs marked this pull request as draft March 23, 2025 22:23
@abidlabs
Copy link
Member Author

Actually the behavior isn't great when streaming (note the flickering & syntax error). Will work on a better solution:

The syntax error has been resolved. The flickering is still there, but that's tricky to resolve as the HTML gets re-rendered every time there's a new update. Note that this happens anytime there's HTML in the chat body, e.g. with this:

import gradio as gr
import time

value = """  
abc

<marquee>abc</marquee>

defdefdefdefdefdefdefdef
defdefdefdefdefdefdefdef
defdefdefdefdefdefdefdef
"""

demo = gr.Blocks()

def update():
    for i in range(len(value)):
        time.sleep(0.03)
        yield [(value[:i+1], None)]

with demo:
    chatbot = gr.Chatbot(height=600)
    btn = gr.Button()

    btn.click(update, None, chatbot)
demo.launch()

There are ways we could fix this, e.g. by adding a small debounce before the html is rendered, but I think we can tackle that later.

@abidlabs abidlabs marked this pull request as ready for review March 24, 2025 16:45
import render_math_in_element from "katex/contrib/auto-render";
import "katex/dist/katex.min.css";
import { create_marked } from "./utils";
import { sanitize } from "@gradio/sanitize";
import "./prism.css";
import { standardHtmlTags } from "./html-tags";
import mermaid from "mermaid";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mermaid can be pretty big, maybe we should dynamically import it if someone uses mermaid syntax, rather than everyone paying that cost.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now @pngwn

@abidlabs
Copy link
Member Author

Thanks @pngwn!

@abidlabs
Copy link
Member Author

@aliabid94 pointed out that we should use the dark mermaid.js theme when we are in dark mode. Will update and then merge this in for the release

@abidlabs abidlabs enabled auto-merge (squash) March 24, 2025 19:17
@abidlabs abidlabs merged commit 1649b00 into main Mar 24, 2025
23 of 24 checks passed
@abidlabs abidlabs deleted the mermaid branch March 24, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add Mermaid Support to gr.Markdown() Component Mermaid support in markdown for chatbot
3 participants