def test_st_arrow_altair_chart(self): """Test st._arrow_altair_chart.""" import altair as alt df = pd.DataFrame(np.random.randn(3, 3), columns=["a", "b", "c"]) c = (alt.Chart(df).mark_circle().encode(x="a", y="b", size="c", color="c").interactive()) st._arrow_altair_chart(c) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart spec = json.loads(proto.spec) # Checking Vega-Lite is a lot of work so rather than doing that, we # just checked to see if the spec data name matches the dataset. self.assertEqual(spec.get("data").get("name"), proto.datasets[0].name)
def test_date_column_utc_scale(self): """Test that columns with date values have UTC time scale""" df = pd.DataFrame({ "index": [date(2019, 8, 9), date(2019, 8, 10)], "numbers": [1, 10] }).set_index("index") chart = altair._generate_chart(ChartType.LINE, df) st._arrow_altair_chart(chart) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart spec_dict = json.loads(proto.spec) # The x axis should have scale="utc", because it uses date values. x_scale = _deep_get(spec_dict, "encoding", "x", "scale", "type") self.assertEqual(x_scale, "utc") # The y axis should _not_ have scale="utc", because it doesn't # use date values. y_scale = _deep_get(spec_dict, "encoding", "y", "scale", "type") self.assertNotEqual(y_scale, "utc")
def test_altair_chart(self): """Test that it can be called with args.""" df = pd.DataFrame([["A", "B", "C", "D"], [28, 55, 43, 91]], index=["a", "b"]).T chart = alt.Chart(df).mark_bar().encode(x="a", y="b") EXPECTED_DATAFRAME = pd.DataFrame({ "a": ["A", "B", "C", "D"], "b": [28, 55, 43, 91], }) st._arrow_altair_chart(chart) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertEqual(proto.HasField("data"), False) self.assertEqual(len(proto.datasets), 1) pd.testing.assert_frame_equal( bytes_to_data_frame(proto.datasets[0].data.data), EXPECTED_DATAFRAME) spec_dict = json.loads(proto.spec) self.assertEqual( spec_dict["encoding"], { "y": { "field": "b", "type": "quantitative" }, "x": { "field": "a", "type": "nominal" }, }, ) self.assertEqual(spec_dict["data"], {"name": proto.datasets[0].name}) self.assertEqual(spec_dict["mark"], "bar") self.assertTrue("config" in spec_dict) self.assertTrue("encoding" in spec_dict)
# comment this one out to avoid this Cypress-Mapbox related error. # ref: https://github.com/cypress-io/cypress/issues/4322 # st.pydeck_chart() # st.map() # 6 errors try: st._arrow_vega_lite_chart({}, use_container_width=True) except Exception as e: st.write(e) try: st._arrow_vega_lite_chart(data, {}, use_container_width=True) except Exception as e: st.write(e) try: st._arrow_vega_lite_chart(data, use_container_width=True) except Exception as e: st.write(e) try: st._arrow_vega_lite_chart(use_container_width=True) except Exception as e: st.write(e) try: st._arrow_altair_chart(use_container_width=True) except Exception as e: st.write(e)
# Copyright 2018-2021 Streamlit Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # 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 import numpy as np import altair as alt data = np.random.randn(200, 3) df = pd.DataFrame(data, columns=["a", "b", "c"]) chart = alt.Chart(df).mark_circle().encode(x="a", y="b", size="c", color="c") st._arrow_altair_chart(chart)
}, "encoding": { "x": { "field": "a", "type": "quantitative" }, "y": { "field": "b", "type": "quantitative" }, }, }, use_container_width=True, ) altair_element = st._arrow_altair_chart( alt.Chart(df).mark_line(point=True).encode(x="a", y="b").interactive(), use_container_width=True, ) table_element._arrow_add_rows(df) dataframe_element._arrow_add_rows(df) chart_element_1._arrow_add_rows(df) chart_element_2._arrow_add_rows(df) vega_element_1._arrow_add_rows(df) vega_element_2._arrow_add_rows(df) vega_element_3._arrow_add_rows(foo=df) altair_element._arrow_add_rows(df) # Test that `_arrow_add_rows` errors out when the dataframe dimensions don't match. # This should show an error! dataframe_element = st._arrow_dataframe(df) dataframe_element._arrow_add_rows(np.abs(np.random.randn(1, 6)))