streamlit 笔记:

Posted UQI-LIUWJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了streamlit 笔记:相关的知识,希望对你有一定的参考价值。

1  打开streamlit 文件

比如我们要打开hello.py文件,在对应路径的命令行下输入如下内容:

streamlit run hello.py

2 streamlit 基础

2.1 创建标题

import streamlit as st

st.title("This is my first streamlit app")

用第一小节的方式打开后,出来的内容便是:

2.1.1 副标题 

import streamlit as st

st.title('hello')
st.subheader('sub')
st.write('end')

2.2 写内容 

2.2.1 文字内容

import streamlit as st

st.title("This is my first streamlit app")
st.write("hello")

 

 2.2.2表格(DataFrame)

import streamlit as st
import numpy as np
import pandas as pd
data=np.arange(4).reshape(2,2)
df=pd.DataFrame(data,
                index=['a','b'],
                columns=['one','two'])
st.write(df)

#或者最后一行是这样也是可以的:
#st.dataframe(df)

 2.3 绘图

2.3.1 折线图

import streamlit as st
import numpy as np
import pandas as pd
data=np.random.randn(200).reshape(100,2)
df=pd.DataFrame(data,
                columns=['one','two'])
st.line_chart(df)

 2.3.2 map

绘制地图

import streamlit as st
import numpy as np
import pandas as pd
map_data = pd.DataFrame(
    np.random.randn(100, 2) + [31,120],
    columns=['lat', 'lon'])
st.map(map_data)

 2.3.3 任意matplotlib的图

import streamlit as st
import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
data=np.random.randn(1000)
ax.hist(data)

st.pyplot(fig)

2.3.4 任意seaborn的图

import streamlit as st
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="ticks", color_codes=True)


tips = sns.load_dataset("tips")
#sns.catplot(x="day", y="total_bill", data=tips)



st.pyplot(sns.catplot(x="day", y="total_bill", data=tips))

 2.3.5 柱状图

Dataframe的每一行是一个bar,每一列是一个颜色 

import streamlit as st
import numpy as np
import pandas as pd
data=np.random.randn(30).reshape(10,3)
df=pd.DataFrame(data,
                columns=['one','two','three'])
st.bar_chart(df)

 2.3.6 任意图

import streamlit as st
from PIL import Image

image=Image.open('car1.png')

st.image(image,caption='car segmentation')

 

 

 3 streamlit交互性

3.1 单选框(st.checkbox)

import streamlit as st
import numpy as np
import pandas as pd
if st.checkbox('Show map'):
    map_data = pd.DataFrame(
        np.random.randn(100, 2) + [31,120],
        columns=['lat', 'lon'])
    st.map(map_data)

 点开之后

 3.2 选择框(selectbox)

import streamlit as st
import numpy as np
import pandas as pd
data=np.arange(6).reshape(3,2)
df=pd.DataFrame(data,
                columns=['one','two'])

option=st.selectbox(
    'which column do you like best?',
    df['one'])

st.write('You selected: ',option)

3.3 column小部件

import streamlit as st

left_column, right_column = st.columns(2)
pressed = left_column.button('Press me?')
if pressed:
    right_column.write("Woohoo!")

 

3.3.1 小部件分别画图

import streamlit as st
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
sns.set_theme(style="ticks", color_codes=True)

left_column, right_column = st.columns(2)

with left_column:
    st.subheader('map')
    map_data = pd.DataFrame(
        np.random.randn(100, 2) + [31,120],
        columns=['lat', 'lon'])
    st.map(map_data)

with right_column:
    st.subheader('scatter')
    tips = sns.load_dataset("tips")

    st.pyplot(sns.catplot(x="day", y="total_bill", data=tips))

 

 3.4 expander部件

import streamlit as st

expander = st.expander("FAQ")
expander.write("Here you could put in some really, really long explanations..................Here you could put in some really, really long explanations...")

3.4.1 expander里面加columns组件

import streamlit as st

with st.expander('see below'):
    col1,col2,col3=st.columns(3)
    col1.subheader('ok')
    col1.write('left')
    col2.write('middle')
    col3.write('right')

3.5 progress进度条

import streamlit as st
import time


word=st.empty()
#文字占位符

bar=st.progress(0)
#进度条

for i in range(100):
    word.text('Iteration: '+str(i+1))
    bar.progress(i+1)
    time.sleep(0.1)

 3.6 测边框 sidebar

import streamlit as st
import numpy as np
import pandas as pd
data=np.arange(6).reshape(3,2)
df=pd.DataFrame(data,
                columns=['one','two'])


st.sidebar.title('Column selection')
option=st.sidebar.selectbox(
    'which column do you like best?',
    df['one'])

st.write('You selected: ',option)

 3.7 输入框

import streamlit as st

data=st.text_input('input a number',12)
st.write('the number is '+str(data))

3.8 滑动输入条

import streamlit as st

num=st.select_slider('select a number',options=range(15))
st.write(num)

以上是关于streamlit 笔记:的主要内容,如果未能解决你的问题,请参考以下文章

没有名为 streamlit.__main__ 的模块; ‘streamlit’是一个包,不能直接执行

Macbook 上的 Streamlit

docker中的Nginx,fastapi和streamlit - 反向代理不适用于streamlit

streamlit pyplot 子图失真

如何从 Javascript 向 Streamlit 发送数据?

streamlit - 同步输入字段