streamlit

Форк
0
/
st_file_uploader.py 
108 строк · 3.0 Кб
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 streamlit as st
16
from streamlit import runtime
17

18
single_file = st.file_uploader("Drop a file:", type=["txt"], key="single")
19
if single_file is None:
20
    st.text("No upload")
21
else:
22
    st.text(single_file.read())
23

24
# Here and throughout this file, we use `if runtime.is_running():`
25
# since we also run e2e python files in "bare Python mode" as part of our
26
# Python tests, and this doesn't work in that circumstance
27
# st.session_state can only be accessed while running with streamlit
28
if runtime.exists():
29
    st.write(repr(st.session_state.single) == repr(single_file))
30

31
disabled = st.file_uploader(
32
    "Can't drop a file:", type=["txt"], key="disabled", disabled=True
33
)
34
if disabled is None:
35
    st.text("No upload")
36
else:
37
    st.text(disabled.read())
38

39
if runtime.exists():
40
    st.write(repr(st.session_state.disabled) == repr(disabled))
41

42
multiple_files = st.file_uploader(
43
    "Drop multiple files:",
44
    type=["txt"],
45
    accept_multiple_files=True,
46
    key="multiple",
47
)
48
if multiple_files is None:
49
    st.text("No upload")
50
else:
51
    files = [file.read().decode() for file in multiple_files]
52
    st.text("\n".join(files))
53

54
if runtime.exists():
55
    st.write(repr(st.session_state.multiple) == repr(multiple_files))
56

57
with st.form("foo"):
58
    form_file = st.file_uploader("Inside form:", type=["txt"])
59
    st.form_submit_button("Submit")
60
    if form_file is None:
61
        st.text("No upload")
62
    else:
63
        st.text(form_file.read())
64

65

66
hidden_label = st.file_uploader(
67
    "Hidden label:",
68
    key="hidden_label",
69
    label_visibility="hidden",
70
)
71

72
if hidden_label is None:
73
    st.text("No upload")
74
else:
75
    st.text(hidden_label.read())
76

77
if runtime.exists():
78
    st.write(repr(st.session_state.hidden_label) == repr(hidden_label))
79

80
collapsed_label = st.file_uploader(
81
    "Collapsed label:",
82
    key="collapsed_label",
83
    label_visibility="collapsed",
84
)
85

86
if collapsed_label is None:
87
    st.text("No upload")
88
else:
89
    st.text(collapsed_label.read())
90

91
if runtime.exists():
92
    st.write(repr(st.session_state.collapsed_label) == repr(collapsed_label))
93

94
if runtime.exists():
95
    if not st.session_state.get("counter"):
96
        st.session_state["counter"] = 0
97

98
    def file_uploader_on_change():
99
        st.session_state.counter += 1
100

101
    st.file_uploader(
102
        "Drop a file:",
103
        type=["txt"],
104
        key="on_change_file_uploader_key",
105
        on_change=file_uploader_on_change,
106
    )
107

108
    st.text(st.session_state.counter)
109

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

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

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

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