From 62042f4b4491f4afdc0b317856bb4f64e2d7c4b5 Mon Sep 17 00:00:00 2001 From: jrycw Date: Sat, 5 Oct 2024 15:00:57 +0800 Subject: [PATCH 01/10] Add `GT.write_html()` function --- great_tables/_export.py | 47 +++++++++++++++++++++++++++++++++++++++++ great_tables/gt.py | 3 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/great_tables/_export.py b/great_tables/_export.py index 0e033dd03..2bc36a571 100644 --- a/great_tables/_export.py +++ b/great_tables/_export.py @@ -445,3 +445,50 @@ def _dump_debug_screenshot(driver, path): "document.getElementsByTagName('table')[0].style.border = '3px solid green'; " ) driver.save_screenshot(path) + + +def write_html( + gt: GT, filename: str | Path, encoding: str = "utf-8", newline: str | None = None +) -> 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 + This function does not return a value; it writes the HTML to the specified file path. + """ + import os + + _html = """ + + + + + + + + {} + + +""".format( + gt.as_raw_html() + ) + + newline = newline if newline is not None else os.linesep + + with open(filename, "w", encoding=encoding, newline=newline) as f: + f.write(_html) diff --git a/great_tables/gt.py b/great_tables/gt.py index 5715720a9..3e717281f 100644 --- a/great_tables/gt.py +++ b/great_tables/gt.py @@ -10,7 +10,7 @@ from great_tables._body import body_reassemble from great_tables._boxhead import cols_align, cols_label from great_tables._data_color import data_color -from great_tables._export import as_raw_html, save, show +from great_tables._export import as_raw_html, save, show, write_html from great_tables._formats import ( fmt, fmt_bytes, @@ -270,6 +270,7 @@ def __init__( save = save show = show as_raw_html = as_raw_html + write_html = write_html # ----- From 135b23215da51fbfd74c2867e6bfdfd8d99f3b0d Mon Sep 17 00:00:00 2001 From: jrycw Date: Sat, 5 Oct 2024 15:02:29 +0800 Subject: [PATCH 02/10] Add tests for `GT.write_html()` function --- tests/test_export.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_export.py b/tests/test_export.py index a382912a3..d8eb38499 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -96,3 +96,18 @@ def test_create_temp_file_server(): r.content.decode() == "abc" thread.join() + + +def test_write_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_html(p_file) + assert p_file.exists() + + # Pass the filename as a string + import os + + s_file = str(Path(tmp_dir, "table2.html")) + gt_tbl.write_html(s_file) + assert os.path.exists(s_file) From ecff5da618be82281d70dd93fbf7fcfc210e2c03 Mon Sep 17 00:00:00 2001 From: jrycw Date: Sat, 5 Oct 2024 19:42:19 +0800 Subject: [PATCH 03/10] update _quarto.yml to expose `GT.write_html()` --- docs/_quarto.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/_quarto.yml b/docs/_quarto.yml index 344e9a837..dddb28825 100644 --- a/docs/_quarto.yml +++ b/docs/_quarto.yml @@ -208,6 +208,7 @@ quartodoc: - GT.save - GT.show - GT.as_raw_html + - GT.write_html - title: Value formatting functions desc: > If you have single values (or lists of them) in need of formatting, we have a set of From 025ac6953d277cb8e4e05a7c1cf9afab5bdc4bde Mon Sep 17 00:00:00 2001 From: jrycw Date: Mon, 7 Oct 2024 14:45:36 +0800 Subject: [PATCH 04/10] Remove usage of the `os` module in tests for `GT.write_html()` --- tests/test_export.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index d8eb38499..3b536dea5 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -106,8 +106,6 @@ def test_write_html(gt_tbl): assert p_file.exists() # Pass the filename as a string - import os - s_file = str(Path(tmp_dir, "table2.html")) gt_tbl.write_html(s_file) - assert os.path.exists(s_file) + assert Path(s_file).exists() From ceb8e6026e489b586606f948a79106674a4cb592 Mon Sep 17 00:00:00 2001 From: jrycw Date: Wed, 16 Oct 2024 02:28:55 +0800 Subject: [PATCH 05/10] Leverage the parameters of `GT.as_raw_html()` --- great_tables/_export.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/great_tables/_export.py b/great_tables/_export.py index 2bc36a571..f01c1472a 100644 --- a/great_tables/_export.py +++ b/great_tables/_export.py @@ -448,7 +448,12 @@ def _dump_debug_screenshot(driver, path): def write_html( - gt: GT, filename: str | Path, encoding: str = "utf-8", newline: str | None = None + 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. @@ -473,22 +478,9 @@ def write_html( """ import os - _html = """ - - - - - - - - {} - - -""".format( - gt.as_raw_html() - ) + 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) + f.write(html_content) From 91dc2d960c408cae9cdfc014a5ba53c80ec32815 Mon Sep 17 00:00:00 2001 From: jrycw Date: Wed, 13 Nov 2024 14:46:28 +0800 Subject: [PATCH 06/10] Update `.write_html()` to return a string if no filename is specified --- great_tables/_export.py | 12 ++++++++---- tests/test_export.py | 6 +++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/great_tables/_export.py b/great_tables/_export.py index f01c1472a..b731ec709 100644 --- a/great_tables/_export.py +++ b/great_tables/_export.py @@ -449,12 +449,12 @@ def _dump_debug_screenshot(driver, path): def write_html( gt: GT, - filename: str | Path, + filename: str | Path | None = None, encoding: str = "utf-8", newline: str | None = None, make_page: bool = False, all_important: bool = False, -) -> None: +) -> str | None: """ Write the table to an HTML file. @@ -467,6 +467,7 @@ def write_html( A GT object. filename The name of the file to save the HTML. Can be a string or a `pathlib.Path` object. + If set to None (default), the output is returned as a string instead. encoding The encoding used when writing the file. Defaults to 'utf-8'. newline @@ -476,10 +477,13 @@ def write_html( None This function does not return a value; it writes the HTML to the specified file path. """ - import os - html_content = as_raw_html(gt, make_page=make_page, all_important=all_important) + if filename is None: + return html_content + + import os + newline = newline if newline is not None else os.linesep with open(filename, "w", encoding=encoding, newline=newline) as f: diff --git a/tests/test_export.py b/tests/test_export.py index 3b536dea5..633b2f0b1 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -5,7 +5,7 @@ import time from great_tables import GT, exibble, md -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 @@ -98,6 +98,10 @@ def test_create_temp_file_server(): thread.join() +def test_write_html_default(gt_tbl): + assert as_raw_html(gt_tbl) == gt_tbl.write_html() + + def test_write_html(gt_tbl): with tempfile.TemporaryDirectory() as tmp_dir: # pass the filename as a pathlib.Path() object From 529cc0f500010bcb729a04c6f685a9ddc054796f Mon Sep 17 00:00:00 2001 From: jrycw Date: Wed, 13 Nov 2024 14:51:38 +0800 Subject: [PATCH 07/10] Update _quarto.yml --- docs/_quarto.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/_quarto.yml b/docs/_quarto.yml index dddb28825..186bde4c7 100644 --- a/docs/_quarto.yml +++ b/docs/_quarto.yml @@ -203,11 +203,10 @@ 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 `write_html()` method. contents: - GT.save - GT.show - - GT.as_raw_html - GT.write_html - title: Value formatting functions desc: > From cfcbdebffe993264bd8e1ed64e0849f80dcf1ff0 Mon Sep 17 00:00:00 2001 From: jrycw Date: Wed, 13 Nov 2024 15:01:03 +0800 Subject: [PATCH 08/10] Import `write_html()` to gt.py --- great_tables/gt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/great_tables/gt.py b/great_tables/gt.py index 769ebe89c..04e378d9a 100644 --- a/great_tables/gt.py +++ b/great_tables/gt.py @@ -10,7 +10,7 @@ from great_tables._body import body_reassemble from great_tables._boxhead import cols_align, cols_label from great_tables._data_color import data_color -from great_tables._export import as_raw_html, as_latex, save, show +from great_tables._export import as_raw_html, as_latex, save, show, write_html from great_tables._formats import ( fmt, From 83d3adf3eba68e00fc9b179dd29942de117d1e32 Mon Sep 17 00:00:00 2001 From: jrycw Date: Wed, 13 Nov 2024 15:07:09 +0800 Subject: [PATCH 09/10] Fix pre-commit --- tests/test_export.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index 0d8489793..02e91179c 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -100,7 +100,6 @@ def test_create_temp_file_server(): thread.join() - def test_write_html_default(gt_tbl): assert as_raw_html(gt_tbl) == gt_tbl.write_html() @@ -117,6 +116,7 @@ def test_write_html(gt_tbl): gt_tbl.write_html(s_file) assert Path(s_file).exists() + def test_snap_as_latex(snapshot): gt_tbl = ( @@ -135,4 +135,3 @@ def test_snap_as_latex(snapshot): latex_str_as_latex = gt_tbl.as_latex(use_longtable=True) assert snapshot == latex_str_as_latex - From d32044bdd26e4ba6caf1a85f9bec093489e04be1 Mon Sep 17 00:00:00 2001 From: jrycw Date: Wed, 13 Nov 2024 15:32:21 +0800 Subject: [PATCH 10/10] Update docstrings for `write_html()` --- great_tables/_export.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/great_tables/_export.py b/great_tables/_export.py index b1c57c2a9..6833694dc 100644 --- a/great_tables/_export.py +++ b/great_tables/_export.py @@ -561,15 +561,16 @@ def write_html( A GT object. filename The name of the file to save the HTML. Can be a string or a `pathlib.Path` object. - If set to None (default), the output is returned as a string instead. + If set to `None` (default), the output is returned as a string instead. 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 - This function does not return a value; it writes the HTML to the specified file path. + str | None + If `filename` is `None` (default), returns the HTML output as a string. Otherwise, writes + the HTML to the specified file path and returns `None`. """ html_content = as_raw_html(gt, make_page=make_page, all_important=all_important)