-
Notifications
You must be signed in to change notification settings - Fork 44
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
Conversation
There was a problem hiding this 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!)
There was a problem hiding this 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
def save( | ||
self: GTData, | ||
filename: str, | ||
path: Optional[str] = None, |
There was a problem hiding this comment.
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
great_tables/_export.py
Outdated
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") |
There was a problem hiding this comment.
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.
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>
There was a problem hiding this 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)
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 thezoom=
argument; theexpand=
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