Problem with embeding python code in html?

Question:

NOT SURE ABOUT THIS:
I think we cannot embed python code in html like we do with PHP, JSP

I have a piece of code I am trying to tamper with.

PAGE = urllib.unquote_plus("%3C%21doctype+html+public+%22-%2F%2FW3C%2FDTD+HTML+4.01%2F%2FEN%22+%22http ........

When I view source in the html file, I can see it being neatly displayed. But I am having a very hard time trying to figure out the %22 3C

What is happening? How to convert a sample code written neatly that python can understand.

Asked By: user244333

||

Answers:

You can use a template engine like Jinja2, Django’s Template Engine or Mako Templates.

Answered By: jbochi

One way would be using <py-script> tag:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Hello World!</title>
    <!-- linking to PyScript assets -->
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <body>
  <!-- Put Python code inside the the <py-script> tag -->
    <py-script>
        print("Hello World!")
        input("Enter your name:")
    </py-script>
  </body>
</html>

Learn more about this at the PyScript site.

Answered By: Sreedhar_K
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.