Styler ====== .. cpp:class:: numpy::Styler numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Styler Styler obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit Styler(const DataFrame& data)`` - df_styler.h:166 - * - ``Styler(const Styler& other)`` - df_styler.h:171 - 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 - df_styler.h:136 - * - ``size_t get_col_index(const std::string& col_name) const`` - size_t - df_styler.h:129 - * - ``std::string get_column_name(size_t col) const`` - std::string - df_styler.h:142 - * - ``std::string get_index_str(size_t row) const`` - std::string - df_styler.h:139 - Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector> export_styles() const`` - std::vector> - df_styler.h:501 - 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 - df_styler.h:145 - * - ``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 - df_styler.h:529 - * - ``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 - df_styler.h:542 - * - ``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 - df_styler.h:573 - * - ``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 - df_styler.h:592 - * - ``std::string to_string(bool sparse_index = true, bool sparse_columns = true) const`` - std::string - df_styler.h:616 - :ref:`View ` * - ``void to_string(std::ostream& buf, bool sparse_index = true, bool sparse_columns = true) const`` - void - df_styler.h:622 - :ref:`View ` Other Methods ------------- .. 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& - df_styler.h:190 - * - ``Styler& background_gradient(const std::string& cmap = "PuBu", double low = 0.0, double high = 0.0, std::optional axis = 0, const StyleSubset& subset =`` - Styler& - df_styler.h:304 - * - ``Styler& bar(const StyleSubset& subset =`` - Styler& - df_styler.h:344 - * - ``Styler& clear()`` - Styler& - df_styler.h:494 - :ref:`View ` * - ``Styler& concat(const Styler& other)`` - Styler& - df_styler.h:636 - * - ``const DataFrame& data() const`` - const DataFrame& - df_styler.h:652 - :ref:`View ` * - ``void ensure_style_matrices()`` - void - df_styler.h:126 - * - ``static std::string escape_html(const std::string& str)`` - static std::string - df_styler.h:148 - * - ``Styler& format(const std::string& formatter = "", const StyleSubset& subset =`` - Styler& - df_styler.h:369 - :ref:`View ` * - ``Styler& format(const std::map& formatters, const std::string& na_rep = "", int precision = -1, bool escape = true)`` - Styler& - df_styler.h:379 - :ref:`View ` * - ``Styler& hide(const StyleSubset& subset =`` - Styler& - df_styler.h:480 - * - ``Styler& highlight_between(const StyleSubset& subset =`` - Styler& - df_styler.h:259 - * - ``Styler& highlight_max(const StyleSubset& subset =`` - Styler& - df_styler.h:228 - * - ``Styler& highlight_min(const StyleSubset& subset =`` - Styler& - df_styler.h:242 - * - ``Styler& highlight_null(const std::string& color = "red", const StyleSubset& subset =`` - Styler& - df_styler.h:215 - * - ``Styler& highlight_quantile(const StyleSubset& subset =`` - Styler& - df_styler.h:279 - * - ``static std::string interpolate_color(double normalized, const std::string& low_color, const std::string& high_color)`` - static std::string - df_styler.h:151 - * - ``Styler& map(CellStyleFunc func, const StyleSubset& subset =`` - Styler& - df_styler.h:201 - * - ``static std::tuple parse_color(const std::string& color)`` - static std::tuple - df_styler.h:156 - * - ``auto pipe(Func func) -> decltype(func(\*this))`` - auto - df_styler.h:645 - * - ``std::string render() const`` - std::string - df_styler.h:657 - * - ``std::pair, std::vector> resolve_subset( const StyleSubset& subset) const`` - std::pair, std::vector> - df_styler.h:132 - * - ``static std::string rgb_to_hex(int r, int g, int b)`` - static std::string - df_styler.h:159 - * - ``Styler& set_caption(const std::string& caption)`` - Styler& - df_styler.h:433 - * - ``Styler& set_properties(const std::map& props, const StyleSubset& subset =`` - Styler& - df_styler.h:395 - * - ``Styler& set_sticky(std::optional axis = 0, std::optional pixel_size = std::nullopt, const std::vector& levels =`` - Styler& - df_styler.h:463 - * - ``Styler& set_table_attributes(const std::string& attributes)`` - Styler& - df_styler.h:417 - * - ``Styler& set_table_styles( const std::vector>& table_styles, std::optional axis = std::nullopt, bool overwrite = false)`` - Styler& - df_styler.h:406 - * - ``Styler& set_td_classes(const std::vector>& classes)`` - Styler& - df_styler.h:425 - * - ``Styler& set_tooltips(const std::vector>& ttips, const std::string& props = "", const std::string& css_class = "")`` - Styler& - df_styler.h:443 - * - ``Styler& set_uuid(const std::string& uuid)`` - Styler& - df_styler.h:453 - * - ``Styler& text_gradient(const std::string& cmap = "PuBu", double low = 0.0, double high = 0.0, std::optional axis = 0, const StyleSubset& subset =`` - Styler& - df_styler.h:318 - * - ``Styler& use(const std::vector>& styles)`` - Styler& - df_styler.h:509 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-styler-to_string-0: .. dropdown:: to_string (np_test_1_all.cpp:454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 444 :emphasize-lines: 11 // Modify through different views view1.setElementAt({0, 0}, 100); view2.setElementAt({2, 1}, 200); // This is (1, 2) in original view3.setElementAt({0, 0}, 300); // This is (1, 1) in original // std::cout << "After modifications through multiple views:" << std::endl; //original.printArray(); // Verify all changes are reflected if (!(original.getElementAt({0, 0}) == 100)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 2}) == 200)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 1}) == 300)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)"; .. _example-styler-to_string-1: .. dropdown:: to_string (np_test_1_all.cpp:454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 444 :emphasize-lines: 11 // Modify through different views view1.setElementAt({0, 0}, 100); view2.setElementAt({2, 1}, 200); // This is (1, 2) in original view3.setElementAt({0, 0}, 300); // This is (1, 1) in original // std::cout << "After modifications through multiple views:" << std::endl; //original.printArray(); // Verify all changes are reflected if (!(original.getElementAt({0, 0}) == 100)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 2}) == 200)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 1}) == 300)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)"; .. _example-styler-clear-2: .. dropdown:: clear (np_test_2_all.cpp:4161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4151 :emphasize-lines: 11 auto array = createFloat32Array({ 10, 10 }, static_cast(i)); temp_arrays.push_back(array); auto view = array.view(); temp_views.push_back(view); } // std::cout << "Created 100 arrays and 100 views" << std::endl; // Clear all arrays (views will keep memory alive) temp_arrays.clear(); // std::cout << "Cleared original arrays" << std::endl; // Verify views still work if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) { std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Views still valid after originals cleared"; .. _example-styler-data-3: .. dropdown:: data (np_test_1_all.cpp:2084) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2074 :emphasize-lines: 11 } if (!(derived_ptr->extra == 2)) { std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(derived_ptr->extra == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test 6: Large object handling struct LargeObject { std::vector data; LargeObject() : data(10000, 3.14159) {} // Large object }; LargeObject large; object_ large_obj(large); // Should handle large objects if (!(!large_obj.is_null())) { std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!large_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(large_obj.is_type())) { .. _example-styler-format-4: .. dropdown:: format (main.cpp:22) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12 :emphasize-lines: 11 int main() { // Automatically log all output to temp/test_output.log numpy::TestLogger logger("temp/test_output.log"); int res = 0; int res1 = 0; std::string resS = ""; // call all the tests res1 = numpy_tests::np_test_main(); resS += std::format(" np_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-5: .. dropdown:: format (main.cpp:22) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12 :emphasize-lines: 11 int main() { // Automatically log all output to temp/test_output.log numpy::TestLogger logger("temp/test_output.log"); int res = 0; int res1 = 0; std::string resS = ""; // call all the tests res1 = numpy_tests::np_test_main(); resS += std::format(" np_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; }