posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.81k stars 60 forks source link

Encoding issue on windows when saving a table #440

Open bart-r-willems opened 2 weeks ago

bart-r-willems commented 2 weeks ago

Description

When saving a table with the save message, an exception occurs in Windows.

Reproducible example

The bottom of the stack trace shows this

File D:\Python\Python312\Lib\site-packages\great_tables\_export.py:310, in save(self, file, selector, scale, expand, web_driver, window_size, debug_port, _debug_dump)
    303 with (
    304     tempfile.TemporaryDirectory() as tmp_dir,
    305     wdriver(options=wd_options) as headless_browser,
    306 ):
    307 
    308     # Write the HTML content to the temp file
    309     with open(f"{tmp_dir}/table.html", "w") as temp_file:
--> 310         temp_file.write(html_content)
    312     # Open the HTML file in the headless browser
    313     headless_browser.set_window_size(window_size[0], window_size[1])

File D:\Python\Python312\Lib\encodings\cp1252.py:19, in IncrementalEncoder.encode(self, input, final)
     18 def encode(self, input, final=False):
---> 19     return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 7916: character maps to <undefined>

Note that the error occurs due to CP1252 encoding which is the default. I fixed it by adding encoding='utf-8' ib line 309 of the _export.py file:

    # Write the HTML content to the temp file
    with open(f"{tmp_dir}/table.html", "w", encoding='utf-8') as temp_file:
        temp_file.write(html_content)

(Sorry I'm not comfortable with pull requests I don't want to ruin anything)

Expected result

A file test.pdf should be created

Development environment

HansMellman commented 2 weeks ago

Encountering the same issue when trying to utilize a Unicode character in the heading section (in lieu of an image). Unable to figure out a workaround yet without just removing the character.

bart-r-willems commented 2 weeks ago

I just tested it, with my fix in place that works without issues (windows 10, python 3.12.4)

On Sat, Sep 14, 2024 at 8:58 AM HansMellman @.***> wrote:

Encountering the same issue when trying to utilize a Unicode character in the heading section (in lieu of an image). Unable to figure out a workaround yet without just removing the character.

— Reply to this email directly, view it on GitHub https://github.com/posit-dev/great-tables/issues/440#issuecomment-2350982021, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEOWAQKP6H4EUPFPID7H263ZWQXHFAVCNFSM6AAAAABODZ26S6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNJQHE4DEMBSGE . You are receiving this because you authored the thread.Message ID: @.***>

HansMellman commented 2 weeks ago

You sir, are wonderful. Thank you for that - did the trick perfectly! Hopefully they'll be able to implement the fix natively.