Created
April 19, 2022 13:56
-
-
Save asehmi/8b957747ab1d55ee2ad032e4aff594a9 to your computer and use it in GitHub Desktop.
Render SVG in Streamlit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import streamlit as st | |
| import base64 | |
| import textwrap | |
| # https://gist.github.com/treuille/8b9cbfec270f7cda44c5fc398361b3b1 | |
| def render_svg(svg): | |
| """Renders the given svg string.""" | |
| b64 = base64.b64encode(svg.encode('utf-8')).decode("utf-8") | |
| html = r'<img src="data:image/svg+xml;base64,%s"/>' % b64 | |
| st.write(html, unsafe_allow_html=True) | |
| def render_svg_example(): | |
| svg = """ | |
| <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"> | |
| <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> | |
| </svg> | |
| """ | |
| st.write('## Rendering an SVG in Streamlit') | |
| st.write('### SVG Input') | |
| st.code(textwrap.dedent(svg), 'svg') | |
| st.write('### SVG Output') | |
| render_svg(svg) | |
| if __name__ == '__main__': | |
| render_svg_example() |
Author
asehmi
commented
Apr 19, 2022

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