You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Streamlit嵌入本地HTML文件报错求助:找不到指定页面

解决方案

方法1:直接读取HTML内容嵌入(推荐)

components.iframe()默认会将传入的路径识别为Streamlit应用的内部路由,而非静态文件路径,这是报错的核心原因。更可靠的方式是用components.html()直接读取本地HTML文件的内容嵌入:

import streamlit as st
import streamlit.components.v1 as components

# 读取本地HTML文件内容
with open("mypage.html", "r", encoding="utf-8") as f:
    html_content = f.read()

# 嵌入HTML内容,height参数可根据页面实际高度调整
components.html(html_content, height=600)

将mypage.html放在你的主应用文件(如app.py)同一目录下即可,无需放入pages文件夹。

方法2:通过本地HTTP服务托管HTML后用iframe加载

如果必须使用iframe,需要先通过HTTP服务托管本地HTML文件,让Streamlit能通过URL访问到它:

  1. 在mypage.html所在目录启动内置HTTP服务:
python -m http.server 8000
  1. 在Streamlit代码中用iframe指向该服务的URL:
import streamlit as st
import streamlit.components.v1 as components

components.iframe("http://localhost:8000/mypage.html", height=600)

注意要确保HTTP服务与Streamlit应用同时运行,端口号保持一致。

内容的提问来源于stack exchange,提问作者Atanas Atanasov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:57:09