streamlit

Форк
0
/
st_add_rows.py 
101 строка · 3.2 Кб
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 time
16

17
import altair as alt
18
import numpy as np
19
import pandas as pd
20

21
import streamlit as st
22

23
df = pd.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
24

25
table_element = st.table(df)
26
dataframe_element = st.dataframe(df)
27
chart_element_1 = st.line_chart()
28
chart_element_2 = st.line_chart(df)
29

30
# 4 identical charts, built in different ways.
31
vega_element_1 = st.vega_lite_chart(
32
    df,
33
    {
34
        "mark": {"type": "line", "point": True},
35
        "encoding": {
36
            "x": {"field": "a", "type": "quantitative"},
37
            "y": {"field": "b", "type": "quantitative"},
38
        },
39
    },
40
    use_container_width=True,
41
)
42
vega_element_2 = st.vega_lite_chart(
43
    {
44
        "datasets": {"foo": df},
45
        "data": {"name": "foo"},
46
        "mark": {"type": "line", "point": True},
47
        "encoding": {
48
            "x": {"field": "a", "type": "quantitative"},
49
            "y": {"field": "b", "type": "quantitative"},
50
        },
51
    },
52
    use_container_width=True,
53
)
54
vega_element_3 = st.vega_lite_chart(
55
    {
56
        "datasets": {"foo": df},
57
        "data": {"name": "foo"},
58
        "mark": {"type": "line", "point": True},
59
        "encoding": {
60
            "x": {"field": "a", "type": "quantitative"},
61
            "y": {"field": "b", "type": "quantitative"},
62
        },
63
    },
64
    use_container_width=True,
65
)
66
altair_element = st.altair_chart(
67
    alt.Chart(df).mark_line(point=True).encode(x="a", y="b").interactive(),
68
    use_container_width=True,
69
)
70

71
table_element.add_rows(df)
72
dataframe_element.add_rows(df)
73
chart_element_1.add_rows(df)
74
chart_element_2.add_rows(df)
75
vega_element_1.add_rows(df)
76
vega_element_2.add_rows(df)
77
vega_element_3.add_rows(foo=df)
78
altair_element.add_rows(df)
79

80
# The following example was failing due to an issue (#3653) in st.add_rows.
81
# In the previous implementation of Quiver, we were mutating the Quiver element
82
# in the addRows function, which prevented re-rendering of the line chart.
83
# This example reproduces the issue, so that we don't repeat the same mistake
84
# in the future.
85

86
current_time = pd.to_datetime("08:00:00 2021-01-01", utc=True)
87
simulation_step = pd.Timedelta(seconds=10)
88

89
df1 = pd.DataFrame(data=[[current_time, 1]], columns=["t", "y"]).set_index("t")
90
line_chart = st.line_chart(df1, use_container_width=True)
91

92
for count in range(5):
93
    current_time += simulation_step
94
    df2 = pd.DataFrame(data=[[current_time, count]], columns=["t", "y"]).set_index("t")
95
    line_chart.add_rows(df2)
96
    time.sleep(0.25)
97

98
# Test that `add_rows` errors out when the dataframe dimensions don't match.
99
# This should show an error!
100
dataframe_element = st.dataframe(df)
101
dataframe_element.add_rows(np.abs(np.random.randn(1, 6)))
102

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

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

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

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