顯示滑塊小部件。
這支持 int、float、date、time 和 datetime 類型。
這還允許您通過將二元素元組或列表作為 value
傳遞來呈現範圍滑塊。
st.slider
和 st.select_slider
之間的區別在於,slider
隻接受數字或日期/時間數據,並將範圍作為輸入,而 select_slider
接受任何數據類型並采用一組可迭代的選項。
函數簽名
st.slider(label, min_value=None, max_value=None, value=None, step=None, format=None, key=None, help=None, on_change=None, args=None, kwargs=None, *, disabled=False)
參數 | 說明 |
---|---|
label (str) | 一個簡短的標簽,向用戶解釋此滑塊的用途。 |
min_value (a supported type or None) | 最小允許值。如果 value 是 int,則默認為 0,如果是 float,則默認為 0.0, value - timedelta(days=14) 如果是 date/datetime,如果是 time,則默認為 time.min |
max_value (a supported type or None) | 最大允許值。如果 value 是 int,則默認為 100,如果是 float,則默認為 1.0,如果是 date/datetime,則 value + timedelta(days=14),如果是 time,則默認為 time.max |
value (a supported type or a tuple/list of supported types or None) | 滑塊首次渲染時的值。如果在此處傳遞兩個值的元組/列表,則呈現具有這些下限和上限的範圍滑塊。例如,如果設置為 |
step (int/float/timedelta or None) | 步進間隔。如果值是 int,則默認為 1,如果是 float,則默認為 0.01,如果是日期/日期時間,則默認為 timedelta(days=1),如果是 time(或者如果 max_value - min_value < 1 天),則默認為 timedelta(minutes=15) |
format (str or None) | printf-style 格式字符串控製接口應如何顯示數字。這不會影響返回值。 int/float 格式器支持:%d %e %f %g %i 日期/時間/日期時間格式器使用 Moment.js 表示法:https://momentjs.com/docs/#/displaying/format/ |
key (str or int) | 一個可選的字符串或整數,用作小部件的唯一鍵。如果省略,將根據其內容為小部件生成一個 key 。相同類型的多個小部件可能不共享相同的鍵。 |
help (str) | 顯示在滑塊旁邊的可選工具提示。 |
on_change (callable) | 當此滑塊的值更改時調用的可選回調。 |
args (tuple) | 傳遞給回調的可選參數元組。 |
kwargs (dict) | 一個可選的 kwargs 字典傳遞給回調。 |
disabled (bool) | 一個可選的布爾值,如果設置為 True,則禁用滑塊。默認值為假。此參數隻能由關鍵字提供。 |
返回 | 說明 |
(int/float/date/time/datetime or tuple of int/float/date/time/datetime) | 滑塊小部件的當前值。返回類型將匹配 value 參數的數據類型。 |
例子
age = st.slider('How old are you?', 0, 130, 25)
st.write("I'm ", age, 'years old')
這是範圍滑塊的示例:
values = st.slider(
'Select a range of values',
0.0, 100.0, (25.0, 75.0))
st.write('Values:', values)
這是一個範圍時間滑塊:
from datetime import time
appointment = st.slider(
"Schedule your appointment:",
value=(time(11, 30), time(12, 45)))
st.write("You're scheduled for:", appointment)
最後,一個日期時間滑塊:
from datetime import datetime
start_time = st.slider(
"When do you start?",
value=datetime(2020, 1, 1, 9, 30),
format="MM/DD/YY - hh:mm")
st.write("Start time:", start_time)
相關用法
- Python Streamlit st.spinner用法及代碼示例
- Python Streamlit st.subheader用法及代碼示例
- Python Streamlit st.success用法及代碼示例
- Python Streamlit st.set_page_config用法及代碼示例
- Python Streamlit st.selectbox用法及代碼示例
- Python Streamlit st.select_slider用法及代碼示例
- Python Streamlit st.snow用法及代碼示例
- Python Streamlit st.stop用法及代碼示例
- Python Streamlit st.experimental_singleton.clear用法及代碼示例
- Python Streamlit st.bokeh_chart用法及代碼示例
- Python Streamlit st.caption用法及代碼示例
- Python Streamlit st.text_input用法及代碼示例
- Python Streamlit st.area_chart用法及代碼示例
- Python Streamlit st.title用法及代碼示例
- Python Streamlit st.cache用法及代碼示例
- Python Streamlit st.experimental_singleton用法及代碼示例
- Python Streamlit st.empty用法及代碼示例
- Python Streamlit st.error用法及代碼示例
- Python Streamlit st.video用法及代碼示例
- Python Streamlit st.vega_lite_chart用法及代碼示例
- Python Streamlit st.header用法及代碼示例
- Python Streamlit st.container用法及代碼示例
- Python Streamlit st.form_submit_button用法及代碼示例
- Python Streamlit st.form用法及代碼示例
- Python Streamlit st.plotly_chart用法及代碼示例
注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.slider。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。