def test_pyarrow_table_data(self): df = mock_data_frame() table = pa.Table.from_pandas(df) st._arrow_table(table) proto = self.get_delta_from_queue().new_element.arrow_table self.assertEqual(proto.data, pyarrow_table_to_bytes(table))
def test_pandas_version_1_3_0_and_above(self, mock_styler_translate, _): """Tests that `styler._translate` is called with correct arguments in Pandas >= 1.3.0""" df = mock_data_frame() styler = df.style.set_uuid("FAKE_UUID") st._arrow_table(styler) mock_styler_translate.assert_called_once_with(False, False)
def test_pandas_version_below_1_3_0(self, mock_styler_translate, _): """Tests that `styler._translate` is called without arguments in Pandas < 1.3.0""" df = mock_data_frame() styler = df.style.set_uuid("FAKE_UUID") st._arrow_table(styler) mock_styler_translate.assert_called_once_with()
def test_caption(self): df = mock_data_frame() styler = df.style styler.set_caption("FAKE_CAPTION") st._arrow_table(styler) proto = self.get_delta_from_queue().new_element.arrow_table self.assertEqual(proto.styler.caption, "FAKE_CAPTION")
def test_uuid(self): df = mock_data_frame() styler = df.style styler.set_uuid("FAKE_UUID") st._arrow_table(styler) proto = self.get_delta_from_queue().new_element.arrow_table self.assertEqual(proto.styler.uuid, "FAKE_UUID")
def test_st_arrow_table(self): """Test st._arrow_table.""" from streamlit.type_util import bytes_to_data_frame df = pd.DataFrame([[1, 2], [3, 4]], columns=["col1", "col2"]) st._arrow_table(df) proto = self.get_delta_from_queue().new_element.arrow_table pd.testing.assert_frame_equal(bytes_to_data_frame(proto.data), df)
def test_cell_styles(self): df = mock_data_frame() styler = df.style # NOTE: If UUID is not set - a random UUID will be generated. styler.set_uuid("FAKE_UUID") styler.highlight_max(axis=None) st._arrow_table(styler) proto = self.get_delta_from_queue().new_element.arrow_table self.assertEqual(proto.styler.styles, "#T_FAKE_UUIDrow1_col2 { background-color: yellow }")
def test_display_values(self): df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], ) styler = df.style.format("{:.2%}") st._arrow_table(styler) expected = pd.DataFrame([["100.00%", "200.00%", "300.00%"], ["400.00%", "500.00%", "600.00%"]], ) proto = self.get_delta_from_queue().new_element.arrow_table pd.testing.assert_frame_equal( bytes_to_data_frame(proto.styler.display_values), expected)
def test_table_styles(self): df = mock_data_frame() styler = df.style # NOTE: If UUID is not set - a random UUID will be generated. styler.set_uuid("FAKE_UUID") styler.set_table_styles([{ "selector": ".blank", "props": [("background-color", "red")] }]) st._arrow_table(styler) proto = self.get_delta_from_queue().new_element.arrow_table self.assertEqual(proto.styler.styles, "#T_FAKE_UUID .blank { background-color: red }")
def test_dataframe_data(self): df = mock_data_frame() st._arrow_table(df) proto = self.get_delta_from_queue().new_element.arrow_table pd.testing.assert_frame_equal(bytes_to_data_frame(proto.data), df)
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import altair as alt import numpy as np import pandas as pd import streamlit as st df = pd.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]}) table_element = st._arrow_table(df) dataframe_element = st._arrow_dataframe(df) chart_element_1 = st._arrow_line_chart() chart_element_2 = st._arrow_line_chart(df) # 4 identical charts, built in different ways. vega_element_1 = st._arrow_vega_lite_chart( df, { "mark": { "type": "line", "point": True }, "encoding": { "x": { "field": "a",
# See the License for the specific language governing permissions and # limitations under the License. import pandas as pd import streamlit as st # NOTE: DO NOT CHANGE the order of elements. # Otherwise, snapshots will fail. "## CategoricalIndex" df = pd.DataFrame( [["foo", 100], ["bar", 200]], index=pd.CategoricalIndex([1, 2]), ) st._arrow_table(df) "## DatetimeIndex" df = pd.DataFrame( [["foo", 100], ["bar", 200]], index=pd.Series(pd.date_range("2021-01-01", periods=2, freq="Y")), ) st._arrow_table(df) "## Float64Index" df = pd.DataFrame( [["foo", 100], ["bar", 200]], index=pd.Float64Index([1.23, 2.34]), ) st._arrow_table(df)
df = pd.DataFrame(np.random.randn(ROWS, cols), index=range(ROWS), columns=range(cols)) st._arrow_dataframe(df) st.header("Overriding st._arrow_dataframe") for cols in [4, 5, 6, 20]: df = pd.DataFrame(np.random.randn(ROWS, cols), index=range(ROWS), columns=range(cols)) df_elt = st._arrow_dataframe(np.random.randn(200, 200)) df_elt._arrow_dataframe(df) st.header("Using st._arrow_table") for cols in [4, 5, 6, 20]: df = pd.DataFrame(np.random.randn(ROWS, cols), index=range(ROWS), columns=range(cols)) st._arrow_table(df) st.header("Overriding st._arrow_table") for cols in [4, 5, 6, 20]: df = pd.DataFrame(np.random.randn(ROWS, cols), index=range(ROWS), columns=range(cols)) df_elt = st._arrow_table(np.random.randn(200, 200)) df_elt._arrow_table(df)
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import streamlit as st import pandas as pd "## Table Caption" df = pd.DataFrame([["foo", 100], ["bar", 200]]) styler = df.style.set_caption("The caption") st._arrow_table(styler) "## CategoricalIndex" df = pd.DataFrame( [["foo", 100], ["bar", 200]], index=pd.CategoricalIndex([1, 2]), ) st._arrow_table(df) "## IntervalIndex" df = pd.DataFrame( [["foo", 100], ["bar", 200]], index=pd.interval_range(start=0, end=2), ) st._arrow_table(df)
st.header("Empty list") st.write([]) st.header("Empty dataframes") st.write(np.array(0)) st.write(pd.DataFrame([])) st._arrow_dataframe() st._arrow_dataframe([]) st._arrow_dataframe(np.array(0)) st._arrow_dataframe(pd.DataFrame([])) st.header("Empty one-column dataframes") st.write(np.array([])) st._arrow_dataframe(np.array([])) st.header("Empty two-column dataframes (only shows 1)") st.write(pd.DataFrame({"lat": [], "lon": []})) st._arrow_dataframe(pd.DataFrame({"lat": [], "lon": []})) st.header("Empty tables") st._arrow_table() st._arrow_table([]) st._arrow_table(np.array(0)) st._arrow_table(pd.DataFrame([])) st.header("Empty one-column table") st._arrow_table(np.array([])) st.header("Empty two-column table") st._arrow_table(pd.DataFrame({"lat": [], "lon": []}))
else: # from .apply(axis=None) is_max = data == data.max().max() return pd.DataFrame(np.where(is_max, attr, ""), index=data.index, columns=data.columns) # Create a table to be styled in various ways np.random.seed(24) df = pd.DataFrame({"A": np.linspace(1, 5, 5)}) df = pd.concat( [df, pd.DataFrame(np.random.randn(5, 4), columns=list("BCDE"))], axis=1) df.iloc[0, 2] = np.nan # Unstyled st._arrow_table(df) # Custom formatting st._arrow_table(df.style.format("{:.2%}")) # Colors st._arrow_table( df.style.applymap(color_negative_red).apply(highlight_max, color="darkorange", axis=0)) # Add rows throws an exception when the dataframe has a styler x = st._arrow_table( df.style.set_properties(**{ "background-color": "black", "color": "lawngreen"