Home > Back-end >  MYSQL output using Python within HTML file
MYSQL output using Python within HTML file

Time:01-30

I am new to Python. I want to display MYSQL output using Python in an html file. I have the below code. However, it is not working and not actually giving me an error so I am not sure where I have gone wrong. I reviewed resources online but since I am not sure what I am looking for, not sure what to search for to fix the issue.

import mysql.connector
import webbrowser

conn = mysql.connector.connect(user='root', password='pswd#',
                              host='mysql',database='crm')

if conn:
    print ("Connected Successfully")
else:
    print ("Connection Not Established")

select_users = """SELECT * FROM users"""
cursor = conn.cursor()
cursor.execute(select_users)
result = cursor.fetchall()

p = []

tbl = "<tr><td>ID</td><td>Name</td><td>Email</td></tr>"
p.append(tbl)

for row in result:
    a = "<tr><td>%s</td>"%row[0]
    p.append(a)
    b = "<td>%s</td>"%row[1]
    p.append(b)
    c = "<td>%s</td></tr>"%row[2]
    p.append(c)


contents = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Python Webbrowser</title>
</head>
<body>
<table>
%s
</table>
</body>
</html>
'''%(p)

filename = 'python.html'

def main(contents, filename):
    output = open(filename,"w")
    output.write(contents)
    output.close()

main(contents, filename)    
webbrowser.open(filename)

if(conn.is_connected()):
    cursor.close()
    conn.close()
    print("MySQL connection is closed.")

What I am getting in the browser output is this.

browser output

Outcome I am looking for: I would like to display the table using pything/mysql in an html file. I am using Yahoo web hosting, I was able to use print("Hello World!") in an html file and get the output.

I would be very appreciative if anyone could point me in the right direction. Thank you.

CodePudding user response:

Using the new style str.format notation. {} are used for placeholder than call format to fill them (in case of an iterable don't forget to unpack it!).

Trick: make your own templates and then call them from anywhere in your code.

results = [
    ("1", "Smith", "[email protected]"),
    ("2", "John", "[email protected]"),
    ("3", "Mikey", "[email protected]"),
]

html_table_template = """<table>
{}
</table>"""
row_template = "<tr><td>{}</td><td>{}</td><td>{}</td></tr>"

col_names = "ID", "Name", "Email"
html_rows = [row_template.format(*col_names)]
for record in results:
    html_rows.append(row_template.format(*record))

html_table = html_table_template.format('\n  '.join(html_rows))

print(html_table)

Output

<table>
  <tr><td>ID</td><td>Name</td><td>Email</td></tr>
  <tr><td>1</td><td>Smith</td><td>[email protected]</td></tr>
  <tr><td>2</td><td>John</td><td>[email protected]</td></tr>
  <tr><td>3</td><td>Mikey</td><td>[email protected]</td></tr>
</table>

For your question: as mentioned in my comment a str.join is required. The p is a list so, to have consistent output, you have to make it back a string using str.join method: term separator - join -> string where with term seperator is a string used to separate each term of the list and can be used for line breaks, ....

  • Related