NCS
NCS

Reputation: 11

Generate calendar Python webpage

Re-phrasing my Question, as suggested by moderator.

I need to create a calendar with Python & CSS for a web page, I tried the following in Python:

#!/usr/bin/env python

import os, re, sys, calendar  
from datetime import datetime 

myCal = calendar.monthcalendar(2011,9)
html += str(myCal)
mycal = myCal[:1]
cal1 = myCal[1:2]
cal2 = myCal[2:3]
cal3 = myCal[3:4]
cal4 = myCal[4:5]

html += str(mycal)+'<br>'
html += str(cal1)+'<br>'
html += str(cal2)+'<br>'
html += str(cal3)+'<br>'
html += str(cal4)+'<br>'
html += "<br>"

This is the following output on the web page:

[[0, 0, 0, 1, 2, 3, 4]]<br>
[[5, 6, 7, 8, 9, 10, 11]]<br>
[[12, 13, 14, 15, 16, 17, 18]]<br>
[[19, 20, 21, 22, 23, 24, 25]]<br>
[[26, 27, 28, 29, 30, 0, 0]]<br>

How can I arrange the above in the following format below? ( This is a SAMPLE format, I have not done the actual Day / Date match. The format needs to be in TWO rows.
eg.
Day
Date
imgN
Day
Date
imgN
next month )

                      Dec , 2011
----------------------------------------------------------------------------
sun    | mon   | tue   | wed thu fri sat sun mon tue wed thu fri sat sun
-------|-------|-------|----------------------------------------------------
.... 1 |.. 2 ..|.. 3 ..| 4 5 6 7 8 9 10 11 12 13 14 15<br>
------ |-------|-------|----------------------------------------------------
img1   | img2  | img3  | ....

                      Jan , 2012
----------------------------------------------------------------------------
sun    | mon   | tue   | wed thu fri sat sun mon tue wed thu fri sat sun
-------|-------|-------|----------------------------------------------------
.... 1 |.. 2 ..|.. 3 ..| 4 5 6 7 8 9 10 11 12 13 14 15<br>
------ |-------|-------|----------------------------------------------------
img1   | img2  | img3  | ....

                      Feb , 2012
----------------------------------------------------------------------------
sun    | mon   | tue   | wed thu fri sat sun mon tue wed thu fri sat sun
-------|-------|-------|----------------------------------------------------
.... 1 |.. 2 ..|.. 3 ..| 4 5 6 7 8 9 10 11 12 13 14 15<br>
------ |-------|-------|----------------------------------------------------
img1   | img2  | img3  | ....

Upvotes: 1

Views: 2190

Answers (2)

James Hurford
James Hurford

Reputation: 2058

I'm not quiet sure I understand the exact format your wanting but you can put the calender into a table of 3 rows.

Try this for each of your months

import calendar

myCal = calendar.monthcalendar(2011,9)


#make multi rowed month into a single row list
days = list()
for x in myCal:
    days.extend(x)


#match this up with the week names with enough weeks to cover the month
weeks = len(myCal) * ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']

#some images
images = ['image1', 'image2', '...']

#make sure there is at least a zero at the end of the list
days.append(0)
#find start and end indexes where the actual day numbers lie
start_index = days.index(1)
#if there are no zeros at the end of the list this will fail
end_index = days[start_index:].index(0) + len(days) - len(days[start_index:])

header = 'Dec, 2011'
#Create the table rows of just the items between start_index and end_index.
weekday_row =  '<tr>%s</tr>'%(''.join(['<td>%s</td>'%(x) 
                                       for x in weeks[start_index:end_index]]))
day_row =  '<tr>%s</tr>'%(''.join(['<td>%d</td>'%(x) 
                                   for x in days[start_index:end_index]]))
image_row = '<tr>%s</tr>'%(''.join(['<td>%s</td>' % (x) for x in images]))


#finally put them all together to form your month block
html =  '<div>%s</div><table>\n\n%s\n\n%s\n\n%s</table>' % (header, 
                                                            weekday_row, 
                                                            day_row, 
                                                            image_row)

You can repeat that as many times as you need

Upvotes: 1

Pengman
Pengman

Reputation: 708

I'm not sure exactly what you are looking for, this produces a table with the 3 rows you describe, but for the entire month (not just the first 15 days). I may be starting point

import calendar
import itertools

blank = "&nbsp;"

myCal = calendar.monthcalendar(2011,9)
day_names = itertools.cycle(['mon','tue','wed','thu','fri','sat','sun']) # endless list
cal = [day for week in myCal for day in week] # flatten list of lists

# empty lists to hold the data
headers = []
numbers = []
imgs = []

# fill lists
for d in cal:
    if d != 0:
        headers.append(day_names.next())
        numbers.append(d)
        imgs.append("image"+str(d))
    else:
        headers.append(day_names.next())
        numbers.append(blank)
        imgs.append(blank)

# format data
html = "<table><tr></tr>{0}<tr>{1}</tr><tr>{2}</tr></table>".format(
    "".join(["<td>%s</td>"% h for h in headers]),
    "".join(["<td>%s</td>"% n for n in numbers]),
    "".join(["<td>%s</td>"% i for i in imgs]))

Upvotes: 3

Related Questions