下面是我的代码,其中 svg 元素内有一个圆圈,我想通过 jinja2 使用 streamlit 中的 st.html 来渲染它,问题是容器可见,但 svg 元素不可见。它是空白的。请参阅下面的代码。
<style>
html,body {
height: 100%;
background-color: #404040;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
.container {
display: flex;
width: 500px;
/* Extended width for pipe */
height: 500px;
border: 3px solid #EEEEEE;
justify-content: center;
align-items: center;
border-radius: 20px;
}
.drawing {
position: relative;
width: 2000px;
/* Extended width for pipe */
height: 600px;
}
.circle-svg {
width: 100%;
height: 100%;
}
.c3 {
fill: none;
stroke: grey;
stroke-width: 10;
}
</style>
<body>
<div class="container">
<div class="drawing">
<svg viewBox="0 0 500 500" class="circle-svg">
<!-- Circle -->
<circle class="c3" cx="250" cy="250" r="198"></circle>
</svg>
</div>
</div>
</body>
Python代码:
def cutting_dimention(html_content):
# Directly use the HTML content without treating it as a file path
template = Template(html_content)
rendered_html = template.render() # Render the HTML with Jinja2
st.text(rendered_html) # Display rendered HTML text
st.html(rendered_html) # Display rendered HTML as an HTML block in Streamlit
使用 HTML 内容调用该函数
切割尺寸(我的圆)
我刚刚在Streamlit的游乐场页面上玩了一下 , 发现它
st.html()
会接受<p>A paragraph</p>
但不是你的 SVG 标签。根据记录,他们使用DOMPurify进行内部清理,将其从输出中删除,因为该
<svg>
标签不在可接受标签列表中。我建议你两个选择:
A.创建组件
您可以按照 官方文档创建自己的Streamlit组件。
B. 尝试允许
<svg>
标签我没找到如何修改DOMPurify的配置。他们的 API 似乎没有提供这个功能。
因此我询问了Google Gemini:
它告诉我,由于无法通过 API 访问配置,因此无法直接修改配置,但它也建议通过
st.components.v1.html(your_svg_code, height=110)
.我显然不太信服,因为它会把你的 SVG 图像放在 里面
<iframe>
,这意味着你无法设置它的样式。其次,你还得给这个 iframe 设置高度,这对你来说很蠢。因此,选择选项 A,即创建自定义组件。