Now I have been modifying the Python program firstly to accommodate the changes in the data structure, and also to display any selected record as a HTML page. This is for two reasons:
Firstly HTML pages can display images when opened in a browser; Secondly HTML can take web links in the database and turn them into clickable hyperlinks. You can't do either of these just on the IDLE command line.(A third reason that isn't put into practice here is HTML makes tables easier, particularly automating column width - useful when you want to display data in tabular form).
The whole program is now 415 lines long, so I won't just copy and paste the whole thing. That's one of the things I've noticed since starting out learning Python. When you keep working on a program, it can grow surprisingly big. The trick is to make sure that big does not mean unreadable or unmanageable (functions really help here, as do comments in the code).
Making changes to the data structure can mean lots of changes to Python code, and I've been going through the code and testing it, seeing what the ramifications of changing the columns are for the code. This has affected editing existing records, entering new records, displaying the main table and of course displaying records.
This is the function that has changed the most, the one for displaying individual records.
def displayrecord(name):
curs.execute("SELECT * FROM historicalfigures WHERE Name = '"+ name +"';")
maindump = curs.fetchall()
curs.execute("SELECT * FROM roles WHERE Name = '"+ name +"';")
rolesdump = curs.fetchall()
curs.execute("SELECT * FROM weblinks WHERE Name = '"+ name +"';")
webdump = curs.fetchall()
mainrecord = maindump[0]
FileH = open("temp.html", 'w')
FileH.write("<HTML>")
FileH.write("<h3>Name: " + name +"</h3> \n")
FileH.write("<p>Born: " + str(mainrecord[1])+"</p> \n")
FileH.write("<p>Died: " + str(mainrecord[2])+"</p> \n")
FileH.write("<p>Nation: " + mainrecord[3]+"</p> \n")
FileH.write("<p>Image: " + str(mainrecord[4])+"</p> \n")
FileH.write("<br><img src='"+str(mainrecord[4])+"' width=300 align='right'>")
rolestring = "<p><B>Roles</B></p> <ul>\n"
for line in rolesdump:
rolestring = rolestring + "<li>"+ line[1] +"</li> \n"
rolestring += "</ul> \n"
FileH.write(rolestring)
if webdump: # does webdump contain any lines?
FileH.write("<p><b>Web addresses for more info</b></p> \n")
FileH.write("<ul>")
for line in webdump:
FileH.write("<li><a href ='"+line[1]+"'>"+line[2] +"</a></li> \n")
FileH.write("</ul>")
FileH.write("</HTML>")
FileH.close()
webbrowser.open("temp.html")
As you can see it first collects the data from SQLite (using the curs cursor object as usual), then creates a file (temp.html) and file handle (a.k.a. io stream) so Python can write to it, and then writes HTML code to the file modified by what the data in the database is. Finally it uses the webbrowser module (imported along with sqlite3 module earlier in the code) to open the newly created web page in your web browser.
Here is the HTML contents of a typical record:
<HTML><h3>Name: Cardinal Thomas Wolsey</h3>
<p>Born: 1473</p>
<p>Died: 1530</p>
<p>Nation: England</p>
<p>Image: https://collectionimages.npg.org.uk/large/mw06903/Thomas-Wolsey.jpg</p>
<br><img src='https://collectionimages.npg.org.uk/large/mw06903/Thomas-Wolsey.jpg' width=300 align='right'><p><B>Roles</B></p> <ul>
<li>Politician</li>
<li>Cleric</li>
</ul>
</HTML>
No comments:
Post a Comment