系列目录
- 学习streamlit-1,简介
- 学习streamlit-2,st.write输出
- 学习streamlit-3,其它输出显示方法
- 学习streamlit-4,滑块
- 学习streamlit-5,图表
- 学习streamlit-6,选项框
- 学习streamlit-7,复选框
- 学习streamlit-8,组件
- 学习streamlit-9,密钥
- 学习streamlit-10,文件上传
- 学习streamlit-11,股票蜡烛图
- 学习streamlit-12,页面布局
- 学习streamlit-13,BMI计算应用
前两篇文章中一直在使用streamlit的方法st.write()
来输出文本和参数到页面。
除了文本和参数,st.write()
还支持以下内容的显示:
- 打印字符串,与
st.markdown()
方法相似。 - 显示python字典。
- 显示
pandas
数据帧,以表格的样式。 - 画图,来源可以是
matplotlib
,plotly
,altair
,graphviz
,bokeh
。 - 更多特性还在添加。
st.write
下面用一段代码来演示st.write()
显示各种内容:
import numpy as np
import altair as alt
import pandas as pd
import streamlit as st
st.header('st.write')
# Example 1
st.write('Hello, *World!* :sunglasses:')
# Example 2
st.write(1234)
# Example 3
df = pd.DataFrame({
'first column': [1, 2, 3, 4],
'second column': [10, 20, 30, 40]
})
st.write(df)
# Example 4
st.write('Below is a DataFrame:', df, 'Above is a dataframe.')
# Example 5
df2 = pd.DataFrame(
np.random.randn(200, 3),
columns=['a', 'b', 'c'])
c = alt.Chart(df2).mark_circle().encode(
x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c'])
st.write(c)
运行后显示效果:
除了st.write()
方法,还可以使用下面几个方法来显示内容:
st.markdown
import streamlit as st
st.markdown('Streamlit is **_really_ cool**.')
st.markdown(”This text is :red[colored red], and this is **:blue[colored]** and bold.”)
st.markdown(":green[$\sqrt{x^2+y^2}=1$] is a Pythagorean identity. :pencil:")
显示效果:
st.title
import streamlit as st
st.title('This is a title')
st.title('A title with _italics_ :blue[colors] and emojis :sunglasses:')
显示效果:
st.header
import streamlit as st
st.header('This is a header')
st.header('A header with _italics_ :blue[colors] and emojis :sunglasses:')
显示效果:
st.subheader
import streamlit as st
st.subheader('This is a subheader')
st.subheader('A subheader with _italics_ :blue[colors] and emojis :sunglasses:')
显示效果:
st.caption
import streamlit as st
st.caption('This is a string that explains something above.')
st.caption('A caption with _italics_ :blue[colors] and emojis :sunglasses:')
显示效果:
st.code
import streamlit as st
code = '''def hello():
print("Hello, Streamlit!")'''
st.code(code, language='python')
显示效果:
st.text
import streamlit as st
st.text('This is some text.')
显示效果:
st.latex
import streamlit as st
st.latex(r'''
a + ar + a r^2 + a r^3 + \cdots + a r^{n-1} =
\sum_{k=0}^{n-1} ar^k =
a \left(\frac{1-r^{n}}{1-r}\right)
''')
显示效果:
magic
streamlit中还有一个"magic"命令,它可以让我们几乎显示任何东西,markdown、数据、图表等等,并且无需输入任何显式命令,只输入要显示的代码行本身就可以显示,比如以下代码:
# 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 # 👈 Draw a Matplotlib chart
运行后效果:
视频教程:
How to use st.write and magic commands
公众号 | FunIO
微信搜一搜 “funio”,发现更多精彩内容。