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 the .save() and .as_raw_html() methods #208

Merged
merged 73 commits into from
Feb 26, 2024
Merged

Add the .save() and .as_raw_html() methods #208

merged 73 commits into from
Feb 26, 2024

Conversation

rich-iannone
Copy link
Member

@rich-iannone rich-iannone commented Feb 22, 2024

This adds the .save() and .as_raw_html() methods. The first allows for image export of a GT table. It uses Selenium and the Chrome webdriver to take the screenshot, leaving cropping of the table to the PIL (via pillow) library. The user is able to control the detail of the resulting image via the zoom= argument; the expand= argument allows for a looser cropping of the table image for a better presentation. It's possible to save to a PNG, PDF, TIFF, BMP, or GIF file; this is detected from the file extension of the provided output filename.

The .as_raw_html() method facilitates the export of an HTML fragment for the table. This is useful for inspecting the table HTML and also for embedding the self-contained table into other environments.

Fixes: #177
Fixes: #203

@github-actions github-actions bot temporarily deployed to pr-208 February 22, 2024 05:42 Destroyed
Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I left some questions about whether as_raw_html might be duplicating render, and whether we might make gtsave() a method.

(I did try running the code, and wanted to check on these things first!)

docs/_quarto.yml Outdated Show resolved Hide resolved
great_tables/gt.py Show resolved Hide resolved
great_tables/gt.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@machow machow mentioned this pull request Feb 22, 2024
2 tasks
@github-actions github-actions bot temporarily deployed to pr-208 February 22, 2024 19:48 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 22, 2024 20:33 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 16:37 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 16:46 Destroyed
Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I pulled the PR locally and tested out saving images. I've left some comments based on:

  • small docstring improvements based on where I looked for info (e.g. it took me a sec to find what I should write for the extension, so I left a suggestion to move it up into parameters)
  • technical pieces (tempfile needs to be deleted after use; shortening try_import stack trace)
  • parameters: a suggestion to remove the path argument, using pandas, polars, and PIL for inspiration (they just use a single name, which can include folders; no need for extra handling internally.)

great_tables/_export.py Outdated Show resolved Hide resolved
great_tables/_export.py Outdated Show resolved Hide resolved
great_tables/_export.py Outdated Show resolved Hide resolved
great_tables/_export.py Outdated Show resolved Hide resolved
great_tables/_utils.py Outdated Show resolved Hide resolved
def save(
self: GTData,
filename: str,
path: Optional[str] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we match tools like polars and pandas, and...

  • remove the path paramter
  • (polars uses the name "file" for filename, and slashes are interpreted as directories; pandas uses the name path_or_buf, which seems a bit gratuitous!)

It seems like the full file path can just be passed straight into PIL. See PIL's .save() method docs

temp_dir = tempfile.mkdtemp()

# Create a temp file to store the HTML file; use the .html file extension
temp_file = tempfile.mkstemp(dir=temp_dir, suffix=".html")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to manually delete this file when we're done with it (unfortunately this detail is buried at the bottom of tempfile.mkstemp() 😭 )

You can use a context handler to do it automatically. This is important, because if the code errors, the context handler will still make sure to delete the file...

with tempfile.TemporaryFile() as fp:
    fp.write(b'Hello world!')
    fp.seek(0)
    fp.read()

Note that the file gets deleted after the with block, so everything using the file needs to go in there.

rich-iannone and others added 5 commits February 23, 2024 14:50
Co-authored-by: Michael Chow <mc_al_github@fastmail.com>
Co-authored-by: Michael Chow <mc_al_github@fastmail.com>
Co-authored-by: Michael Chow <mc_al_github@fastmail.com>
Co-authored-by: Michael Chow <mc_al_github@fastmail.com>
Co-authored-by: Michael Chow <mc_al_github@fastmail.com>
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 19:54 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 19:55 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 19:55 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 19:56 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 19:57 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 20:06 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 20:08 Destroyed
@github-actions github-actions bot temporarily deployed to pr-208 February 23, 2024 20:14 Destroyed
Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great--thanks for knocking out all the changes!

Since many of the commits are simply "Update file.py", WDYT of use doing a squash merge? (It makes sense you might do a lot of commits to trigger CI)

@rich-iannone rich-iannone merged commit 626f725 into main Feb 26, 2024
7 checks passed
@rich-iannone rich-iannone deleted the gt-save-image branch February 26, 2024 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ability to save table to a png epic: Implement gtsave()
2 participants