streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
35.98k stars 3.11k forks source link

Allow Katex extensions #5013

Open chicolucio opened 2 years ago

chicolucio commented 2 years ago

Problem

I would like to render chemical formulas in my app. I see here that Streamlit uses KaTeX to render LaTeX. However, the mhchem LaTeX package is not provided in the core KaTeX. It is provided as an extension by KaTeX.

Solution

The following script should be added to the <head> tag to allow chemical formula rendering with KaTeX:

<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mhchem.min.js" integrity="sha384-RTN08a0AXIioPBcVosEqPUfKK+rPp+h1x/izR7xMkdMyuwkcZCWdxO+RSwIFtJXN"  crossorigin="anonymous"></script>

There is no direct way to add scripts to the head of a page using Streamlit.

On https://github.com/streamlit/streamlit/issues/969 it was suggested that arbitrary JavaScript code blocks should be allowed. However, the context in my issue is different, since the script must be in the head tag.

On https://github.com/streamlit/streamlit/issues/2262 it is also discussed the need of JavaScript injection. Again, I think that my issue is different since the script must be inside the head tag.

On the issue cited above, it was proposed to inject arbitrary JavaScript via a st.javascript() type command. I would add a parameter head when the script must be inside the head tag:

st.javascript('<script>...</script>', head=True)

That said, I think that the easiest way to solve would be simply add the line on Streamlit code itself since the extension is provided by KaTeX and Streamlit uses KaTeX to render every other LaTeX code.


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

lukasmasuch commented 2 years ago

@chicolucio Thanks for this suggestion!

I think that the easiest way to solve would be simply add the line on Streamlit code itself since the extension is provided by KaTeX and Streamlit uses KaTeX to render every other LaTeX code.

I agree that this way would be the best to solve this specific issue, and it seems to be a simple addition to our st.latex command. I have forwarded this to our product team.

chicolucio commented 1 year ago

Any news on this issue?