admin管理员组

文章数量:1317565

FastHTML returns HTML code in the response. But I want to know the generated HTML without using the response.

Best with an example:

from fasthtml import common as fh

my_var=fh.P('This is a simple example!')

How do I get the HTML content of my_var ?

FastHTML returns HTML code in the response. But I want to know the generated HTML without using the response.

Best with an example:

from fasthtml import common as fh

my_var=fh.P('This is a simple example!')

How do I get the HTML content of my_var ?

Share Improve this question edited Jan 29 at 12:20 Ximo Dante asked Jan 29 at 8:56 Ximo DanteXimo Dante 12511 bronze badges 4
  • 1 Are you looking for print(to_xml(my_var))?. to_xml comes from the same import – user459872 Commented Jan 29 at 9:14
  • Thanks @user459872, that's what I needed! – Ximo Dante Commented Jan 29 at 9:27
  • Also, you should avoid using the from x import * syntax, as it populates your namespace with possibly many things, is not very efficient, and doesn't allow you to keep track what was imported from where which prevents looking efficiently for help or doc. – globglogabgalab Commented Jan 29 at 9:55
  • 1 Thanks @globglogabgalab for your suggestion. I have edited the question – Ximo Dante Commented Jan 29 at 12:21
Add a comment  | 

2 Answers 2

Reset to default 3

You can use to_xml function from the same import to convert fasthtml components into their HTML representation.

Thanks to @user459872 and @globglogabgalab, the enclosed code is less error prune and now more simple. Here is the only missing line of code to obtain the generated HTML

from fasthtml import common as fh

my_var=fh.P('This is a simple example!')
print(fh.to_xml(my_var))

本文标签: pythonHow to get the generated HTML in FastHTMLStack Overflow