How My 10 Lines code of Python Generate HTML Page | Amazing?
You may have seen me, talking good about Python. You may ask, why am I so thankful for being Python developer?
The respect I have for Python has never downsized me.
Till now I have written many tutorials and shared Python code. I wrote the things that I have automated with Python. Got a really good response from readers.
Even for the single static page, you have to write thousand’s lines of code. And it’s not such an easy task. If you are little aware of web designing, you might be knowing this.
For the online tool which I was developing, I wanted to create the HTML table to list down all the ASCII codes corresponding to each character symbol. I was so tired to write those thousand lines of code.
And Python comes to the rescue when I don’t want to write entire HTML code by myself.
Let’s see how I have used Python to create HTML code.
This is a simple application where you can use Python for automation.
Python Generate HTML Table
Being like any other developer, I don’t have patience. So here is a code I have written to create an HTML page using Python script.
Note: Before looking at the code, you should know the basic syntax for creating a table in HTML.
#Python is amazing #Create ASCII-Char HTML Table strTable = "<html><table><tr><th>Char</th><th>ASCII</th></tr>" for num in range(33,48): symb = chr(num) strRW = "<tr><td>"+str(symb)+ "</td><td>"+str(num)+"</td></tr>" strTable = strTable+strRW strTable = strTable+"</table></html>" hs = open("asciiCharHTMLTable.html", 'w') hs.write(strTable) print strTable
Steps to follow for Python Generate HTML:
- Get data to feed in the table (Here ASCII code for each char value is calculated.)
- Keep Loops over a number of rows in the table and feed data on HTML table.
- Save the generated HTML code in .html file.
- Open a file in the browser.
Output of the Python Code:
It will save the HTML table code in asciiCharHTMLTable.html file in the same directory.
Now open that HTML code file in the browsers (or you can check the output table here).
Here is HTML output table for symbol and its associated ASCII code:
Amazing, isn’t it?
Note: I have applied CSS to this HTML table. If you run the same code, you might get a different view of the HTML table. But the content will be the same.
Why is Python best for creating HTML pages?
Now imagine hard work we do to feed data (character and ASCII symbol) to each HTML table, row and the column. If you are manually writing code for HTML table and even you do your I’s and cross your T’s; you may end up with some code glitch or a really silly mistake.
And in the end, time is precious. So get the things done by automation whether it’s small or big. And Python is best suitable for it.
This is why I love Python. It is so powerful for Automation. If you are new to the Python or want to enhance your Python skills, check it out my complete Python tutorial for you.
Here is one more Python tweak for you. Do you want to create an HTTP server on your local system? You don’t need any software or external tool. Here are 2 lines of code that create a simple HTTP server using Python.
Don’t forget to mention the things you have automated using Python by commenting below. Also, you can ask any query regarding Python generate HTML.