Example #1
0
    def test_concat_cell_style_array(self):
        """Test streamlit.data_frame_proto._concat_cell_style_array."""
        cell_style1 = CellStyle()
        cell_style1.css.extend([_css_style("color", "black")])

        cell_style2 = CellStyle()
        cell_style2.css.extend([_css_style("vertical-align", "middle")])

        style0 = CellStyleArray()

        style1 = CellStyleArray()
        style1.styles.extend([cell_style1])

        style2 = CellStyleArray()
        style2.styles.extend([cell_style2])

        # Combine 1 and 2
        style3 = CellStyleArray()
        style3.styles.extend([cell_style1, cell_style2])

        # not empty
        data_frame_proto._concat_cell_style_array(style1, style2)
        self.assertEqual(str(style1), str(style3))

        # style0 is empty
        data_frame_proto._concat_cell_style_array(style0, style1)
        self.assertEqual(str(style0), str(style1))
Example #2
0
def get_cell_style(proto_df, col, row):
    """Returns the CellStyle for the given cell, or an empty CellStyle
    if no style for the given cell exists
    """
    if col >= len(proto_df.style.cols):
        return CellStyle()

    col_style = proto_df.style.cols[col]
    if row >= len(col_style.styles):
        return CellStyle()

    return col_style.styles[row]
Example #3
0
    def test_add_rows(self):
        """Test streamlit.data_frame_proto._add_rows."""
        # Generic Data
        aa = AnyArray()
        aa.int64s.data.extend([1, 2])

        cell_style = CellStyle()
        cell_style.css.extend([_css_style("color", "black")])

        style = CellStyleArray()
        style.styles.extend([cell_style])

        # Delta DataFrame
        dt1 = Delta()
        dt1.new_element.data_frame.data.cols.extend([aa])
        dt1.new_element.data_frame.index.plain_index.data.int64s.data.extend([3, 4])
        dt1.new_element.data_frame.columns.plain_index.data.int64s.data.extend([5, 6])
        dt1.new_element.data_frame.style.cols.extend([style])

        dt2 = Delta()
        dt2.new_element.data_frame.data.cols.extend([aa])
        dt2.new_element.data_frame.index.plain_index.data.int64s.data.extend([3, 4])
        dt2.new_element.data_frame.columns.plain_index.data.int64s.data.extend([5, 6])
        dt2.new_element.data_frame.style.cols.extend([style])

        combined = Delta()
        aa_combined = AnyArray()
        aa_combined.int64s.data.extend([1, 2, 1, 2])

        style_combined = CellStyleArray()
        style_combined.styles.extend([cell_style, cell_style])

        combined.new_element.data_frame.data.cols.extend([aa_combined])
        row_index = combined.new_element.data_frame.index.plain_index
        row_index.data.int64s.data.extend([3, 4, 3, 4])
        col_index = combined.new_element.data_frame.columns.plain_index
        col_index.data.int64s.data.extend([5, 6])
        combined.new_element.data_frame.style.cols.extend([style_combined])

        # Test both not empty
        data_frame_proto.add_rows(dt1, dt2)
        self.assertEqual(dt1, combined)

        # Test one empty
        dt0 = Delta()
        dt0.new_element.data_frame.data.cols.extend([])

        data_frame_proto.add_rows(dt0, dt1)
        self.assertEqual(str(dt0), str(dt1))

        # Test both empty
        empty0 = Delta()
        empty0.new_element.data_frame.data.cols.extend([])

        empty1 = Delta()
        empty1.new_element.data_frame.data.cols.extend([])

        data_frame_proto.add_rows(empty0, empty1)
        self.assertEqual(str(empty0), str(empty1))

        # Test different data shapes
        diff0 = Delta()
        diff0.new_element.data_frame.data.cols.extend([aa, aa])

        diff1 = Delta()
        diff1.new_element.data_frame.data.cols.extend([aa])

        with pytest.raises(ValueError) as e:
            data_frame_proto.add_rows(diff0, diff1)

        err_msg = "Dataframes have incompatible shapes"
        self.assertEqual(err_msg, str(e.value))