Styler ====== .. cpp:class:: pandas::Styler Styling class for DataFrame rendering. Example ------- .. code-block:: cpp #include using namespace pandas; // Use Styler Styler obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit Styler(const DataFrame& data)`` - pd_styler.h:167 - * - ``Styler(const Styler& other)`` - pd_styler.h:172 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string get_cell_str(size_t row, size_t col) const`` - std::string - pd_styler.h:137 - * - ``size_t get_col_index(const std::string& col_name) const`` - size_t - pd_styler.h:130 - * - ``std::string get_column_name(size_t col) const`` - std::string - pd_styler.h:143 - * - ``std::string get_index_str(size_t row) const`` - std::string - pd_styler.h:140 - Missing Data ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static std::string interpolate_color(double normalized, const std::string& low_color, const std::string& high_color)`` - static std::string - pd_styler.h:152 - Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Styler& apply(ColumnStyleFunc func, std::optional axis = 0, const StyleSubset& subset = {})`` - Styler& - pd_styler.h:191 - :ref:`View ` * - ``Styler& map(CellStyleFunc func, const StyleSubset& subset = {})`` - Styler& - pd_styler.h:202 - :ref:`View ` * - ``auto pipe(Func func) -> decltype(func(\*this))`` - auto - pd_styler.h:646 - :ref:`View ` Combining --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Styler& concat(const Styler& other)`` - Styler& - pd_styler.h:637 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static std::optional to_double(const std::string& s)`` - static std::optional - pd_styler.h:146 - * - ``std::string to_html(const std::string& table_uuid = "", const std::string& table_attributes = "", bool sparse_index = true, bool sparse_columns = true, bool bold_headers = false, const std::string& caption = "", const std::string& encoding = "utf-8", bool doctype_html = false, bool exclude_styles = false) const`` - std::string - pd_styler.h:530 - :ref:`View ` * - ``void to_html(std::ostream& buf, const std::string& table_uuid = "", const std::string& table_attributes = "", bool sparse_index = true, bool sparse_columns = true, bool bold_headers = false, const std::string& caption = "", const std::string& encoding = "utf-8", bool doctype_html = false, bool exclude_styles = false) const`` - void - pd_styler.h:543 - :ref:`View ` * - ``std::string to_latex(const std::string& column_format = "", const std::string& position = "", const std::string& position_float = "", bool hrules = false, const std::string& clines = "", const std::string& label = "", const std::string& caption = "", bool sparse_index = true, bool sparse_columns = true, const std::string& multirow_align = "c", const std::string& multicol_align = "r", bool siunitx = false, const std::string& environment = "", const std::string& encoding = "utf-8", bool convert_css = false) const`` - std::string - pd_styler.h:574 - :ref:`View ` * - ``void to_latex(std::ostream& buf, const std::string& column_format = "", const std::string& position = "", const std::string& position_float = "", bool hrules = false, const std::string& clines = "", const std::string& label = "", const std::string& caption = "", bool sparse_index = true, bool sparse_columns = true, const std::string& multirow_align = "c", const std::string& multicol_align = "r", bool siunitx = false, const std::string& environment = "", const std::string& encoding = "utf-8", bool convert_css = false) const`` - void - pd_styler.h:593 - :ref:`View ` * - ``std::string to_string(bool sparse_index = true, bool sparse_columns = true) const`` - std::string - pd_styler.h:617 - :ref:`View ` * - ``void to_string(std::ostream& buf, bool sparse_index = true, bool sparse_columns = true) const`` - void - pd_styler.h:623 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Styler& background_gradient(const std::string& cmap = "PuBu", double low = 0.0, double high = 0.0, std::optional axis = 0, const StyleSubset& subset = {}, double text_color_threshold = 0.408, std::optional vmin = std::nullopt, std::optional vmax = std::nullopt)`` - Styler& - pd_styler.h:305 - :ref:`View ` * - ``Styler& bar(const StyleSubset& subset = {}, std::optional axis = 0, const std::string& color = "#d65f5f", const std::string& cmap = "", double width = 100.0, double height = 100.0, const std::string& align = "left", std::optional vmin = std::nullopt, std::optional vmax = std::nullopt)`` - Styler& - pd_styler.h:345 - :ref:`View ` * - ``Styler& clear()`` - Styler& - pd_styler.h:495 - :ref:`View ` * - ``const DataFrame& data() const`` - const DataFrame& - pd_styler.h:653 - :ref:`View ` * - ``void ensure_style_matrices()`` - void - pd_styler.h:127 - * - ``static std::string escape_html(const std::string& str)`` - static std::string - pd_styler.h:149 - * - ``std::vector> export_styles() const`` - std::vector> - pd_styler.h:502 - * - ``Styler& format(const std::string& formatter = "", const StyleSubset& subset = {}, const std::string& na_rep = "", int precision = -1, const std::string& thousands = "", bool escape = true)`` - Styler& - pd_styler.h:370 - :ref:`View ` * - ``Styler& format(const std::map& formatters, const std::string& na_rep = "", int precision = -1, bool escape = true)`` - Styler& - pd_styler.h:380 - :ref:`View ` * - ``Styler& hide(const StyleSubset& subset = {}, std::optional axis = std::nullopt, const std::vector& level = {}, bool names = false)`` - Styler& - pd_styler.h:481 - :ref:`View ` * - ``Styler& highlight_between(const StyleSubset& subset = {}, const std::string& color = "yellow", std::optional axis = std::nullopt, std::optional left = std::nullopt, std::optional right = std::nullopt, const std::string& inclusive = "both", const std::string& props = "")`` - Styler& - pd_styler.h:260 - :ref:`View ` * - ``Styler& highlight_max(const StyleSubset& subset = {}, const std::string& color = "yellow", std::optional axis = 0, const std::string& props = "")`` - Styler& - pd_styler.h:229 - :ref:`View ` * - ``Styler& highlight_min(const StyleSubset& subset = {}, const std::string& color = "yellow", std::optional axis = 0, const std::string& props = "")`` - Styler& - pd_styler.h:243 - :ref:`View ` * - ``Styler& highlight_null(const std::string& color = "red", const StyleSubset& subset = {}, const std::string& props = "")`` - Styler& - pd_styler.h:216 - :ref:`View ` * - ``Styler& highlight_quantile(const StyleSubset& subset = {}, const std::string& color = "yellow", std::optional axis = 0, double q_left = 0.0, double q_right = 1.0, const std::string& inclusive = "both", const std::string& props = "")`` - Styler& - pd_styler.h:280 - :ref:`View ` * - ``static std::tuple parse_color(const std::string& color)`` - static std::tuple - pd_styler.h:157 - * - ``std::string render() const { return to_html()`` - std::string - pd_styler.h:658 - :ref:`View ` * - ``std::pair, std::vector> resolve_subset( const StyleSubset& subset) const`` - std::pair, std::vector> - pd_styler.h:133 - * - ``static std::string rgb_to_hex(int r, int g, int b)`` - static std::string - pd_styler.h:160 - * - ``Styler& set_caption(const std::string& caption)`` - Styler& - pd_styler.h:434 - :ref:`View ` * - ``Styler& set_properties(const std::map& props, const StyleSubset& subset = {})`` - Styler& - pd_styler.h:396 - :ref:`View ` * - ``Styler& set_sticky(std::optional axis = 0, std::optional pixel_size = std::nullopt, const std::vector& levels = {})`` - Styler& - pd_styler.h:464 - * - ``Styler& set_table_attributes(const std::string& attributes)`` - Styler& - pd_styler.h:418 - * - ``Styler& set_table_styles( const std::vector>& table_styles, std::optional axis = std::nullopt, bool overwrite = false)`` - Styler& - pd_styler.h:407 - :ref:`View ` * - ``Styler& set_td_classes(const std::vector>& classes)`` - Styler& - pd_styler.h:426 - * - ``Styler& set_tooltips(const std::vector>& ttips, const std::string& props = "", const std::string& css_class = "")`` - Styler& - pd_styler.h:444 - * - ``Styler& set_uuid(const std::string& uuid)`` - Styler& - pd_styler.h:454 - :ref:`View ` * - ``Styler& text_gradient(const std::string& cmap = "PuBu", double low = 0.0, double high = 0.0, std::optional axis = 0, const StyleSubset& subset = {}, std::optional vmin = std::nullopt, std::optional vmax = std::nullopt)`` - Styler& - pd_styler.h:319 - :ref:`View ` * - ``Styler& use(const std::vector>& styles)`` - Styler& - pd_styler.h:510 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-styler-apply-0: .. dropdown:: apply (pd_test_1_all.cpp:11244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11234 :emphasize-lines: 11 void pd_test_func_apply_dataframe_apply_axis0() { std::cout << "========= DataFrame apply axis=0 ======================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // apply axis=0 applies function to each column auto result = df.apply([](const std::vector& col) { return std::accumulate(col.begin(), col.end(), 0.0); }, 0); bool passed = true; // Plan F·dtype: axis=0 reduce now returns a single "result" column // with the original column names ("A", "B") as the row index. // Sum of A: 1+2+3=6, Sum of B: 4+5+6=15 const auto& result_col = result["result"]; double sum_a = std::stod(result_col.get_value_str(0)); .. _example-styler-map-1: .. dropdown:: map (pd_test_1_all.cpp:5839) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5829 :emphasize-lines: 11 // Map Tests // ============================================================================ void pd_test_categorical_index_map() { std::cout << "========= map ========================================="; pandas::CategoricalArray arr({"yes", "no", "yes"}); pandas::CategoricalIndex idx(arr); std::unordered_map mapping = {{"yes", "1"}, {"no", "0"}}; pandas::CategoricalIndex mapped = idx.map(mapping); bool passed = (mapped.has_category("1") && mapped.has_category("0") && !mapped.has_category("yes") && !mapped.has_category("no")); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_map()" << std::endl; throw std::runtime_error("pd_test_categorical_index_map failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-pipe-2: .. dropdown:: pipe (pd_test_1_all.cpp:11164) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11154 :emphasize-lines: 11 // Pipe applies function to entire Series auto add_mean = [](const pandas::Series& ser, double offset) { auto mean_val = ser.mean(); std::vector result; for (size_t i = 0; i < ser.size(); ++i) { result.push_back(ser[i] + mean_val.value_or(0.0) + offset); } return pandas::Series(result, ser.name()); }; auto result = s.pipe(add_mean, 10.0); bool passed = true; // mean is 2.5, offset is 10.0, so each value + 12.5 std::vector expected = {13.5, 14.5, 15.5, 16.5}; for (size_t i = 0; i < result.size(); ++i) { if (!approx_equal(result[i], expected[i])) { passed = false; std::cout << " [FAIL] : in pd_test_func_apply_series_pipe() : value mismatch at " << i << std::endl; throw std::runtime_error("pd_test_func_apply_series_pipe failed: value mismatch"); } .. _example-styler-concat-3: .. dropdown:: concat (pd_test_1_all.cpp:17717) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 17707 :emphasize-lines: 11 } void pd_test_period_index_concat() { std::cout << "========= concat factory =============================="; std::vector ordinals1 = {0, 1}; std::vector ordinals2 = {2, 3}; pandas::PeriodIndex idx1(ordinals1, "D"); pandas::PeriodIndex idx2(ordinals2, "D"); pandas::PeriodIndex concatenated = pandas::PeriodIndex::concat({idx1, idx2}); bool passed = (concatenated.size() == 4); if (!passed) { std::cout << " [FAIL] : in pd_test_period_index_concat()" << std::endl; throw std::runtime_error("pd_test_period_index_concat failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-to_html-4: .. dropdown:: to_html (pd_test_1_all.cpp:13438) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13428 :emphasize-lines: 11 void pd_test_io_to_html() { std::cout << "========= to_html ================================"; std::map> data; data["A"] = {1.0, 2.0}; data["B"] = {3.0, 4.0}; pandas::DataFrame df(data); std::string html = df.to_html(); // Check for key HTML elements bool has_table = (html.find("") != std::string::npos); bool has_tbody = (html.find("") != std::string::npos); bool has_th = (html.find("") != std::string::npos); bool has_td = (html.find("") != std::string::npos); bool passed = has_table && has_thead && has_tbody && has_th && has_td; .. _example-styler-to_html-5: .. dropdown:: to_html (pd_test_1_all.cpp:13438) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13428 :emphasize-lines: 11 void pd_test_io_to_html() { std::cout << "========= to_html ================================"; std::map> data; data["A"] = {1.0, 2.0}; data["B"] = {3.0, 4.0}; pandas::DataFrame df(data); std::string html = df.to_html(); // Check for key HTML elements bool has_table = (html.find("") != std::string::npos); bool has_tbody = (html.find("") != std::string::npos); bool has_th = (html.find("") != std::string::npos); bool has_td = (html.find("") != std::string::npos); bool passed = has_table && has_thead && has_tbody && has_th && has_td; .. _example-styler-to_latex-6: .. dropdown:: to_latex (pd_test_2_all.cpp:9446) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9436 :emphasize-lines: 11 void pd_test_styler_to_latex() { std::cout << "========= to_latex ================================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); auto styler = df.style(); std::string latex = styler.to_latex(); if (!contains(latex, "\\begin{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not produce tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not produce tabular environment"); } if (!contains(latex, "\\end{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not close tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not close tabular environment"); } .. _example-styler-to_latex-7: .. dropdown:: to_latex (pd_test_2_all.cpp:9446) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9436 :emphasize-lines: 11 void pd_test_styler_to_latex() { std::cout << "========= to_latex ================================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); auto styler = df.style(); std::string latex = styler.to_latex(); if (!contains(latex, "\\begin{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not produce tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not produce tabular environment"); } if (!contains(latex, "\\end{tabular}")) { std::cout << " [FAIL] : in pd_test_styler_to_latex() : did not close tabular environment" << std::endl; throw std::runtime_error("pd_test_styler_to_latex failed: did not close tabular environment"); } .. _example-styler-to_string-8: .. dropdown:: to_string (pd_test_1_all.cpp:2693) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2683 :emphasize-lines: 11 pandas::PeriodArray arr_m(std::vector{ "2020-01", "NaT", "2025-06" }, "M"); // Year auto years = arr_m.year(); auto y0 = years[0]; if (!y0.has_value() || y0.value() != 2020) { std::cout << " [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]"); } auto y1 = years[1]; if (y1.has_value()) { std::cout << " [FAIL] : year[1] should be NA (NaT)" << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA"); } auto y2 = years[2]; .. _example-styler-to_string-9: .. dropdown:: to_string (pd_test_1_all.cpp:2693) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2683 :emphasize-lines: 11 pandas::PeriodArray arr_m(std::vector{ "2020-01", "NaT", "2025-06" }, "M"); // Year auto years = arr_m.year(); auto y0 = years[0]; if (!y0.has_value() || y0.value() != 2020) { std::cout << " [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]"); } auto y1 = years[1]; if (y1.has_value()) { std::cout << " [FAIL] : year[1] should be NA (NaT)" << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA"); } auto y2 = years[2]; .. _example-styler-background_gradient-10: .. dropdown:: background_gradient (pd_test_2_all.cpp:9286) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9276 :emphasize-lines: 11 void pd_test_styler_background_gradient() { std::cout << "========= background_gradient ======================"; std::map> data = { {"A", {1.0, 2.0, 3.0, 4.0, 5.0}}, {"B", {10.0, 20.0, 30.0, 40.0, 50.0}} }; pandas::DataFrame df(data); auto styler = df.style().background_gradient(); std::string html = styler.to_html(); if (!contains(html, "background-color: #")) { std::cout << " [FAIL] : in pd_test_styler_background_gradient() : did not apply color styles" << std::endl; throw std::runtime_error("pd_test_styler_background_gradient failed: did not apply color styles"); } auto styler2 = df.style().background_gradient("Reds"); std::string html2 = styler2.to_html(); .. _example-styler-bar-11: .. dropdown:: bar (pd_test_2_all.cpp:9332) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9322 :emphasize-lines: 11 } void pd_test_styler_bar() { std::cout << "========= bar ======================================"; std::map> data = { {"A", {10.0, 30.0, 50.0, 70.0, 100.0}} }; pandas::DataFrame df(data); auto styler = df.style().bar(); std::string html = styler.to_html(); if (!contains(html, "linear-gradient")) { std::cout << " [FAIL] : in pd_test_styler_bar() : did not apply gradient background" << std::endl; throw std::runtime_error("pd_test_styler_bar failed: did not apply gradient background"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-clear-12: .. dropdown:: clear (pd_test_1_all.cpp:16341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16331 :emphasize-lines: 11 } // Test has_type passed = attrs.has_type("author") && !attrs.has_type("author"); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : has_type" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: has_type"); } // Test clear attrs.clear(); passed = attrs.empty(); if (!passed) { std::cout << " [FAIL] : in pd_test_ndframe_attrs() : clear" << std::endl; throw std::runtime_error("pd_test_ndframe_attrs failed: clear"); } std::cout << " -> tests passed" << std::endl; } // ===================================================================== .. _example-styler-data-13: .. dropdown:: data (pd_test_1_all.cpp:9114) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9104 :emphasize-lines: 11 throw std::runtime_error("pd_test_datetime_mixin_default_constructor failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_datetime_mixin_array_constructor() { std::cout << "========= DatetimeTDMixin array constructor ========================="; // Create DatetimeArray with some values numpy::NDArray data(std::vector{3}); data.setElementAt({0}, numpy::datetime64(1000000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2001 data.setElementAt({1}, numpy::datetime64(1500000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2017 data.setElementAt({2}, numpy::datetime64(1600000000000000000LL, numpy::DateTimeUnit::Nanosecond)); // ~2020 numpy::NDArray mask(std::vector{3}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); mask.setElementAt({2}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); .. _example-styler-format-14: .. dropdown:: format (main.cpp:20) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10 :emphasize-lines: 11 int main() { // Automatically log all output to temp/pd_test_output.log numpy::TestLogger logger("temp/pd_test_output.log"); int res = 0; int res1 = 0; std::string resS = ""; // call all the tests res1 = dataframe_tests::pd_test_main(); resS += std::format(" pd_test_main: {} errors\n", res1); res += res1; std::cout << "\n------------------------- main --------------------------------------------\n"; std::cout << std::endl << "All tests completed. Nb errors = " << res << std::endl; std::cout << "Details: \n" << resS; std::cout << "\n---------------------------------------------------------------------------\n"; return res; } .. _example-styler-format-15: .. dropdown:: format (main.cpp:20) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10 :emphasize-lines: 11 int main() { // Automatically log all output to temp/pd_test_output.log numpy::TestLogger logger("temp/pd_test_output.log"); int res = 0; int res1 = 0; std::string resS = ""; // call all the tests res1 = dataframe_tests::pd_test_main(); resS += std::format(" pd_test_main: {} errors\n", res1); res += res1; std::cout << "\n------------------------- main --------------------------------------------\n"; std::cout << std::endl << "All tests completed. Nb errors = " << res << std::endl; std::cout << "Details: \n" << resS; std::cout << "\n---------------------------------------------------------------------------\n"; return res; } .. _example-styler-hide-16: .. dropdown:: hide (pd_test_2_all.cpp:9420) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9410 :emphasize-lines: 11 std::cout << "========= hide ====================================="; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}}, {"C", {7.0, 8.0, 9.0}} }; pandas::DataFrame df(data); pandas::StyleSubset subset({"B"}); auto styler = df.style().hide(subset, 0); std::string html = styler.to_html(); if (contains(html, "B")) { std::cout << " [FAIL] : in pd_test_styler_hide() : did not hide column B" << std::endl; throw std::runtime_error("pd_test_styler_hide failed: did not hide column B"); } if (!contains(html, "A") || !contains(html, "C")) { std::cout << " [FAIL] : in pd_test_styler_hide() : removed wrong columns" << std::endl; throw std::runtime_error("pd_test_styler_hide failed: removed wrong columns"); .. _example-styler-highlight_between-17: .. dropdown:: highlight_between (pd_test_2_all.cpp:9266) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9256 :emphasize-lines: 11 void pd_test_styler_highlight_between() { std::cout << "========= highlight_between ========================"; std::map> data = { {"A", {1.0, 5.0, 10.0}}, {"B", {2.0, 6.0, 8.0}} }; pandas::DataFrame df(data); auto styler = df.style().highlight_between({}, "lightblue", std::nullopt, 3.0, 7.0); std::string html = styler.to_html(); if (!contains(html, "background-color: lightblue")) { std::cout << " [FAIL] : in pd_test_styler_highlight_between() : did not apply lightblue background" << std::endl; throw std::runtime_error("pd_test_styler_highlight_between failed: did not apply lightblue background"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-highlight_max-18: .. dropdown:: highlight_max (pd_test_2_all.cpp:9188) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9178 :emphasize-lines: 11 void pd_test_styler_highlight_max() { std::cout << "========= highlight_max ============================"; std::map> data = { {"A", {1.0, 3.0, 2.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // Highlight max with default color (yellow) auto styler = df.style().highlight_max(); std::string html = styler.to_html(); if (!contains(html, "background-color: yellow")) { std::cout << " [FAIL] : in pd_test_styler_highlight_max() : did not apply yellow background" << std::endl; throw std::runtime_error("pd_test_styler_highlight_max failed: did not apply yellow background"); } // Test with custom color auto styler2 = df.style().highlight_max({}, "lightgreen"); std::string html2 = styler2.to_html(); .. _example-styler-highlight_min-19: .. dropdown:: highlight_min (pd_test_2_all.cpp:9217) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9207 :emphasize-lines: 11 void pd_test_styler_highlight_min() { std::cout << "========= highlight_min ============================"; std::map> data = { {"A", {1.0, 3.0, 2.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); auto styler = df.style().highlight_min(); std::string html = styler.to_html(); if (!contains(html, "background-color: yellow")) { std::cout << " [FAIL] : in pd_test_styler_highlight_min() : did not apply yellow background" << std::endl; throw std::runtime_error("pd_test_styler_highlight_min failed: did not apply yellow background"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-highlight_null-20: .. dropdown:: highlight_null (pd_test_2_all.cpp:9237) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9227 :emphasize-lines: 11 void pd_test_styler_highlight_null() { std::cout << "========= highlight_null ==========================="; std::map> data = { {"A", {"1.0", "NaN", "3.0"}}, {"B", {"4.0", "5.0", "NA"}} }; pandas::DataFrame df(data); auto styler = df.style().highlight_null(); std::string html = styler.to_html(); if (!contains(html, "background-color: red")) { std::cout << " [FAIL] : in pd_test_styler_highlight_null() : did not apply red background" << std::endl; throw std::runtime_error("pd_test_styler_highlight_null failed: did not apply red background"); } // Test with custom color auto styler2 = df.style().highlight_null("orange"); std::string html2 = styler2.to_html(); .. _example-styler-highlight_quantile-21: .. dropdown:: highlight_quantile (pd_test_2_all.cpp:9672) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9662 :emphasize-lines: 11 } void pd_test_styler_highlight_quantile() { std::cout << "========= highlight_quantile ======================="; std::map> data = { {"A", {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}} }; pandas::DataFrame df(data); auto styler = df.style().highlight_quantile({}, "lightgreen", 0, 0.8, 1.0); std::string html = styler.to_html(); if (!contains(html, "background-color: lightgreen")) { std::cout << " [FAIL] : in pd_test_styler_highlight_quantile() : did not apply highlight" << std::endl; throw std::runtime_error("pd_test_styler_highlight_quantile failed: did not apply highlight"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-render-22: .. dropdown:: render (pd_test_2_all.cpp:9161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9151 :emphasize-lines: 11 std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // Get Styler auto styler = df.style(); // Check that render produces HTML std::string html = styler.render(); if (!contains(html, "")) { std::cout << " [FAIL] : in pd_test_styler_basic() : render() did not produce table tag" << std::endl; throw std::runtime_error("pd_test_styler_basic failed: render() did not produce table tag"); } if (!contains(html, "")) { std::cout << " [FAIL] : in pd_test_styler_basic() : render() did not include column header A" << std::endl; throw std::runtime_error("pd_test_styler_basic failed: render() did not include column header A"); } if (!contains(html, "")) { std::cout << " [FAIL] : in pd_test_styler_basic() : render() did not include column header B" << std::endl; .. _example-styler-set_caption-23: .. dropdown:: set_caption (pd_test_2_all.cpp:9370) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9360 :emphasize-lines: 11 } void pd_test_styler_set_caption() { std::cout << "========= set_caption =============================="; std::map> data = { {"A", {1.0, 2.0, 3.0}} }; pandas::DataFrame df(data); auto styler = df.style().set_caption("My Data Table"); std::string html = styler.to_html(); if (!contains(html, "")) { std::cout << " [FAIL] : in pd_test_styler_set_caption() : did not add caption element" << std::endl; throw std::runtime_error("pd_test_styler_set_caption failed: did not add caption element"); } std::cout << " -> tests passed" << std::endl; } .. _example-styler-set_properties-24: .. dropdown:: set_properties (pd_test_2_all.cpp:9394) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9384 :emphasize-lines: 11 std::map> data = { {"A", {1.0, 2.0}}, {"B", {3.0, 4.0}} }; pandas::DataFrame df(data); std::map props = { {"font-weight", "bold"}, {"text-align", "center"} }; auto styler = df.style().set_properties(props); std::string html = styler.to_html(); if (!contains(html, "font-weight: bold")) { std::cout << " [FAIL] : in pd_test_styler_set_properties() : did not apply font-weight" << std::endl; throw std::runtime_error("pd_test_styler_set_properties failed: did not apply font-weight"); } if (!contains(html, "text-align: center")) { std::cout << " [FAIL] : in pd_test_styler_set_properties() : did not apply text-align" << std::endl; throw std::runtime_error("pd_test_styler_set_properties failed: did not apply text-align"); } .. _example-styler-set_table_styles-25: .. dropdown:: set_table_styles (pd_test_2_all.cpp:9696) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9686 :emphasize-lines: 11 std::map> data = { {"A", {1.0, 2.0, 3.0}} }; pandas::DataFrame df(data); std::vector> styles = { {"th", "background-color: lightgray;"}, {"td", "padding: 10px;"} }; auto styler = df.style().set_table_styles(styles); std::string html = styler.to_html(); if (!contains(html, "
AB
My Data Table