函数签名[source] | |
---|---|
st.echo(code_location="above") | |
参数 | |
code_location ("above" or "below") | 是否在执行代码块的结果之前或之后显示回显的代码。 |
示例
import streamlit as st with st.echo(): st.write('This code will be printed')
Display code
有时你希望你的Streamlit应用包含既有通常的Streamlit图形元素又有生成这些元素的代码。这就是st.echo()
的用武之地。
好的,假设你有以下文件,并且你希望通过在Streamlit应用中使中间部分可见来使其应用更加直观易懂:
import streamlit as st
def get_user_name():
return 'John'
# ------------------------------------------------
# Want people to see this part of the code...
def get_punctuation():
return '!!!'
greeting = "Hi there, "
user_name = get_user_name()
punctuation = get_punctuation()
st.write(greeting, user_name, punctuation)
# ...up to here
# ------------------------------------------------
foo = 'bar'
st.write('Done!')
上面的文件创建了一个包含“Hi there, John
”的Streamlit应用程序,然后是“Done!”。
现在让我们使用st.echo()
来使代码的中间部分在应用程序中可见:
import streamlit as st
def get_user_name():
return 'John'
with st.echo():
# Everything inside this block will be both printed to the screen
# and executed.
def get_punctuation():
return '!!!'
greeting = "Hi there, "
value = get_user_name()
punctuation = get_punctuation()
st.write(greeting, value, punctuation)
# And now we're back to _not_ printing to the screen
foo = 'bar'
st.write('Done!')
就是这么简单!
注意
你可以在同一个文件中拥有多个st.echo()
块。
随意使用它!
还有问题吗?
我们的 论坛 充满了有用的信息和Streamlit专家。