streamlit
76 строк · 2.3 Кб
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 streamlit as st
16from streamlit import runtime
17
18# st.session_state can only be used in streamlit
19if runtime.exists():
20
21def on_click(x, y):
22if "click_count" not in st.session_state:
23st.session_state.click_count = 0
24
25st.session_state.click_count += 1
26st.session_state.x = x
27st.session_state.y = y
28
29i1 = st.button(
30"button 1", key="button", on_click=on_click, args=(1,), kwargs={"y": 2}
31)
32st.write("value:", i1)
33st.write("value from state:", st.session_state["button"])
34
35button_was_clicked = "click_count" in st.session_state
36st.write("Button was clicked:", button_was_clicked)
37
38if button_was_clicked:
39st.write("times clicked:", st.session_state.click_count)
40st.write("arg value:", st.session_state.x)
41st.write("kwarg value:", st.session_state.y)
42
43i2 = st.checkbox("reset button return value")
44
45i3 = st.button("button 2 (disabled)", disabled=True)
46st.write("value 2:", i3)
47
48i4 = st.button("button 3 (primary)", type="primary")
49st.write("value 3:", i4)
50
51i5 = st.button("button 4 (primary + disabled)", type="primary", disabled=True)
52st.write("value 4:", i5)
53
54st.button("button 5 (container_width)", use_container_width=True)
55
56st.button(
57"button 6 (container_width + help)", use_container_width=True, help="help text"
58)
59
60st.button("_button 7_ (**styled** :green[label])")
61
62cols = st.columns(3)
63
64# Order of conn_types matters to preserve the order in st_button.spec.js and the snapshot
65conn_types = [
66"snowflake",
67"bigquery",
68"huggingface",
69"aws_s3",
70"http_file",
71"postgresql",
72"gsheets",
73"custom",
74]
75for i in range(len(conn_types)):
76cols[i % 3].button(conn_types[i], use_container_width=True)
77