วันพุธที่ 13 พฤศจิกายน พ.ศ. 2556

week 2

week 2

 1.เขียนโปรแกรมโดยใช้ภาษาpython สร้างไฟร์ HTML แสดงเวลา วันที่ และปฏิทิน

ส่วนของ code จะแสดง:
 -วัน เวลา ปัจจุบัน
-ปฏิทินเฉพาะเดือนปัจจุบัน
-ปฎิทินแสดงเดือนทั้งหมดใน 1 ปี


คำอธิบาย code:
-โค้ดจะเป็นการเรียกใช้ library ชื่อ calendar มาช่วยในการสร้างปฏิทินสามารถเลือกได้ว่าจะสร้าง ปฏิทินเฉพาะเดือน หรือจะแสดงเป็นปี
-library ชื่อ time ใช้แสดงวัน เวลาปัจจุบัน

 ตัวอย่าง Source code ที่ได้ทำการเขียน:
import calendar, webbrowser, time

html = ''
localtime = time.asctime( time.localtime(time.time()) )
year = time.localtime(time.time())[0]
month = time.localtime(time.time())[1]

n = 3 
cal = calendar.HTMLCalendar(calendar.SUNDAY)
cal_yea = cal.formatyear(year, n)
cal_mon = cal.formatmonth(year, month)
#create html file
html += "<body BGCOLOR='lightblue'>"
html += "<center>"
html += "<font size = 5 color=red><B>"+localtime+"</B></font><BR>"
html += cal_mon
html += "<hr noshade size=2 color='red'><hr noshade size=2 color='red'>"
html += cal_yea
html += "</center></body>"

html_cal = open("calendar.html", "w+")   #open file html
html_cal.write(html)      #write file html
html_cal.close()
webbrowser.open("calendar.html", new=1)  #run file html
 


 ผล output ที่ได้:
ภาพแสดงหน้า Browser หลังจากรันโปรแกรม

 =======================================================================

 

 2.เขียนโปรแกรมโดยใช้ภาษาpython ดึงข้อมูลจากไฟร์ .txt ซึ่งประกอบด้วยข้อมูลส่วนตัว และข้อมูลของเพื่อนๆในชั้นเรียน มาเขียนแสดงบนไฟร์ html

 ส่วนของ code จะแสดง:

-ส่วนของหน้าแรก home จะแสดงระหัสนักศึกษาแต่ละคน

-เมื่อทำการคลิกที่ระหัสนักศึกษา ก็จะทำการลิ้งเข้าไปแสดงข้อมูลของคนนั้นๆ

-ในส่วนของข้อมูลจะแสดงชื่อ นามสกุล ชื่อเล่น วันเกิด อายุ เพศ ศาสนา น้ำหนัก ส่วนสูง E-mail ที่อยู่ facebook เบอร์โทรศัพท์



คำอธิบาย code:

 -โค้ดจะแยกการเขียนไฟร์ html ไฟร์แรกจะเป็น html ในส่วนของ home และไฟร์ html ย่อยจะเป็นไฟร์ html ของแต่ละคนแยกตามบุคคล 1 คนต่อ 1 ไฟร์

-ไฟร์ส่วนของ home จะทำหน้าที่ลิ้งเข้าไปหาไฟร์ html ที่เก็บข้อมูลของแต่ละคน

-ไฟร์ .txt ที่จะอ่านค่าจะเก็บข้อมูลของแต่ละคน 1 คนต่อ 1 บรรทัด โดยข้อมูลแต่ละชนิดจะถูกขั้นด้วนเครื่องหมาย /

-ส่วนแรกของโค้ดก็จะทำการอ่านข้อมูลจากไฟร์ .txt มาทัละบรรทัดแล้วทำการ split / ออกจากข้อมูลซึ่งจะได้ข้อมูลออกมาเป็น list

-เมื่อได้ข้อมูลที่ split ออกมาเป็น list แล้วก็จะทำการเขียนไฟร์ html ส่วนของ home โดยเขียนข้อมูลระหัสนักศึกษา และเขียนลิ้งไปที่ชื่อไฟร์ที่เก็บข้อมูลของบุคคลนั้น

-เขียนไฟร์ html ส่วนของการเก็บข้อมูล ชื่อไฟร์จะใช้ชื่อเดียวกับที่ได้เขียนลิ้งกับไฟร์ home ไว้

 

ตัวอย่าง Source code ที่ได้ทำการเขียน:

#24-11-2013
#klanarong raluek
#----------------------------


import webbrowser, os

list_profile = []
num_file = 1
home = """
        <html>
        <head>
            <title>Index </title>
        </head>
        <body background = "002.jpg">
        &nbsp;<font size='7' color = 'orange'><B> Student ID </B></font>
        <hr size=4>
        <UL Type='Disc'>
        """
my_html = ""
file_txt = open('list_profile.txt', 'r')

while True:
    #read .txt file
    data = file_txt.readline()
    list_data = data[:-1].split("\\")
    if data == '':
        break
    elif list_data[0] == 'StudentID ':
        list_profile = list_data
        continue
    #create home html file
    home += "<Li><font size='4'><A href='"+ 'profile'+str(num_file) +"'><I><B><font color='red'>"+ list_data[0] +"</A></font></I></B><BR>"
    #create profile html file
    my_html = """
        <html>
        <head>
            <title>Index </title>
        </head>
        <body background = "002.jpg">
            &nbsp;<font size='7' color = 'orange'><B>"""+ list_data[0] +"""</B></font>
            <hr size=4><center>
            """           
    for i in range(len(list_profile)):
        if i == 0:
            continue
        my_html += "<table BORDER = 4 ><tr><td width = '100'><B><I><font size='4' color = blue>"+ list_profile[i] +"</font></I></B></td>"
        my_html += "<td width = '300'><B><I><font size='4' color = blue>"+ list_data[i] +"</font></I></B></td></tr>"
    my_html += """
            </table>
        </center></body>
        </html>
        """
    htmlFile = open('profile'+str(num_file), "w+")   #open file html
    htmlFile.write(my_html)   #write file html
    htmlFile.close()
    num_file += 1
   
home += """</UL> </body>
        </html>
        """
htmlFile = open('Home.html', "w+")   #open file html
htmlFile.write(home)   #write file html
htmlFile.close()
webbrowser.open('Home.html', new=1)   #run file html 


ผล output ที่ได้:

ภาพแสดงหน้า home

ภาพแสดงข้อมูลส่วนตัว เมื่อมีการคลิกลิ้งของแต่ละคน

ภาพแสดงข้อมูลส่วนตัว เมื่อมีการคลิกลิ้งของแต่ละคน

ภาพแสดงข้อมูลส่วนตัว เมื่อมีการคลิกลิ้งของแต่ละคน

 =======================================================================

ไม่มีความคิดเห็น:

แสดงความคิดเห็น