向應用程序寫入參數。
這是 Streamlit 命令的瑞士軍刀:它會根據你向它扔的東西做不同的事情。與其他 Streamlit 命令不同,write() 具有一些獨特的屬性:
- 您可以傳入多個參數,所有參數都將被寫入。
- 它的行為取決於輸入類型,如下所示。
- 它返回None,所以它在App中的"slot"不能被重用。
函數簽名
st.write(*args, **kwargs)
參數 | 說明 |
---|---|
*args (any) | 要打印到應用程序的一個或多個對象。 參數處理如下:
|
unsafe_allow_html (bool) | 這是一個僅限關鍵字的參數,默認為 False。 默認情況下,字符串中的任何 HTML 標記都將被轉義,因此被視為純文本。通過將此參數設置為 True,可以關閉此行為。 也就是說, https://github.com/streamlit/streamlit/issues/152 另請注意,`unsafe_allow_html` 是一項臨時措施,可能隨時從 Streamlit 中刪除。 如果您還是決定啟用 HTML,請在此處告訴我們您的確切用例:https://discuss.streamlit.io/t/96。 這將幫助我們提供安全的 API,讓您可以隨心所欲。 |
示例
它的基本用例是繪製 Markdown 格式的文本,隻要輸入是字符串:
write('Hello, *World!* :sunglasses:')
如前所述,st.write()
還接受其他數據格式,例如數字、 DataFrame 、樣式 DataFrame 和分類對象:
st.write(1234)
st.write(pd.DataFrame({
'first column': [1, 2, 3, 4],
'second column': [10, 20, 30, 40],
}))
最後,您可以傳入多個參數來執行以下操作:
st.write('1 + 1 = ', 2)
st.write('Below is a DataFrame:', data_frame, 'Above is a dataframe.')
哦,還有一件事:st.write
也接受圖表對象!例如:
import pandas as pd
import numpy as np
import altair as alt
df = pd.DataFrame(
np.random.randn(200, 3),
columns=['a', 'b', 'c'])
c = alt.Chart(df).mark_circle().encode(
x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c'])
st.write(c)
相關用法
- Python Streamlit st.warning用法及代碼示例
- Python Streamlit st.experimental_singleton.clear用法及代碼示例
- 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.experimental_singleton用法及代碼示例
- Python Streamlit st.empty用法及代碼示例
- Python Streamlit st.error用法及代碼示例
- 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用法及代碼示例
- Python Streamlit st.plotly_chart用法及代碼示例
- Python Streamlit st.bar_chart用法及代碼示例
- Python Streamlit st.code用法及代碼示例
- Python Streamlit st.experimental_memo.clear用法及代碼示例
- Python Streamlit st.image用法及代碼示例
- Python Streamlit st.markdown用法及代碼示例
- Python Streamlit st.expander用法及代碼示例
注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.write。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。