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

Streamlit新手求助:如何正确展示函数输出?当前展示异常

解决Streamlit中st.write显示函数签名而非执行结果的问题

你遇到的问题是因为直接把函数对象传给了st.write,而不是调用函数后传入它的返回值。Streamlit在接收函数对象时,会默认显示函数的签名信息,也就是你看到的main.ecc_display(ecc_duration_percentage)这类内容。

解决方法很简单:

  • 调用函数时必须加上括号,并传入函数所需的参数,把函数执行后的返回值传给st.write。

示例代码:

import streamlit as st

# 你的自定义函数
def ecc_display(ecc_duration_percentage):
    if ecc_duration_percentage < 20:
        return "ECC时长占比偏低"
    elif 20 <= ecc_duration_percentage <= 60:
        return "ECC时长占比适中"
    else:
        return "ECC时长占比偏高"

# 模拟输入参数(实际场景可以是用户输入或其他变量)
current_ecc_percent = st.number_input("输入ECC时长占比", min_value=0, max_value=100, value=30)

# 正确写法:调用函数并传入参数,将返回值传给st.write
st.write(ecc_display(current_ecc_percent))

关键要点:

  • 错误写法:st.write(ecc_display) → 传入函数本身,显示签名
  • 正确写法:st.write(ecc_display(参数)) → 执行函数,传入返回值,显示预期字符串

内容的提问来源于stack exchange,提问作者Markus Füirst

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 05:05:14