def test_dict_unflatten(self): """Test passing a spec as keywords.""" st._arrow_vega_lite_chart(df1, x="foo", boink_boop=100, baz={"boz": "booz"}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart pd.testing.assert_frame_equal(bytes_to_data_frame(proto.data.data), df1, check_dtype=False) self.assertDictEqual( json.loads(proto.spec), merge_dicts( autosize_spec, { "baz": { "boz": "booz" }, "boink": { "boop": 100 }, "encoding": { "x": "foo" }, }, ), )
def test_datasets_in_spec(self): """Test passing datasets={foo: df} inside the spec.""" st._arrow_vega_lite_chart({"mark": "rect", "datasets": {"foo": df1}}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertEqual(proto.HasField("data"), False) self.assertDictEqual(json.loads(proto.spec), merge_dicts(autosize_spec, {"mark": "rect"}))
def test_spec_in_arg1(self): """Test that it can be called with spec as the 1st arg.""" st._arrow_vega_lite_chart({"mark": "rect"}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertEqual(proto.HasField("data"), False) self.assertDictEqual(json.loads(proto.spec), merge_dicts(autosize_spec, {"mark": "rect"}))
def test_spec_but_no_data(self): """Test that it can be called with only data set to None.""" st._arrow_vega_lite_chart(None, {"mark": "rect"}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertEqual(proto.HasField("data"), False) self.assertDictEqual(json.loads(proto.spec), merge_dicts(autosize_spec, {"mark": "rect"}))
def test_width_inside_spec(self): """Test that Vega-Lite sets the width.""" st._arrow_vega_lite_chart(df1, {"mark": "rect", "width": 200}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertDictEqual( json.loads(proto.spec), merge_dicts(autosize_spec, {"mark": "rect", "width": 200}), )
def test_pyarrow_table_data(self): """Test that you can pass pyarrow.Table as data.""" table = pa.Table.from_pandas(df1) st._arrow_vega_lite_chart(table, {"mark": "rect"}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertEqual(proto.HasField("data"), True) self.assertEqual(proto.data.data, pyarrow_table_to_bytes(table))
def test_use_container_width(self): """Test that use_container_width=True autosets to full width.""" st._arrow_vega_lite_chart(df1, {"mark": "rect"}, use_container_width=True) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertDictEqual( json.loads(proto.spec), merge_dicts(autosize_spec, {"mark": "rect"}) ) self.assertEqual(proto.use_container_width, True)
def test_data_in_spec(self): """Test passing data=df inside the spec.""" st._arrow_vega_lite_chart({"mark": "rect", "data": df1}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart pd.testing.assert_frame_equal(bytes_to_data_frame(proto.data.data), df1, check_dtype=False) self.assertDictEqual(json.loads(proto.spec), merge_dicts(autosize_spec, {"mark": "rect"}))
def test_no_args_add_rows(self): """Test that you can call _arrow_add_rows on a arrow_vega_lite_chart (without data).""" chart = st._arrow_vega_lite_chart({"mark": "rect"}) proto = self.get_delta_from_queue().new_element.arrow_vega_lite_chart self.assertEqual(proto.HasField("data"), False) chart._arrow_add_rows(df1) proto = self.get_delta_from_queue().arrow_add_rows pd.testing.assert_frame_equal( bytes_to_data_frame(proto.data.data), df1, check_dtype=False )
import matplotlib.pyplot as plt import streamlit as st import pandas as pd data = pd.DataFrame({"a": [1, 2, 3, 4], "b": [1, 3, 2, 4]}) spec = { "mark": "line", "encoding": { "x": {"field": "a", "type": "quantitative"}, "y": {"field": "b", "type": "quantitative"}, }, } # 5 empty charts st._arrow_vega_lite_chart(spec, use_container_width=True) fig, ax = plt.subplots() st.pyplot(fig, use_container_width=True) st._arrow_line_chart() st._arrow_bar_chart() st._arrow_area_chart() # 1 empty map # 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)
"field": "b", "type": "quantitative" }, "size": { "field": "c", "type": "quantitative" }, "color": { "field": "c", "type": "quantitative" }, }, "width": "500", } st._arrow_vega_lite_chart(df, spec, use_container_width=True) st._arrow_vega_lite_chart(df, spec, use_container_width=True) st._arrow_vega_lite_chart(df, spec) st._arrow_vega_lite_chart(df, spec_with_width) # Screenshot comparison st.header("Different ways to get the exact same plot") df = pd.DataFrame([["A", "B", "C", "D"], [28, 55, 43, 91]], index=["a", "b"]).T st.write("Using a top-level `df` and a `spec` dict:") st._arrow_vega_lite_chart( df, {
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", "type": "quantitative" }, "y": { "field": "b", "type": "quantitative" }, }, }, use_container_width=True, ) vega_element_2 = st._arrow_vega_lite_chart( { "datasets": { "foo": df }, "data": {
def test_none_args(self): """Test that an error is raised when called with args set to None.""" with self.assertRaises(ValueError): st._arrow_vega_lite_chart(None, None)
def test_no_args(self): """Test that an error is raised when called with no args.""" with self.assertRaises(ValueError): st._arrow_vega_lite_chart()