views:

1054

answers:

4

I think I want to use pythons built in calendar module to create an HTML calendar with data. I say I think because I'll probably think of a better way, but right now it's a little personal. I don't know if this was intended to be used this way but it seems like it is a little pointless if you can't at least making the days into a <a hrefs>.

This sets up a calendar for this month with Sunday as the first day.

import calendar
myCal = calendar.HTMLCalendar(calendar.SUNDAY)
print myCal.formatmonth(2009, 7)

it prints

<table border="0" cellpadding="0" cellspacing="0" class="month">\n<tr>
<th colspan="7" class="month">July 2009</th></tr>\n<tr><th class="sun">Sun</th>
<th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th>
<th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th></tr>\n
<tr><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td>
<td class="noday">&nbsp;</td><td class="wed">1</td><td class="thu">2</td><td class="fri">3</td>
<td class="sat">4</td></tr>\n<tr><td class="sun">5</td><td class="mon">6</td><td class="tue">7</td>
<td class="wed">8</td><td class="thu">9</td><td class="fri">10</td>
<td class="sat">11</td></tr>\n<tr><td class="sun">12</td><td class="mon">13</td>
<td class="tue">14</td><td class="wed">15</td><td class="thu">16</td><td class="fri">17</td>
<td class="sat">18</td></tr>\n<tr><td class="sun">19</td><td class="mon">20</td>
<td class="tue">21</td><td class="wed">22</td><td class="thu">23</td><td class="fri">24</td>
<td class="sat">25</td></tr>\n<tr><td class="sun">26</td><td class="mon">27</td>
<td class="tue">28</td><td class="wed">29</td><td class="thu">30</td><td class="fri">31</td>
<td class="noday">&nbsp;</td></tr>\n</table>\n

I would like to insert some data into the HTMLCalendar object before it renders the html string. I just can't figure out how.

For example

<td class="tue">28<br />[my data]</td>
A: 

It's hard to say without knowing exactly what you're trying to accomplish, but here's one idea.

Instead of printing myCal.formatmonth(2009, 7), why don't you assign it to a string. Then you could manipulate it, perhaps with a regex.

Here's a really bad example:

import calendar
import re

myCal = calendar.HTMLCalendar(calendar.SUNDAY)
myStr = myCal.formatmonth(2009, 7)

re.sub('28', '28<br/>[My Data]', myStr)

print myStr

It does what you want, but it's pretty ugly.

Anthony Lewis
ultimately the goal is to pull data from mysql and plug it in a calendar. There are dozens of ways to do this and I thought "hey, python is batteries included" so I looked for something that made HTML Calendars. I found the calendar.HTMLCalendar object. It just doesn't seem to want you to load the calender with any information. So it became a little personal.
RogueFalcon
I added an example, but this is probably the worst way to do it. Are you using a web framework for this project? Your framework might include a better way to make calendars.
Anthony Lewis
I actually wrote something like that in my frustration with the object.
RogueFalcon
+1  A: 

You may load html, which seems to be valid XMl into a XML tree, modify it and again output it. e.g. this adds <br/> cool to each Tue td node.

import calendar
import xml.etree.ElementTree as etree

myCal = calendar.HTMLCalendar(calendar.SUNDAY)
htmlStr = myCal.formatmonth(2009, 7)
htmlStr = htmlStr.replace("&nbsp;"," ")

root = etree.fromstring(htmlStr)
for elem in root.findall("*//td"):
    if elem.get("class") != "tue":
        continue
    elem.text += "!"

    br = etree.SubElement(elem, "br")
    br.tail = "cool!"

print etree.tostring(root)

I do not yet know why you need to generate a HTML calendar, but there are better ways of doing that depending on needs and framework you are using.

Anurag Uniyal
The end goal is an employee scheduling manager. I am probably not going to use the calender module. I read the python source for it and it doesn't allow any modification. It just prints an HTML table. Seems totally useless to me. It seems like your method would work.
RogueFalcon
is this a web app or desktop app? if desktop which framework? and why you need html in that case? if web there are better html calendar controls available.
Anurag Uniyal
adn yes you can use my method to modify HTML output of calendar, which was your question, so you can accept this as answer :)
Anurag Uniyal
+5  A: 

The calendar module has usually been pretty useless, but in 2.5 it introduced the Calendar object. This won't render an HTML calendar for you, but it has loads of methods that will help you render a calendar.

For example, monthdatescalendar(year, month) will give you a list of all weeks in the month given, where each week in turn is a list of the seven days. So monthdatescalendar(2009,7) will start with [[datetime.date(2009, 6, 29), datetime.date(2009, 6, 30), and end with datetime.date(2009, 8, 1), datetime.date(2009, 8, 2)]]

With this, it then becomes a trivial excersize to generate the HTML you want.

Lennart Regebro
That is a good point.
RogueFalcon
Yeah, so vote up! :-) I want 10 points! :-D
Lennart Regebro
+4  A: 

Create a new class inheriting from HTMLCalendar. Override the formatday method.

Whoever makes comments like "this library is useless" obviously doesn't understand Python.

class EmployeeScheduleCalendar(HTMLCalendar):
    def formatday(self, day, weekday):
        """
          Return a day as a table cell.
        """
        if day == 0:
            return '<td class="noday">&nbsp;</td>' # day outside month
        else:
            return '<td class="%s"><a href="%s">%d</a></td>' % (self.cssclasses[weekday], weekday, day)
Alan Hynes