Naposledy aktivní 10 months ago

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

streamlit_embed_html.py Raw
1import streamlit as st
2import streamlit.components.v1 as components
3
4st.title("嵌入 HTML 內容的範例")
5
6# 使用 components.html() 來插入自訂 HTML
7html_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 參數設定顯示區域的高度
16components.html(html_code, height=200)
17
streamlit_html_example.py Raw
1import streamlit as st
2
3st.title("嵌入 HTML 的範例")
4
5html_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
13st.components.v1.html(html_code, height=200)
14