来自 Python 的 HTML 片段

发布于 2024-08-02 15:46:55 字数 563 浏览 1 评论 0原文

我对 Python 和 CGI​​ 很陌生,所以这可能是微不足道的。但我希望 Python 在访问者加载页面时生成 HTML 块。

<html>
<body>
<!-- Beautiful website with great content -->

<!-- Suddenly... -->
<h1> Here's Some Python Output </h1>
<!-- RUN PYTHON SCRIPT, display output -->

</body>
</html>

使用Python脚本,为了简单起见,看起来像这样:

#!/usr/bin/python
print "Content-type: text/html"
print 
print "<p>Hello world!</p>"

我能找到的大多数资源都演示了如何使用Python脚本生成整个网页。您可以在页面中间调用 Python 脚本来生成 HTML 片段吗?

I'm new to Python and CGI, so this may be trivial. But I'd like Python to produce a block of HTML whenever a visitor loads a page.

<html>
<body>
<!-- Beautiful website with great content -->

<!-- Suddenly... -->
<h1> Here's Some Python Output </h1>
<!-- RUN PYTHON SCRIPT, display output -->

</body>
</html>

Using a Python script that, for simplicity, looks something like this:

#!/usr/bin/python
print "Content-type: text/html"
print 
print "<p>Hello world!</p>"

Most resources I was able to find demonstrate how to produce an ENTIRE webpage using a Python script. Can you invoke a Python script mid-page to produce JUST an HTML snippet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-08-09 15:46:55

使用 AJAX 客户端或模板服务器端。

模板将允许您保持页面的大部分静态,但使用服务器端脚本语言(如 Python)来填充动态位。 Stackoverflow 上有很多关于 Python 模板系统的好文章。 这是一个

AJAX将允许您更新页面之后最初加载来自服务器的结果。

Use AJAX client-side, or templates server side.

A template will allow you to keep most of your page static, but use a server-side scripting language (like Python) to fill in the dynamic bits. There are lots of good posts on Python template systems on Stackoverflow. Here's one

AJAX will allow you to update a page after it initially loads with results from a server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文