如何在 Streamlit Markdown 中使用 python 变量

how to use a python variable inside streamlit markdown

  import streamlit as st
  statename = "some state name"
  d = 2000
  
  st.markdown("""
    #### "<span style="color:blue">{temp1}</span> has made {temp} calls".format(temp1 = statename, temp = str(d))    
  """,  unsafe_allow_html=True)

此处 .format 正在运行。整行被视为一个字符串。

输出:

知道如何解决这个问题吗?

你能试试下面的 .format 以外的 "f" 吗?:

f"""
  #### "<span style="color:blue">{temp1}</span> has made {str(d)} calls"  
"""

查看下面的输出:

  #### "<span style="color:blue">some state name</span> has made 2000 calls"

使用格式:

"""
  #### "<span style="color:blue">{}</span> has made {} calls"   
""".format(statename,str(d))

"""
  #### "<span style="color:blue">{temp1}</span> has made {temp} calls"   
""".format(temp1=statename,temp=str(d))