最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to get the generated HTML in FastHTML? - Stack Overflow

programmeradmin2浏览0评论

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))
发布评论

评论列表(0)

  1. 暂无评论