streamlit
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 st16from streamlit import runtime17
18single_file = st.file_uploader("Drop a file:", type=["txt"], key="single")19if single_file is None:20st.text("No upload")21else:22st.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
28if runtime.exists():29st.write(repr(st.session_state.single) == repr(single_file))30
31disabled = st.file_uploader(32"Can't drop a file:", type=["txt"], key="disabled", disabled=True33)
34if disabled is None:35st.text("No upload")36else:37st.text(disabled.read())38
39if runtime.exists():40st.write(repr(st.session_state.disabled) == repr(disabled))41
42multiple_files = st.file_uploader(43"Drop multiple files:",44type=["txt"],45accept_multiple_files=True,46key="multiple",47)
48if multiple_files is None:49st.text("No upload")50else:51files = [file.read().decode() for file in multiple_files]52st.text("\n".join(files))53
54if runtime.exists():55st.write(repr(st.session_state.multiple) == repr(multiple_files))56
57with st.form("foo"):58form_file = st.file_uploader("Inside form:", type=["txt"])59st.form_submit_button("Submit")60if form_file is None:61st.text("No upload")62else:63st.text(form_file.read())64
65
66hidden_label = st.file_uploader(67"Hidden label:",68key="hidden_label",69label_visibility="hidden",70)
71
72if hidden_label is None:73st.text("No upload")74else:75st.text(hidden_label.read())76
77if runtime.exists():78st.write(repr(st.session_state.hidden_label) == repr(hidden_label))79
80collapsed_label = st.file_uploader(81"Collapsed label:",82key="collapsed_label",83label_visibility="collapsed",84)
85
86if collapsed_label is None:87st.text("No upload")88else:89st.text(collapsed_label.read())90
91if runtime.exists():92st.write(repr(st.session_state.collapsed_label) == repr(collapsed_label))93
94if runtime.exists():95if not st.session_state.get("counter"):96st.session_state["counter"] = 097
98def file_uploader_on_change():99st.session_state.counter += 1100
101st.file_uploader(102"Drop a file:",103type=["txt"],104key="on_change_file_uploader_key",105on_change=file_uploader_on_change,106)107
108st.text(st.session_state.counter)109