最后活跃于 10 months ago

此範例展示如何在 Streamlit 應用中嵌入 HTML、CSS 和 JavaScript,允許自訂 UI 元素、樣式和互動功能,使 Web 應用更具靈活性和可視化效果。

timmy 修订了这个 Gist 10 months ago. 转到此修订

1 file changed, 16 insertions

streamlit_embed_html.py(文件已创建)

@@ -0,0 +1,16 @@
1 + import streamlit as st
2 + import streamlit.components.v1 as components
3 +
4 + st.title("嵌入 HTML 內容的範例")
5 +
6 + # 使用 components.html() 來插入自訂 HTML
7 + html_code = """
8 + <div style="text-align:center;">
9 + <h2 style="color: blue;">這是自訂的 HTML 標題</h2>
10 + <p style="font-size: 18px;">這是一段內嵌的 HTML 內容,你可以自由修改。</p>
11 + <button onclick="alert('你點擊了按鈕!')">點我一下</button>
12 + </div>
13 + """
14 +
15 + # 透過 components.html() 來顯示 HTML,height 參數設定顯示區域的高度
16 + components.html(html_code, height=200)

timmy 修订了这个 Gist 10 months ago. 转到此修订

1 file changed, 0 insertions, 0 deletions

gistfile1.txt 重命名为 streamlit_html_example.py

文件已重命名,但内容与之前没有差异

timmy 修订了这个 Gist 10 months ago. 转到此修订

1 file changed, 13 insertions

gistfile1.txt(文件已创建)

@@ -0,0 +1,13 @@
1 + import streamlit as st
2 +
3 + st.title("嵌入 HTML 的範例")
4 +
5 + html_code = """
6 + <div style="border: 2px solid #4CAF50; padding: 10px; border-radius: 5px;">
7 + <h2 style="color: #4CAF50;">這是一個 HTML 區塊</h2>
8 + <p>你可以在這裡嵌入 HTML, CSS, 甚至 JavaScript。</p>
9 + <button onclick="alert('Hello, Streamlit!')">點擊我</button>
10 + </div>
11 + """
12 +
13 + st.components.v1.html(html_code, height=200)
上一页 下一页