streamlit
56 строк · 1.8 Кб
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 random
16
17import numpy as np
18import pandas as pd
19
20import streamlit as st
21from streamlit.type_util import DataFormat
22from tests.streamlit.data_mocks import SHARED_TEST_CASES, TestCaseMetadata
23
24np.random.seed(0)
25random.seed(0)
26
27st.set_page_config(layout="wide")
28
29activate_dynamic_editing = st.toggle("Activate dynamic editing")
30show_return_data = st.toggle("Show return data")
31
32TEST_CASES = SHARED_TEST_CASES.copy()
33TEST_CASES.append(
34(
35pd.DataFrame(
36np.random.randn(3, 3),
37columns=pd.MultiIndex.from_tuples(
38[("A", "foo"), ("A", "bar"), ("B", "foo")]
39),
40), # Explicitly set the range index to have the same behavior across versions
41TestCaseMetadata(0, 2, DataFormat.PANDAS_DATAFRAME),
42),
43)
44
45# # Render all test cases with st.data_editor:
46for i, test_case in enumerate(TEST_CASES):
47data = test_case[0]
48data_format = str(test_case[1].expected_data_format)
49st.subheader(data_format)
50return_df_fixed = st.data_editor(
51data,
52key=f"data_editor-{i}",
53num_rows="dynamic" if activate_dynamic_editing else "fixed",
54)
55if show_return_data:
56st.dataframe(return_df_fixed)
57