在`with` 塊中使用以在應用程序上繪製一些代碼,然後執行它。
函數簽名
st.echo(code_location="above")
參數 | 說明 |
---|---|
code_location ("above" or "below") | 是否在執行代碼塊的結果之前或之後顯示回顯代碼。 |
示例
with st.echo():
st.write('This code will be printed')
顯示代碼
有時您希望您的 Streamlit 應用程序包含 both
您常用的 Streamlit 圖形元素 and
生成這些元素的代碼。這就是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!')
上麵的文件創建了一個 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!')
that
很簡單!
注意
您可以在同一個文件中有多個 st.echo()
塊。隨心所欲地使用它!
相關用法
- Python Streamlit st.experimental_singleton.clear用法及代碼示例
- Python Streamlit st.experimental_singleton用法及代碼示例
- Python Streamlit st.empty用法及代碼示例
- Python Streamlit st.error用法及代碼示例
- Python Streamlit st.experimental_memo.clear用法及代碼示例
- Python Streamlit st.expander用法及代碼示例
- Python Streamlit st.experimental_memo用法及代碼示例
- Python Streamlit st.experimental_get_query_params用法及代碼示例
- Python Streamlit st.experimental_set_query_params用法及代碼示例
- Python Streamlit st.exception用法及代碼示例
- Python Streamlit st.experimental_rerun用法及代碼示例
- Python Streamlit st.experimental_show用法及代碼示例
- Python Streamlit st.bokeh_chart用法及代碼示例
- Python Streamlit st.caption用法及代碼示例
- Python Streamlit st.text_input用法及代碼示例
- Python Streamlit st.area_chart用法及代碼示例
- Python Streamlit st.title用法及代碼示例
- Python Streamlit st.cache用法及代碼示例
- Python Streamlit st.video用法及代碼示例
- Python Streamlit st.vega_lite_chart用法及代碼示例
- Python Streamlit st.slider用法及代碼示例
- Python Streamlit st.header用法及代碼示例
- Python Streamlit st.container用法及代碼示例
- Python Streamlit st.form_submit_button用法及代碼示例
- Python Streamlit st.form用法及代碼示例
注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.echo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。