classification
Title: HTMLCalendar allow custom classes
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mariatta, Oz.Tiram, doerwalter, rhettinger
Priority: normal Keywords:

Created on 2017-04-18 19:37 by Oz.Tiram, last changed 2017-06-06 13:04 by doerwalter. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1439 merged Oz.Tiram, 2017-05-03 19:15
Messages (18)
msg291841 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-18 19:37
At the moment methods like HTMLCalendar.formatmonthname and HTMLCalendar.formatmonth have hard coded name 'month'.

This class is pretty helpful as a good start, but if you want to customize
the styles it's not helpful.

I think it would be helpful for others too, if this would have be customize able.

Would you accept a PR for such thing?
msg291858 - (view) Author: Mariatta Wijaya (Mariatta) * (Python committer) Date: 2017-04-19 04:45
Can't you override the `.month` css class and customize the style that way?
msg291859 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-19 04:49
Of course I can, but I can't add multiple css classes, and I forced to
have the same class for both the title of the month and the body of the month.
msg291869 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-04-19 09:49
IMHO this could all be done by overwriting the relevant methods.

But this might be overkill.

I think a solution might be to move the CSS classes into class attributes of HTMLCalendar. Customizing the CSS classes would then be done by subclassing HTMLCalendar and overwriting the appropriate class attributes.

Is this what you had in mind?
msg291872 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-19 10:47
@doerwalter, exactly. I found myself overwriting the relevant methods too many times.

I usually did something like this:

    class WorkCalendar(HTMLCalendar):

        def formatmonthname(self, theyear, themonth, withyear=True,
                        style=r'class="month-head"'):
             
            """
            Return a month name as a table row.
            """
            monthname = super().formatmonthname(theyear, themonth, withyear)
            regex = r'class\="month"'
            return re.sub(regex, style, monthname, 1)


Using class attributes would nice, also considering that the days CSS classes are defined as class attributes.

My intention was a few more class attributes (for the month header, and month) and also change the existing code such that each day can have multiple CSS classes and not just one.

I am willing to work on a PR for that if that sounds good and there is someone who would be willing to merge it.
msg291873 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-19 10:48
... Using class attributes would nice, also considering that the days CSS classes are defined as class attributes.

I meant to write :

Using class attributes would be nicer, also considering that the days CSS classes are defined as class attributes.
msg291874 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-04-19 10:50
OK, go ahead. I'm looking forward to what you come up with.
msg292155 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-23 07:02
@Walter, 

I implemented two possible solutions. Chronologically speaking V2 was implemented before V1, but it's a bit über-smart and might surprise it's
users requiring the class attributes to be some kind of iterable.

The first version is my current preferred solution.

I intentionally didn't create a PR for that yet. 

These are the branches:

https://github.com/oz123/cpython/tree/issue30095-v1
https://github.com/oz123/cpython/tree/issue30095-v2
msg292212 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-04-24 09:11
The second link is a 404.

For the v1 patch:

The variable names are a bit inconsistent: The first uses "classes" all others use "styles". This should be consistent within itself and with the existing code, i.e. "classes" should be used.

Also each class attribute should be preceded with a comment, explaining what the CSS class is used for.

As these are now made public to be overwritten by subclasses, I wonder wether it would make sense to document these class attributes in Doc/library/calendar.rst
msg292213 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-24 09:23
@Walter,

I agree with you about consistent naming. Personally, I would prefer to name all the variables "styles". But I preferred not to break past compatibility too. So I guess we are stuck for a while with "classes". I will rename the variables. As for documenting that, I would also do that.
Thanks for the feedback.
msg292236 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-04-24 19:05
Apparently, I forgot to push the second branch. It is now pushed.

I renamed the class attribute names on the v1 branch. I still need to do
the following:

 - Add docs
 - Fix existing tests and add new tests demonstrating the usage of the new attributes.
msg292934 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-05-03 19:17
@Walter,

I implemented your suggestions plus the obvious tasks of adding tests and documentation.

Would be happy to hear your feedback.
msg293081 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-05-05 08:47
See comments on Github
msg294846 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-05-31 15:55
See my comments on the pull request: https://github.com/python/cpython/pull/1439

After you address those, IMHO this is ready to be merged.
msg294952 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-06-01 15:05
See comments on the pull request. Also it seems that currently the pull request can't be merged because of merge conflicts.
msg294954 - (view) Author: Oz Tiram (Oz.Tiram) * Date: 2017-06-01 16:00
@Walter, I fixed the issues your raised, and also solved the merge conflict in What's New.
msg295245 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-06-06 09:36
New changeset 8b7a4cc40e9b2f34da94efb75b158da762624015 by Walter Dörwald (Oz N Tiram) in branch 'master':
bpo-30095: Make CSS classes used by calendar.HTMLCalendar customizable (GH-1439)
https://github.com/python/cpython/commit/8b7a4cc40e9b2f34da94efb75b158da762624015
msg295259 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2017-06-06 13:04
Closing the issue. The patch has been merged.
History
Date User Action Args
2017-06-06 13:04:03doerwaltersetstatus: open -> closed
resolution: fixed
messages: + msg295259

stage: patch review -> resolved
2017-06-06 09:36:02doerwaltersetmessages: + msg295245
2017-06-01 16:00:55Oz.Tiramsetmessages: + msg294954
2017-06-01 15:05:48doerwaltersetmessages: + msg294952
2017-05-31 15:55:32doerwaltersetmessages: + msg294846
2017-05-05 08:47:58doerwaltersetmessages: + msg293081
2017-05-03 20:57:32Mariattasetstage: patch review
versions: + Python 3.7, - Python 3.6
2017-05-03 19:17:37Oz.Tiramsetmessages: + msg292934
2017-05-03 19:15:41Oz.Tiramsetpull_requests: + pull_request1537
2017-04-24 19:05:12Oz.Tiramsetmessages: + msg292236
2017-04-24 09:23:49Oz.Tiramsetmessages: + msg292213
2017-04-24 09:11:42doerwaltersetmessages: + msg292212
2017-04-23 07:02:29Oz.Tiramsetmessages: + msg292155
2017-04-19 10:50:49doerwaltersetmessages: + msg291874
2017-04-19 10:48:11Oz.Tiramsetmessages: + msg291873
2017-04-19 10:47:13Oz.Tiramsetmessages: + msg291872
2017-04-19 09:49:20doerwaltersetnosy: + doerwalter
messages: + msg291869
2017-04-19 05:56:01serhiy.storchakasetnosy: + rhettinger
2017-04-19 04:49:32Oz.Tiramsetmessages: + msg291859
2017-04-19 04:45:11Mariattasetnosy: + Mariatta
messages: + msg291858
2017-04-18 19:37:57Oz.Tiramcreate