Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip REF: redirect df.to_html to Styler if new kwargs are used. #45090

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5b61597
render links
attack68 Dec 24, 2021
fe8dd37
tests
attack68 Dec 24, 2021
11f20eb
whats new
attack68 Dec 24, 2021
a7b3c59
multipl links test
attack68 Dec 24, 2021
bf9b9c2
add LaTeX. chg to `hyperlinks` instead of `render_links`
attack68 Dec 25, 2021
ea9c6a6
whats new update
attack68 Dec 25, 2021
d331a82
extend docs
attack68 Dec 25, 2021
1abc6d9
html deprecation implementation.
attack68 Dec 25, 2021
143f629
add tests, remove conflicting args input
attack68 Dec 27, 2021
b47ac53
Merge remote-tracking branch 'upstream/master' into styler_df_to_html2
attack68 Dec 28, 2021
e8bd87b
rewrite the docs adding deprecations.
attack68 Dec 29, 2021
1fff52a
rewrite the docs adding deprecations.
attack68 Dec 29, 2021
e8f576b
rewrite the docs adding deprecations.
attack68 Dec 29, 2021
1c4528e
rewrite the docs adding deprecations.
attack68 Dec 29, 2021
a43e0a5
rewrite the docs adding deprecations.
attack68 Dec 30, 2021
7057c50
doc warnings
attack68 Jan 2, 2022
182157c
whats new
attack68 Jan 2, 2022
e81dd8b
Merge remote-tracking branch 'upstream/master' into styler_df_to_html2
attack68 Jan 2, 2022
5bf5727
whats new
attack68 Jan 2, 2022
5e2f2a3
fix doc string errors
attack68 Jan 2, 2022
fe2b222
mypy fix
attack68 Jan 2, 2022
c6b74c5
skip no jinja2
attack68 Jan 2, 2022
cf7e5e0
tests for conditional warning based on arg input
attack68 Jan 2, 2022
0972a2c
Merge remote-tracking branch 'upstream/master' into styler_df_to_html2
attack68 Jan 2, 2022
e57c26a
use conventional sphinx links
attack68 Jan 3, 2022
68918aa
fix docstring error
attack68 Jan 3, 2022
4cfc9bf
Merge remote-tracking branch 'upstream/master' into styler_df_to_html2
attack68 Jan 4, 2022
60b81c4
Merge remote-tracking branch 'upstream/master' into styler_df_to_html2
attack68 Jan 5, 2022
a37747f
push to 1.5.0
attack68 Jan 5, 2022
568a92c
push to 1.5.0
attack68 Jan 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 17 additions & 139 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The pandas I/O API is a set of top level ``reader`` functions accessed like
text;`CSV <https://en.wikipedia.org/wiki/Comma-separated_values>`__;:ref:`read_csv<io.read_csv_table>`;:ref:`to_csv<io.store_in_csv>`
text;Fixed-Width Text File;:ref:`read_fwf<io.fwf_reader>`
text;`JSON <https://www.json.org/>`__;:ref:`read_json<io.json_reader>`;:ref:`to_json<io.json_writer>`
text;`HTML <https://en.wikipedia.org/wiki/HTML>`__;:ref:`read_html<io.read_html>`;:ref:`to_html<io.html>`
text;`HTML <https://en.wikipedia.org/wiki/HTML>`__;:ref:`read_html<io.read_html>`;:ref:`Styler.to_html<io.html>`
text;`LaTeX <https://en.wikipedia.org/wiki/LaTeX>`__;;:ref:`Styler.to_latex<io.latex>`
text;`XML <https://www.w3.org/standards/xml/core>`__;:ref:`read_xml<io.read_xml>`;:ref:`to_xml<io.xml>`
text; Local clipboard;:ref:`read_clipboard<io.clipboard>`;:ref:`to_clipboard<io.clipboard>`
Expand Down Expand Up @@ -2682,8 +2682,8 @@ Read in pandas ``to_html`` output (with some loss of floating point precision):
.. code-block:: python

df = pd.DataFrame(np.random.randn(2, 2))
s = df.to_html(float_format="{0:.40g}".format)
dfin = pd.read_html(s, index_col=0)
s = df.style.format("{0:.40g}").to_html()
dfin = pd.read_html(s, index_col=0)[0]

