วันอังคารที่ 12 พฤศจิกายน พ.ศ. 2556

ใช้ python เขียนไฟร์ HTMLเพื่อจัดเก็บข้อมูลส่วนตัว

ใช้ python เขียนไฟร์ HTMLเพื่อจัดเก็บข้อมูลส่วนตัว

 1.เขียนโปรแกรมโดยใช้ภาษาpython printแสดงประวัติส่วนตัว 

Source code ประกอบด้วย:
-ชื่อ
-อายุ
-วันเกิด
-E-mail
-facebook
-โรงเรียน
-มหาวิทยาลัย

ตัวอย่าง Source code ที่ได้ทำการเขียน:
name = 'KLANARONG RALUEK'
age = '21'
birth = '12 November 1992'
email = 'kla_223@hotmail.com'
facebook = 'facebook.com/kla223'
address = '4/4 praputtabat chingklang nan'
high_school = 'Changklang Pracaputtana School'
university = "King Mongkut's University of Technology North Bangkok"

print 'Name : '+name
print 'Age : '+age
print 'Birth day : '+birth
print 'E_mail address : '+email
print 'facebook : '+facebook
print 'address : '+address
print 'High School : '+high_school
print 'University : '+university


ผล output ที่ได้: 
Name : KLANARONG RALUEK
Age : 21
Birth day : 12 November 1992
E_mail address : kla_223@hotmail.com
facebook : facebook.com/kla223
address : 4/4 praputtabat chingklang nan
High School : Changklang Pracaputtana School
University : King Mongkut's University of Technology North Bangkok

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

 2.เขียนโปรแกรมโดยใช้ภาษาpython สร้างไฟร์ HTML เพื่อแสดงข้อมูลส่วนตัว

ส่วนของ code จะแสดง:
-ชื่อ
-อายุ
-วันเกิด
-E-mail
-facebook
-โรงเรียน
-มหาวิทยาลัย

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

#create html code
my_html = """
<html>
    <head>
        <title>Index My Profile </title>
    </head>
    <body>
        <font size='10' color='orange'><center>My Profile</center></font>
        <font size='5' color='black'>Name : <font size='5' color='blue'>KLANARONG RALUEK<BR></font>
        <font size='5' color='black'>Age : <font size='5' color='blue'>21<BR></font>
        <font size='5' color='black'>Birth day : <font size='5' color='blue'>12 November 1992<BR></font>
        <font size='5' color='black'>E_mail address : <font size='5' color='blue'>kla_223@hotmail.com<BR></font>
        <font size='5' color='black'>Facebook : <font size='5' color='blue'>facebook.com/kla223<BR></font>
        <font size='5' color='black'>Address : <font size='5' color='blue'>4/4 praputtabat chingklang nan<BR></font>
        <font size='5' color='black'>High school : <font size='5' color='blue'>Changklang Pracaputtana School<BR></font>
        <font size='5' color='black'>University : <font size='5' color='blue'>King Mongkut's University of Technology North Bangkok<BR></font>
    </body>
</html>
"""
htmlFile = open("profile.html", "w+")    #open file html
htmlFile.write(my_html)      #write file html
htmlFile.close()
webbrowser.open("profile.html",new=1)    #run file html

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

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


 3.เขียนโปรแกรมโดยใช้ภาษาpython สร้างไฟร์ HTML ซึ่งจัดเก็บข้อมูลส่วนตัว โดยเขียน HTML หลายๆไฟร์ให้ลิ้งหากัน

ส่วนของ code จะแสดง:
-ชื่อ
-อายุ
-วันเกิด
-E-mail
-facebook
-โรงเรียน
-มหาวิทยาลัย

คำอธิบาย code:
 -กำหนดตัวแปลเก็บข้อมูลส่วนตัวต่างๆ
-นำตัวแปลที่เก็บข้อมูลส่วนตัวไปเก็บเป็น list
-กำหนดชื่อไฟร์ต่างๆที่ต้องการที่จะลิ้งเก็บเป็นข้อมูลแบบ list
-เขียนไฟร์ HTML ทั้งหมดที่ต้องการลิ้งโดยนำตัวแปลที่เก็บข้อมูลส่วนตัว ไปเขียนไว้ในไฟร์ HTML ให้ตรงกับชื่อที่กำหนดขึ้น
-เขียนไฟร์ HTML หน้าหลักซึ่งจะเป็นส่วนที่ลิ้งไปหาไฟร์ HTML อื่นๆ
-ไฟร์ HTML ทั้งหมดที่สร้างขึ้นต้องอยู่ใน forder เดียวกัน

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

#create profile
age = '21 Year'
birth = '12 November 1992'
email = 'kla_223@hotmail.com'
facebook = 'facebook.com/kla223'
address = '4/4 praputtabat chingklang nan'
high_school = 'Changklang Pracaputtana School'
university = "King Mongkut's University of Technology North Bangkok"

list_profile = [age, birth, email, facebook, address, high_school, university]
list_file = ["age.html", "birth.html", "email.html", "facebook.html", "address.html", "high_school.html", "university.html"]
#create html file
for i in range(7):
    my_html = """
    <html>
        <head>
            <title>My Profile </title>
        </head>
        <body>
            <font size='10' color='black'>"""+list_profile[i]+"""<BR></font>
        </body>
    </html>
    """
    htmlFile = open(list_file[i], "w+")
    htmlFile.write(my_html)
    htmlFile.close()
#link html file
my_html = """
<html>
    <head>
        <title>My Profile </title>
    </head>
    <body>
        <font size='10' color='orange'><center>My Profile</center></font>
        <font size='5' color='black'><center>Name : KLANARONG RALUEK<center><BR></font>
        <a href='age.html'>Age</a>
        <font size='5' color='black'> | </font>
        <a href='birth.html'>Birth day</a>
        <font size='5' color='black'> | </font>
        <a href='email.html'>E-mail</a>
        <font size='5' color='black'> | </font>
        <a href='facebook.html'>Facebook</a>
        <font size='5' color='black'> | </font>
        <a href='address.html'>Address</a>
        <font size='5' color='black'> | </font>
        <a href='high_school.html'>High_school</a>
        <font size='5' color='black'> | </font>
        <a href='university.html'>University</a>
    </body>
</html>
"""
htmlFile = open("profile.html", "w+")    #open file html
htmlFile.write(my_html)       #write file html
htmlFile.close()
webbrowser.open("profile.html", new=2)   #run file html


ผล output ที่ได้:

หน้าหลักเมื่อรันโปรแกรมหรือรันไฟร์ my_profile.html
ลิ้งเมื่อกดคำว่า Age ซึ่งจะลิ้งไปไฟร์ age.html
ลิ้งเมื่อกดคำว่า Birth day ซึ่งจะลิ้งไปไฟร์ birth.html
ลิ้งเมื่อกดคำว่า E-mail ซึ่งจะลิ้งไปไฟร์ email.html
ลิ้งเมื่อกดคำว่า facebook ซึ่งจะลิ้งไปไฟร์ facebook.html
ลิ้งเมื่อกดคำว่า Address ซึ่งจะลิ้งไปไฟร์ address.html
ลิ้งเมื่อกดคำว่า High School ซึ่งจะลิ้งไปไฟร์ high_school.html
ลิ้งเมื่อกดคำว่า E-mail ซึ่งจะลิ้งไปไฟร์ university.html
 =======================================================================

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

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