• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How My 10 Lines code of Python Generate HTML Page | Amazing?

Aniruddha Chaudhari/179661/17
CodeHTML & CSSPython

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.

Coming to this post of Python Generate HTML, I was developing an online tool that converts character symbol into ASCII code. It is purely HTML and Javascript code.

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 Generate HTML Page

 

#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).

Hola!

Here is HTML output table for symbol and its associated ASCII code:

ASCII Char HTML table using PythonAmazing, 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.

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.

Python Interview Questions eBook

CodeHTMLPython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Alexey Sychov
    June 15, 2017 at 1:20 pm
    TABLE = '''
    <!DOCTYPE html>
    <html>
        <head>
        </head>
        <body>
            <table>
                <tr>
                    <th>Char</th>
                    <th>ASCII</th>
                </tr>
    %s
            </table>
        </body>
    </html>
    '''
    ROW = ' ' * 16 + "<tr><td>%s</td><td>%d</td></tr>"
    
    rows_list = [ROW % (chr(q), q) for q in xrange(33, 48)]
    strTable = TABLE % '\n'.join(rows_list)
    
    with open(r"d:\asciiCharHTMLTable.html", 'w') as f:
        f.write(strTable)
    print strTable
    
    • Reply
      Aniruddha Chaudhari
      June 15, 2017 at 1:33 pm

      Hey Alexey, this is really amazing.

      You have really make it easy with the List Comprehensions and skipping loop.

      Thanks!

  • Reply
    Amey Kamat
    February 2, 2018 at 5:52 am

    why not use templating engines? They allow you to keep two languages in different files rather than mixing them in same code.

    Python has jinja2, JavaScript has handlebars and java has thymeleaf for that purpose.

    • Reply
      Aniruddha Chaudhari
      February 3, 2018 at 6:55 pm

      We can use templating as well to generate HTML code. But, here I just wanted to have single static HTML code which I can copy and paste this code snippet in other static web pages without any dependency.

      If we want to generate dynamic content inside web pages by embedding processing code in HTML template, I would have preferred templating. But yes, we can use templating as well here.

  • Reply
    Mandar Negi
    April 23, 2019 at 3:42 pm

    Thanks for your help , it suits my exact requirement

    • Reply
      Aniruddha Chaudhari
      April 23, 2019 at 6:43 pm

      I’m glad you find it useful to get your requirement done.

  • Reply
    Mokshi Jain
    April 23, 2019 at 3:57 pm

    thank you for your sample code

    • Reply
      Aniruddha Chaudhari
      April 23, 2019 at 6:43 pm

      You’re welcome!

  • Reply
    Jinesh Bagrecha
    July 3, 2019 at 3:13 pm

    and how to send output of that html file as mail using python?

    • Reply
      Aniruddha Chaudhari
      July 3, 2019 at 5:28 pm

      You can check this code to send email using Python.

  • Reply
    Harsh Mehta
    July 12, 2019 at 10:31 pm

    This is a great example as it does not have any dependency and anybody can directly copy paste it. How can we create an HTML table? This just gives us to columns and values. If we want to have a specific box around the column and values, how do we do that? Will you please guide me?

    • Reply
      Aniruddha Chaudhari
      July 22, 2019 at 9:51 am

      Hi Harsh, you can use a CSS style sheet for the fromating table, columns or any values inside the table row.

  • Reply
    Ben
    September 25, 2019 at 4:15 am

    Hi Aniruddha Chaudhari,

    I’ve been working on a project and I’m stuck. Would you know how to create a table in html using data from an SQL database? mssql. I’ve tried and had no luck.

    • Reply
      Aniruddha Chaudhari
      September 25, 2019 at 8:18 am

      Hi Ben,

      You can write an SQL query to read the data from the SQL database and then write HTML code to display this data inside the HTML.

      To run SQL command and to put the data dynamically inside the HTML table, you have to learn any of the server-side programmings like Python, Advance Java, PHP…

      References:
      basic HTML tags to create table
      SQL commands

      If you are new to the server-side program, you can follow this simple Python bottle framework for website development.

  • Reply
    rvn
    July 18, 2022 at 9:40 pm

    which one is easy to begin for web development Python or HTML -CSS
    which language should beginners choose to bright on ????

    • Reply
      Aniruddha Chaudhari
      July 30, 2022 at 12:18 pm

      HTML-CSS is very easy. You can go with it first.

    • Reply
      Malinda
      March 7, 2023 at 12:48 pm

      HTML is the markup language for the Web. You would still need to understand it to create a dynamic table or anything that needs to be viewed on the Web, just the same with CSS. CSS for designing purposes. At least learn the basics first.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard