classification
Title: HTMLCalendar allow custom classes
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
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-04-23 07:02 by Oz.Tiram.

Messages (8)
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
History
Date User Action Args
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