学习streamlit-3

系列目录

前两篇文章中一直在使用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”,发现更多精彩内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值