python mariadb & html tables
MRAB
python at mrabarnett.plus.com
Tue Sep 15 16:00:55 EDT 2020
On 2020-09-15 20:33, SS wrote:
> On Tuesday, September 15, 2020 at 2:52:35 PM UTC-4, Larry.Martell at gmail.com wrote:
>> On Tue, Sep 15, 2020 at 11:45 AM SS <sami.... at gmail.com> wrote:
>> >
>> > I'm trying to create an table in html from a Maria DB table, from a python script. I'm getting some unexpected results.
>> >
>> > The environment is Centos 7, I'm using Python3 with apache.
>> >
>> > Here is copy of the script I'm using:
>> >
>> > ******* SCRIPT START *********
>> >
>> > import mysql.connector
>> > import cgitb
>> > cgitb.enable()
>> >
>> > mydb = mysql.connector.connect(
>> > host="localhost",
>> > user="root",
>> > password="somepassword",
>> > database="somedb"
>> > )
>> >
>> > mycursor = mydb.cursor()
>> >
>> > mycursor.execute("select name, provisioned_space, used_space, memory_size, cpus, ip_address, host_cpu, host_mem from projecttable where name like '%newproject%'")
>> >
>> > myresult = mycursor.fetchall()
>> >
>> > print "Content-type: text/plain:charset=utf-8"
>> > print
>> > for x in myresult:
>> > print(x)
>> >
>> >
>> > ******* SCRIPT STOPS *********
>> >
>> > It works. But I get alot of extraneous data I don't need or want. Here is an example of the output:
>> >
>> > ********* OUTPUT STARTS ***********
>> >
>> > Content-type: text/plain:charset=utf-8
>> >
>> > (u'host1.mydomain.com', u'106.11 GB', u'32.72 GB', u'1 GB', u'1', u'172.18.33.62', u'Running', u'16 MHz')
>> > (u'hopst2.mydomain.com', u'106.08 GB', u'56.87 GB', u'1 GB', u'1', u'172.17.4.82', u'Running', u'0 Hz')
>> >
>> > ********* OUTPUT STOPS ***********
>> >
>> > Is this typical of Python? Do I need create another script to clean up the output? Or is there a better way to extract data from a MariaDB instance, and put it in an HTML table? Any advise would be appreciated.
>> What is the extraneous data?
>
> Thanks for responding. I am expecting (or would like) to get the following output:
>
> Content-type: text/plain:charset=utf-8
> <html>
> <body>
> <table>
> <tr> <td> host1.mydomain.com </td> <td> 106.11 GB </td> <td> 32.72 GB </td> <td> 1 GB </td> <td> 172.18.33.62 </td> <td> Running </td> <td> 16 MHz </td> </tr>
> <tr> <td> host2.mydomain.com </td> <td> 106.08 GB 56.87 GB </td> <td> 1 GB </td> <td> 1 GB </td> <td> 172.17.4.82 </td> <td> Running 0 Hz </td> </tr>
> </table>
> </body>
> </html>
>
> But instead I get the following:
>
> Content-type: text/plain:charset=utf-8
>
> (u'host1.mydomain.com', u'106.11 GB', u'32.72 GB', u'1 GB', u'1', u'172.18.33.62', u'Running', u'16 MHz')
> (u'hopst2.mydomain.com', u'106.08 GB', u'56.87 GB', u'1 GB', u'1', u'172.17.4.82', u'Running', u'0 Hz')
>
> So each table row is enclosed in parenthesis, and each field is enclosed in 'u ',
>
> Ultimately, my intention is to create an html table, from a Maria DB table using Python. TIA.
>
Each row (from myresult) is a tuple of Unicode strings. You're asking
Python to print them, which is what it's doing.
If you want a table in HTML, you'll need to write the code to do that.
More information about the Python-list
mailing list