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

Add GT.write_raw_html() as a helper function for easier HTML output #485

Merged
merged 16 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,12 @@ quartodoc:
desc: >
There may come a day when you need to export a table to some specific format. A great method
for that is `save()`, which allows us to save the table as a standalone image file. You can
also get the table code as an HTML fragment with the `as_raw_html()` method.
also get the table code as an HTML fragment with the `*_raw_html()` methods.
contents:
- GT.save
- GT.show
- GT.as_raw_html
- GT.write_raw_html
jrycw marked this conversation as resolved.
Show resolved Hide resolved
- GT.as_latex
- title: Pipeline
desc: >
Expand Down
39 changes: 39 additions & 0 deletions great_tables/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,42 @@ def _dump_debug_screenshot(driver, path):
"document.getElementsByTagName('table')[0].style.border = '3px solid green'; "
)
driver.save_screenshot(path)


def write_raw_html(
gt: GT,
filename: str | Path,
encoding: str = "utf-8",
newline: str | None = None,
make_page: bool = False,
all_important: bool = False,
) -> None:
"""
Write the table to an HTML file.

This helper function saves the output of `GT.as_raw_html()` to an HTML file specified by the
user.

Parameters
----------
gt
A GT object.
filename
The name of the file to save the HTML. Can be a string or a `pathlib.Path` object.
encoding
The encoding used when writing the file. Defaults to 'utf-8'.
newline
The newline character to use when writing the file. Defaults to `os.linesep`.
Returns
-------
None
An HTML file is written to the specified path and the method returns `None`.
"""
import os

html_content = as_raw_html(gt, make_page=make_page, all_important=all_important)

newline = newline if newline is not None else os.linesep

with open(filename, "w", encoding=encoding, newline=newline) as f:
f.write(html_content)
3 changes: 2 additions & 1 deletion great_tables/gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ._body import body_reassemble
from ._boxhead import cols_align, cols_label
from ._data_color import data_color
from ._export import as_latex, as_raw_html, save, show
from ._export import as_latex, as_raw_html, save, show, write_raw_html
from ._formats import (
fmt,
fmt_bytes,
Expand Down Expand Up @@ -273,6 +273,7 @@ def __init__(
save = save
show = show
as_raw_html = as_raw_html
write_raw_html = write_raw_html
as_latex = as_latex

pipe = pipe
Expand Down
21 changes: 20 additions & 1 deletion tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from great_tables import GT, exibble, md
from great_tables.data import gtcars
from great_tables._export import _infer_render_target, _create_temp_file_server
from great_tables._export import as_raw_html, _infer_render_target, _create_temp_file_server

from pathlib import Path

from IPython.terminal.interactiveshell import TerminalInteractiveShell, InteractiveShell
Expand Down Expand Up @@ -121,6 +122,24 @@ def test_create_temp_file_server():
thread.join()


def test_write_raw_html_raises(gt_tbl):
with pytest.raises(TypeError):
gt_tbl.write_raw_html() # `filename=` must be specified


def test_write_raw_html(gt_tbl):
with tempfile.TemporaryDirectory() as tmp_dir:
# pass the filename as a pathlib.Path() object
p_file = Path(tmp_dir, "table1.html")
gt_tbl.write_raw_html(p_file)
assert p_file.exists()

# Pass the filename as a string
s_file = str(Path(tmp_dir, "table2.html"))
gt_tbl.write_raw_html(s_file)
assert Path(s_file).exists()


def test_snap_as_latex(snapshot):
gt_tbl = (
GT(
Expand Down
Loading