Streamlit(二) - write_magic

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 自动将其写入您的应用程序。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heianduck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值