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 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 ASCII code 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.
Python Generate HTML Table:
Being like any other developer, I don’t have patience. So here is a code I have written to create HTML page using Python script.
#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 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 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 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 then column. 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 at 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 and its power for doing Automation. If you are Pythonist, you can get all my Python codes on this blog.
Here is one more Python tweak for you. Do you want to create HTTP server on your local system? You don’t need any software or external tool. Here is 2 lines of code that create Simple HTTP Server using Python.
Wrapping up! 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.