streamlit
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
15import time
16
17import altair as alt
18import numpy as np
19import pandas as pd
20
21import streamlit as st
22
23df = pd.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
24
25table_element = st.table(df)
26dataframe_element = st.dataframe(df)
27chart_element_1 = st.line_chart()
28chart_element_2 = st.line_chart(df)
29
30# 4 identical charts, built in different ways.
31vega_element_1 = st.vega_lite_chart(
32df,
33{
34"mark": {"type": "line", "point": True},
35"encoding": {
36"x": {"field": "a", "type": "quantitative"},
37"y": {"field": "b", "type": "quantitative"},
38},
39},
40use_container_width=True,
41)
42vega_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},
52use_container_width=True,
53)
54vega_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},
64use_container_width=True,
65)
66altair_element = st.altair_chart(
67alt.Chart(df).mark_line(point=True).encode(x="a", y="b").interactive(),
68use_container_width=True,
69)
70
71table_element.add_rows(df)
72dataframe_element.add_rows(df)
73chart_element_1.add_rows(df)
74chart_element_2.add_rows(df)
75vega_element_1.add_rows(df)
76vega_element_2.add_rows(df)
77vega_element_3.add_rows(foo=df)
78altair_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
86current_time = pd.to_datetime("08:00:00 2021-01-01", utc=True)
87simulation_step = pd.Timedelta(seconds=10)
88
89df1 = pd.DataFrame(data=[[current_time, 1]], columns=["t", "y"]).set_index("t")
90line_chart = st.line_chart(df1, use_container_width=True)
91
92for count in range(5):
93current_time += simulation_step
94df2 = pd.DataFrame(data=[[current_time, count]], columns=["t", "y"]).set_index("t")
95line_chart.add_rows(df2)
96time.sleep(0.25)
97
98# Test that `add_rows` errors out when the dataframe dimensions don't match.
99# This should show an error!
100dataframe_element = st.dataframe(df)
101dataframe_element.add_rows(np.abs(np.random.randn(1, 6)))
102