How do i display python output in html?

Helo, I am a newbie ; I know I can use

This is for showing a text on the webpage using HTML but I have a file name my.py And it have a code

print("Helo");

So now I have to show this helo on the screen so how can I do that 🙂

How do i display python output in html?

asked Oct 19, 2021 at 4:04

How do i display python output in html?

1

1 Answer

You can follow this Tutorial to create a basic web server that will display any HTML you want if that's what you're looking for.

answered Oct 19, 2021 at 4:17

Admin_WhoAdmin_Who

2953 silver badges12 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python is one of the most versatile programming languages. It emphasizes code readability with extensive use of white space. It comes with the support of a vast collection of libraries which serve for various purposes, making our programming experience smoother and enjoyable. 

    Python programs are used for:

    • Connecting with databases and performing backend development.
    • Making web applications.
    • Writing effective system scripts.
    • And especially in data science and artificial intelligence.

    With this said, let us see how we can use python programs to generate HTML files as output. This is very effective for those programs which are automatically creating hyperlinks and graphic entities.

    Creating an HTML file in python

    We will be storing HTML tags in a multi-line Python string and saving the contents to a new file. This file will be saved with a .html extension rather than a .txt extension.

    Note: We would be omitting the standard declaration!

    Python3

    f = open('GFG.html', 'w')

    html_template =

    f.write(html_template)

    f.close()

    The above program will create an HTML file:

    How do i display python output in html?

    Viewing the HTML source file

    In order to display the HTML file as a python output, we will be using the codecs library. This library is used to open files which have a certain encoding. It takes a parameter encoding which makes it different from the built-in open() function.  The open() function does not contain any parameter to specify the file encoding, which most of the time makes it difficult for viewing files which are not ASCII but UTF-8.

    Python3

    import codecs

    f = open('GFG.html', 'w')

    html_template =

    f.write(html_template)

    f.close()

    file = codecs.open("GFG.html", 'r', "utf-8")

    print(file.read())

    Output:

    How do i display python output in html?

    Viewing the HTML web file

    In Python, webbrowser module provides a high-level interface which allows displaying Web-based documents to users. The webbrowser module can be used to launch a browser in a platform-independent manner as shown below:

    Python3

    import webbrowser

    webbrowser.open('GFG.html'

    Output:

    True

    How do i display python output in html?


    How do I display output from Python to HTML?

    In order to display the HTML file as a python output, we will be using the codecs library. This library is used to open files which have a certain encoding. It takes a parameter encoding which makes it different from the built-in open() function.

    Can Python output HTML?

    Python language has great uses today in almost every field, it can be used along with other technologies to make our lives easier. One such use of python is getting the data output in an HTML file. We can save any amount of our input data into an HTML file in python using the following examples in two ways.

    How can I see my output in HTML?

    For your requirement, please try the following steps:.
    Right-click on your HTML file..
    Navigate to “Browse With…”.
    Set “Internal Web Browser” as default browser..
    Click on “Browse” or “Ctrl + Shift + W”.

    How do I get Python output on my browser?

    You can display any content and work with dynamic or static pages using Python..
    Launch your Python editor and open the source code file you want to use to print information to a Web page..
    Add the "cgitb" library to the top of the file. ... .
    Set the "Content Type" headers. ... .
    Display a piece of HTML code..