streamlit
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
15import pandas as pd
16
17import streamlit as st
18from streamlit import runtime
19
20options = ("female", "male")
21markdown_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
32v1 = st.radio("radio 1 (default)", options)
33st.write("value 1:", v1)
34
35v2 = st.radio(
36"radio 2 (Formatted options)",
37options,
381,
39format_func=lambda x: x.capitalize(),
40)
41st.write("value 2:", v2)
42
43v3 = st.radio("radio 3 (no options)", [])
44st.write("value 3:", v3)
45
46v4 = st.radio("radio 4 (disabled)", options, disabled=True)
47st.write("value 4:", v4)
48
49v5 = st.radio("radio 5 (horizontal)", options, horizontal=True)
50st.write("value 5:", v5)
51
52v6 = st.radio("radio 6 (options from dataframe)", pd.DataFrame({"foo": list(options)}))
53st.write("value 6:", v6)
54
55v7 = st.radio("radio 7 (hidden label)", options, label_visibility="hidden")
56st.write("value 7:", v7)
57
58v8 = st.radio("radio 8 (collapsed label)", options, label_visibility="collapsed")
59st.write("value 8:", v8)
60
61v9 = st.radio("radio 9 (markdown options)", options=markdown_options)
62st.write("value 9:", v9)
63
64v10 = st.radio(
65"radio 10 (with captions)",
66["A", "B", "C", "D", "E", "F", "G"],
67captions=markdown_options,
68)
69st.write("value 10:", v10)
70
71v11 = st.radio(
72"radio 11 (horizontal, captions)",
73["yes", "maybe", "no"],
74captions=["Opt in", "", "Opt out"],
75horizontal=True,
76)
77st.write("value 11:", v11)
78
79if runtime.exists():
80
81def on_change():
82st.session_state.radio_changed = True
83st.text("Radio widget callback triggered")
84
85st.radio(
86"radio 12 (with callback, help)",
87options,
881,
89key="radio12",
90on_change=on_change,
91help="help text",
92)
93st.write("value 12:", st.session_state.radio12)
94st.write("radio changed:", st.session_state.get("radio_changed") is True)
95# Reset to False:
96st.session_state.radio_changed = False
97
98v13 = st.radio("radio 13 (empty selection)", options, index=None)
99st.write("value 13:", v13)
100