The ``lxml`` backend will raise an error on a failed parse if that is the only
parser you provide. If you only have a single parser you can provide just a
Expand Down Expand Up @@ -2714,156 +2714,34 @@ succeeds, the function will return*.
Writing to HTML files
''''''''''''''''''''''

``DataFrame`` objects have an instance method ``to_html`` which renders the
contents of the ``DataFrame`` as an HTML table. The function arguments are as
in the method ``to_string`` described above.

.. note::

Not all of the possible options for ``DataFrame.to_html`` are shown here for
brevity's sake. See :func:`~pandas.core.frame.DataFrame.to_html` for the
full set of options.
DataFrame *and* Styler objects currently have a ``to_html`` method. We recommend
using the :meth:`Styler.to_html <pandas.io.formats.style.Styler.to_html>` method
over :meth:`DataFrame.to_html` due to the former's greater flexibility with
conditional styling, and the latter's possible argument signature change and/or future deprecation.

.. ipython:: python
:suppress:
Review the documentation for :meth:`Styler.to_html <pandas.io.formats.style.Styler.to_html>`,
which gives examples of conditional styling and explains the operation of its keyword
arguments. The ``to_html`` methods render the contents of the ``DataFrame`` as an HTML table.

def write_html(df, filename, *args, **kwargs):
static = os.path.abspath(os.path.join("source", "_static"))
with open(os.path.join(static, filename + ".html"), "w") as f:
df.to_html(f, *args, **kwargs)
For simple application the following pattern is sufficient:

.. ipython:: python

df = pd.DataFrame(np.random.randn(2, 2))
df
print(df.to_html()) # raw html

.. ipython:: python
:suppress:

write_html(df, "basic")

HTML:

.. raw:: html
:file: ../_static/basic.html
print(df.style.to_html()) # raw html

The ``columns`` argument will limit the columns shown:
To format values before output, chain the :meth:`Styler.format <pandas.io.formats.style.Styler.format>`
and :meth:`Styler.format_index <pandas.io.formats.style.Styler.format_index>` methods.

.. ipython:: python

print(df.to_html(columns=[0]))

.. ipython:: python
:suppress:

write_html(df, "columns", columns=[0])

HTML:

.. raw:: html
:file: ../_static/columns.html

``float_format`` takes a Python callable to control the precision of floating
point values:

.. ipython:: python

print(df.to_html(float_format="{0:.10f}".format))

.. ipython:: python
:suppress:

write_html(df, "float_format", float_format="{0:.10f}".format)

HTML:

.. raw:: html
:file: ../_static/float_format.html

``bold_rows`` will make the row labels bold by default, but you can turn that
off:

.. ipython:: python

print(df.to_html(bold_rows=False))

.. ipython:: python
:suppress:

write_html(df, "nobold", bold_rows=False)

.. raw:: html
:file: ../_static/nobold.html

The ``classes`` argument provides the ability to give the resulting HTML
table CSS classes. Note that these classes are *appended* to the existing
``'dataframe'`` class.

.. ipython:: python

print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"]))

The ``render_links`` argument provides the ability to add hyperlinks to cells
that contain URLs.

.. ipython:: python

url_df = pd.DataFrame(
{
"name": ["Python", "pandas"],
"url": ["https://www.python.org/", "https://pandas.pydata.org"],
}
)
print(url_df.to_html(render_links=True))

.. ipython:: python
:suppress:

write_html(url_df, "render_links", render_links=True)

HTML:

.. raw:: html
:file: ../_static/render_links.html

Finally, the ``escape`` argument allows you to control whether the
"<", ">" and "&" characters escaped in the resulting HTML (by default it is
``True``). So to get the HTML without escaped characters pass ``escape=False``

.. ipython:: python

df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)})


.. ipython:: python
:suppress:

write_html(df, "escape")
write_html(df, "noescape", escape=False)

Escaped:

.. ipython:: python

print(df.to_html())

.. raw:: html
:file: ../_static/escape.html

Not escaped:

.. ipython:: python

print(df.to_html(escape=False))

.. raw:: html
:file: ../_static/noescape.html

.. note::
print(df.style.format("€ {}").to_html())

Some browsers may not show a difference in the rendering of the previous two
HTML tables.
Some browsers or browser applications may process and add css class styling by default to alter the appearance
of HTML tables, such as Jupyter Notebook and Google Colab.


.. _io.html.gotchas:
Expand Down
2 changes: 2 additions & 0 deletions doc/source/user_guide/scale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ column names and dtypes. That's because Dask hasn't actually read the data yet.
Rather than executing immediately, doing operations build up a **task graph**.

.. ipython:: python
:okwarning:

ddf
ddf["name"]
Expand Down Expand Up @@ -333,6 +334,7 @@ known automatically. In this case, since we created the parquet files manually,
we need to supply the divisions manually.

.. ipython:: python
:okwarning:

N = 12
starts = [f"20{i:>02d}-01-01" for i in range(N)]
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ Other Deprecations
- Deprecated the behavior of :func:`to_datetime` with the string "now" with ``utc=False``; in a future version this will match ``Timestamp("now")``, which in turn matches :meth:`Timestamp.now` returning the local time (:issue:`18705`)
- Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`)
- Deprecated parameter ``names`` in :meth:`Index.copy` (:issue:`44916`)
- A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`)
- A deprecation warning is now shown for both :meth:`DataFrame.to_html` and :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments in :meth:`.Styler.to_html` and :meth:`.Styler.to_latex`, respectively, in future versions (:issue:`44411`, :issue:`44451`)
- Deprecated behavior of :func:`concat` between objects with bool-dtype and numeric-dtypes; in a future version these will cast to object dtype instead of coercing bools to numeric values (:issue:`39817`)
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
- Deprecated passing ``set`` or ``dict`` as indexer for :meth:`DataFrame.loc.__setitem__`, :meth:`DataFrame.loc.__getitem__`, :meth:`Series.loc.__setitem__`, :meth:`Series.loc.__getitem__`, :meth:`DataFrame.__getitem__`, :meth:`Series.__getitem__` and :meth:`Series.__setitem__` (:issue:`42825`)
Expand Down
Loading