-
-
Notifications
You must be signed in to change notification settings - Fork 32
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 support for styler pandas object #66
Conversation
… for wider email client support
@@ -63,6 +66,12 @@ def render_table(self, tbl, extra=None): | |||
raise ImportError("Missing package 'pandas'. Prettifying tables requires Pandas.") | |||
|
|||
extra = {} if extra is None else extra | |||
|
|||
# Allow for pandas styler object, convert to inline CSS for email client rendering |
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.
I believe this also takes care of the "TODO: Nicer tables." in the function, but I will leave that to your discretion to remove.
Check out this example of how far the stylizing of tables can go with this simple addition. Also, ignore the data in the table as it is just an ongoing project I am extensively utilizing redmail for. This is achieved with the following code:
Then you just pass it the same as a normal pandas dataframe: It's that easy and brings a whole world of customization to the tables within emails seamlessly with the preexisting redmail table handler. Let me know if you have any questions @Miksus! |
…ld in gmail and other email clients)
@Miksus bumping |
wow nice PR! |
Thanks! I am unable to request your review to have it merged. Either way, feel free to implement this another way if you see fit, takes a couple lines and makes customization really extensive. |
Sorry for taking an eternity to review this (and bunch of other issues and PRs). Have had a bit longer Christmas break from open-source just because I have been busy with hobbies and random events. I wanted to have a full free day to focus on my open-source projects to get back on track with all the amazing PRs and issues I have ignored but seems I have to work on the time I have. This indeed seems promising. The old styling was quite ugly and unreliable. The only issue I see is the Pandas dependency. Some people want Pandas to be an optional dependency (as it is quite heavy). One candidate would be similar setup as I did with Red Bird (it has this small utilility). Perhaps a similar setup would make the optional imports smooth here as well? Another option would be a simply try-except. We also need the css_inline here: https://github.com/Miksus/red-mail/blob/master/pyproject.toml#L36. I have a random event today (again...) but I'll take this as a priority now. |
Perhaps the I hope you don't mind if I tinker with your PR. |
Having the Pandas Styler as an optional dependency is a bit tricky but I'm sure we will come up with an idea. |
Oh, I'm a bit stupid. This section of the code couldn't even be executed if Pandas was missing. I think we can put the import directly to the function. It's ugly but in this sort of situation, there is no better alternative. |
Bit unfortunate that this package my have to resort to using the new css-inline package, i'm sure it will be really good once it starts getting used more widely. However, the older premailer package was battle tested because it had been around for a long time and it was a lot more feature rich and more powerful (though it was a lot slower as a result of being written in python instead of rust). Cant really use it now though when it comes to developing new packages because the latest version of python it officially supports in 3.8 and it hasn't been updated since 2021 |
Ye, it seems it's still active (the last merge was 3 weeks ago). Anyways as this is a feature that does not change any previous behaviour, I don't see much risk in terms of the dependency. However, the existing Pandas table "styling" is pretty ugly so it would be nice to rewrite that with such a library as well. I initially copied the pattern from Pandas itself which was pure dog shit, to be honest. |
Codecov ReportBase: 99.68% // Head: 99.68% // Increases project coverage by
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more Additional details and impacted files@@ Coverage Diff @@
## master #66 +/- ##
=======================================
Coverage 99.68% 99.68%
=======================================
Files 13 13
Lines 627 635 +8
=======================================
+ Hits 625 633 +8
Misses 2 2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Only reason I went with css-inline and not premailer is because the css-inline package converts nth-child elements to be renderable in email clients that do not support it, which is a majority of clients (docs). I really like the alternating gray color from the built in current redmail table and wanted that to still be an option. Obviously if it is only updated till 3.8 then I agree we should switch it to premailer.
Feel free to do whatever you want to the PR. Since it's only a couple lines it doesn't affect much and I see I missed some definitions for dependencies, sorry about that! |
Nah, no problem! I find it fun playing with this and I really appreciate you decided to make the PR in the first place! I'll tinker with this a bit more, write some documentation and fix that one test, and we are good to go. Are you working on this as well at the moment? Just wondering I don't mess with your changes. |
Nope! You are all good, I will let you work out the kinks. Glad I was able to contribute a little bit! |
Actually premailer is the one that hasn't been updated since Python 3.8. css inline seems good except for it's low test coverage statistics. |
Yep, I think this is good to go! I'll sleep the night though and double-check in the morning. The master is auto-deployed to test PyPI so if you need this quickly, it should be installable from there. I possibly will do a formal release tomorrow though. The Thanks again, @ejbills, for the effort and patience and sorry for the delay. Thank you, @Waghabond, also for being active and for helping to push this through! |
See here :Styler object docs
This PR uses css-inline as a way to convert CSS styling into inline CSS styling for wider support on mail clients (<style> tags are not supported on clients like gmail and whatnot). This allows further customization for pandas dataframe tables in converting them to HTML with stylized attributes as seen in the doc link above.
This code does not affect how dataframes are handled currently, and only takes care of adding support for the Styler object.