1.运行需要在命令行里:streamlit run your_app.py
2.常用组件:
作用 | 语句 |
---|---|
显示文本 | st.write(“普通文字”);st.write(“”“makerdown文本”“”);st.text(‘普通文本.’);st.title(‘title’);st.header(‘title’);st.subheader(‘subheader’) |
显示json | st.json({ “code”: 0,“data”: { “sex”: “female”, “age”: 18,“score”: 100}}) |
显示代码 | st.code(“”“print(‘Hello’)”“”, language=‘python’) |
显示DataFrame | st.dataframe(pd.DataFrame(np.random.randn(5, 2), columns=([‘col_1’,‘col_2’])))(表格有滚动条);st.table(df)(表格没有滚动条) |
输入文本 | st.text_input(‘请输入最喜欢的编程语言’, key=“name”) |
按钮 | st.button(“我是按钮,点击我”) |
滑动条 | st.slider(‘temperature’, 0, 100) |
勾选框 | st.checkbox(‘Yes’) |
单选框 | st.radio(‘Pick a language’, [‘python’, ‘c’, ‘rust’, ‘c++’]) |
下拉单选 | st.selectbox(‘用过哪种编程语言?’, (‘python’, ‘c’, ‘java’, ‘rust’)) |
日期选择器 | st.date_input(‘when’) |
颜色选择器 | st.color_picker(‘Pick a color’) |
文件上传选择器 | st.file_uploader(‘Pick a file’) |
3.session_state
if 'counter' not in st.session_state:
st.session_state.counter = 0
if st.button('Increment'):
st.session_state.counter += 1
st.write('Count= ', st.session_state.counter)
4.回调(callbacks)是一个python函数,它在输入组件被更改时被调用,比如按钮被点击、滑动条被拉拽等。
无参回调
if 'counter' not in st.session_state:
st.session_state.counter = 0
def increment_counter():
st.session_state.counter += 1
st.button('Increment', on_click=increment_counter)
st.write('Count= ', st.session_state.counter)
args回调
if 'counter' not in st.session_state:
st.session_state.counter = 0
increment_value = st.number_input('Enter a value', value=0, step=1)
def increment_counter(increment_value):
st.session_state.counter += increment_value
increment = st.button('Increment', on_click=increment_counter,args=(increment_value))
st.write('Count = ', st.session_state.counter)
kwargs回调
if 'counter' not in st.session_state:
st.session_state.counter = 0
def increment_counter(increment_value=0):
st.session_state.counter += increment_value
def decrement_counter(decrement_value=0):
st.session_state.counter -= decrement_value
st.button('Increment', on_click=increment_counter,kwargs=dict(increment_value=5))
st.button('Decrement', on_click=decrement_counter,kwargs=dict(decrement_value=1))
st.write('Count = ', st.session_state.counter)