st.write(*args, **kwargs)
write(string) 打印格式化的 Markdown 字符串,支持 LaTeX 表达式和表情符号短代码。
write(data_frame) :将 DataFrame 显示为表格。
write(error) :打印异常。
write(func) :显示有关函数的信息。
write(module) :显示有关模块的信息。
write(dict) :在交互式小部件中显示 dict。
write(mpl_fig) :显示 Matplotlib 图。
write(altair) :显示 Altair 图表。
write(keras) :显示 Keras 模型。
write(graphviz) :显示 Graphviz 图。
write(plotly_fig) :显示 Plotly 图。
write(bokeh_fig) :显示散景图。
write(sympy_expr) :使用 LaTeX 打印 SymPy 表达式。
write(htmlable) :如果可用,则为对象打印 repr_html() 。
write(obj) :如果其他未知,则打印 str(obj) 。
unsafe_allow_html (bool) :默认为False。默认情况下,在字符串中找到的任何 HTML 标签都将被转义,因此被视为纯文本。可以通过将此参数设置为 True 来关闭此行为。这个参数随时可能被取消。
基本Markdown格式
import streamlit as st
import pandas as pd
import numpy as np
st.write('Hello, *World!* :sunglasses:')
st.write() 还接受其他数据格式,例如数字、数据框、样式数据框和分类对象
st.write(1234)
data_frame = pd.DataFrame({
'first column': [1, 2, 3, 4],
'second column': [10, 20, 30, 40],
})
st.write(data_frame)
#表格既可以排序,也可以全屏
传入多个参数
st.write('1 + 1 = ', 2)
st.write('Below is a DataFrame:', data_frame, 'Above is a dataframe.')
st.write 也接受图表对象
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)
Magic
魔术命令是 Streamlit 中的一项功能,它允许您编写几乎所有内容(降价、数据、图表),而根本无需键入显式命令。只需将您要显示的内容放在自己的代码行中,它就会出现在您的应用程序中。
# Draw a title and some text to the app:
'''
# This is the document title
This is some _markdown_.
'''
import pandas as pd
df = pd.DataFrame({'col1': [1,2,3]})
df # 👈 Draw the dataframe
x = 10
'x', x # 👈 Draw the string 'x' and then the value of x
# Also works with most supported chart types
import matplotlib.pyplot as plt
import numpy as np
arr = np.random.normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)
fig
任何时候 Streamlit 在自己的行中看到变量或文字值,它都会使用 st.write 自动将其写入您的应用程序。