streamlit

Форк
0
99 строк · 2.7 Кб
1
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
import pandas as pd
16

17
import streamlit as st
18
from streamlit import runtime
19

20
options = ("female", "male")
21
markdown_options = (
22
    "**bold text**",
23
    "*italics text*",
24
    "~strikethrough text~",
25
    "shortcode: :blush:",
26
    # link should not work in radio options
27
    "[link text](www.example.com)",
28
    "`code text`",
29
    ":red[red] :blue[blue] :green[green] :violet[violet] :orange[orange]",
30
)
31

32
v1 = st.radio("radio 1 (default)", options)
33
st.write("value 1:", v1)
34

35
v2 = st.radio(
36
    "radio 2 (Formatted options)",
37
    options,
38
    1,
39
    format_func=lambda x: x.capitalize(),
40
)
41
st.write("value 2:", v2)
42

43
v3 = st.radio("radio 3 (no options)", [])
44
st.write("value 3:", v3)
45

46
v4 = st.radio("radio 4 (disabled)", options, disabled=True)
47
st.write("value 4:", v4)
48

49
v5 = st.radio("radio 5 (horizontal)", options, horizontal=True)
50
st.write("value 5:", v5)
51

52
v6 = st.radio("radio 6 (options from dataframe)", pd.DataFrame({"foo": list(options)}))
53
st.write("value 6:", v6)
54

55
v7 = st.radio("radio 7 (hidden label)", options, label_visibility="hidden")
56
st.write("value 7:", v7)
57

58
v8 = st.radio("radio 8 (collapsed label)", options, label_visibility="collapsed")
59
st.write("value 8:", v8)
60

61
v9 = st.radio("radio 9 (markdown options)", options=markdown_options)
62
st.write("value 9:", v9)
63

64
v10 = st.radio(
65
    "radio 10 (with captions)",
66
    ["A", "B", "C", "D", "E", "F", "G"],
67
    captions=markdown_options,
68
)
69
st.write("value 10:", v10)
70

71
v11 = st.radio(
72
    "radio 11 (horizontal, captions)",
73
    ["yes", "maybe", "no"],
74
    captions=["Opt in", "", "Opt out"],
75
    horizontal=True,
76
)
77
st.write("value 11:", v11)
78

79
if runtime.exists():
80

81
    def on_change():
82
        st.session_state.radio_changed = True
83
        st.text("Radio widget callback triggered")
84

85
    st.radio(
86
        "radio 12 (with callback, help)",
87
        options,
88
        1,
89
        key="radio12",
90
        on_change=on_change,
91
        help="help text",
92
    )
93
    st.write("value 12:", st.session_state.radio12)
94
    st.write("radio changed:", st.session_state.get("radio_changed") is True)
95
    # Reset to False:
96
    st.session_state.radio_changed = False
97

98
v13 = st.radio("radio 13 (empty selection)", options, index=None)
99
st.write("value 13:", v13)
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.