st.echo - 显示应用源代码
在with块中使用streamlit的echo
方法显示应用源代码,然后执行。
方法原型
streamlit.echo()
示例代码
>>> with st.echo():
>>> st.write('This code will be printed')
下面的示例将中部代码可视化:
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!')
上面的文件创建了一个Streamlit应用,包含词语“Hi there, John”, 以及 “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
块。