pythonapachexampp

python on xampp how to serve html page


xampp running a python script that writes an html file. How do i get xampp to serve the html from python.

name = "david"
age = 44
square = age**0.5
HTML=f"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Download</title>
</head>
<body>
    {name} age is {age} but he is square so age is{square}
</body>
</html>
"""
with open("test.html","w") as new_file:
    new_file.write(HTML)

This writes test.html how do i ask xampp to display


Solution

  • After generating the document test.html (typically located at C:\xampp\htdocs\test.html or where your xampp is installed), simply try to access it using the address http://localhost/test.html

    Edit: Added following code

    Can you try this now:

    #!/usr/bin/env python3
    
    import cgi
    
    name = "david"
    age = 44
    square = age**0.5
    HTML=f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Dynamic Output</title>
    </head>
    <body>
        <p>{name} age is {age} but he is square so age is {square}</p>
    </body>
    </html>
    """
    
    # for output
    print("Content-Type: text/html\n")
    print(HTML)