如何在 HTML 文件中包含 python 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4161332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:32:52  来源:igfitidea点击:

How can I include python script in a HTML file?

pythonhtml

提问by newbie pisan

How do I put this python script:

我如何放置这个python脚本:

a = ['f','d','s','a']
x = -1
scope = vars()
for i in a:
    scope['x']+=1
    print a[x]

inside of a html file?

在 html 文件中?

采纳答案by Jorge Guberte

Something like this, if you want to create an html, not necessarily display it:

像这样,如果你想创建一个html,不一定要显示它:

 html_file = open('namehere.html','w')
 a = ['f','d','s','a']
 x = -1
 scope = vars()
 data = ''
 for i in a: #TIP: use a generator
     scope['x']+=1
     data += a[x]
     data += '\n'
 html_file.write(data)
 html_file.close()

回答by jgritty

Surround it with a <body>and <head>tag and you're golden.

<body>and<head>标签包围它,你就是金色的。

But seriously, I think what you are trying to do is print fdsa, which would would look like this:

但说真的,我认为你想要做的是打印 fdsa,它看起来像这样:

<head>
<body>
fdsa
</body>
</head>

What you have there is not really a python script. You might need to correct that first, and then give a little more explanation what you are trying to do.

你所拥有的并不是真正的python脚本。您可能需要先更正它,然后再解释一下您要尝试做什么。

回答by user505255

If your web server supports it, you could run it as a CGI script to output an HTML file - more information here: http://www.penzilla.net/tutorials/python/cgi/

如果您的 Web 服务器支持它,您可以将它作为 CGI 脚本运行以输出 HTML 文件 - 更多信息请访问:http: //www.penzilla.net/tutorials/python/cgi/

You would need to modify your script to ouput valid HTML, but that tutorial should get you started.

您需要修改脚本以输出有效的 HTML,但该教程应该可以帮助您入门。

Good luck!

祝你好运!

回答by nonot1

Perhaps CGI is what you are looking for:

也许 CGI 就是你要找的:

http://docs.python.org/library/cgi.html

http://docs.python.org/library/cgi.html

http://www.penzilla.net/tutorials/python/cgi/

http://www.penzilla.net/tutorials/python/cgi/

For example:

例如:

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers

print "<html><head></head><body><pre>"
a = ['f','d','s','a']
x = -1
scope = vars()
for i in a:
    scope['x']+=1
    print a[x]
print "</pre></body></html>"

Hope this helps. Good luck!

希望这可以帮助。祝你好运!

回答by Rafe Kettler

Not possible. Python isn't like PHP; I can't just do this

不可能。Python 不像 PHP;我不能只是这样做

<?php

And be good to go.

祝你好运。

However, if your web server has a Python interpreter (most all do, these days), you can write CGI (common gateway interface) scripts to make Python code run on your webpage.

但是,如果您的 Web 服务器有 Python 解释器(现在大多数都这样),您可以编写 CGI(通用网关接口)脚本来让 Python 代码在您的网页上运行。

If you're trying to generate dynamic content (like change words in HTML), Javascript or PHP is better. Python is more suited to web applications.

如果您尝试生成动态内容(如 HTML 中的更改词),Javascript 或 PHP 会更好。Python 更适合 Web 应用程序。

回答by Arackna

if the script is in a server , you can run it using remote funcion call through JSON-RPC
you may refer the JSON-RPC documentation here

如果脚本在服务器中,您可以通过 JSON-RPC 使用远程函数调用运行它,您可以在此处
参考JSON-RPC 文档

回答by Oliver Ni

You can't.If you want to run script in an HTML File, try another language like JavaScript or PHP. To include javascript, type this:

你不能。如果您想在 HTML 文件中运行脚本,请尝试另一种语言,如 JavaScript 或 PHP。要包含 javascript,请键入:

<script type="text/javascript">
    // ...
</script>

Or in HTML5, you don't even have to type the typeattribute:

或者在 HTML5 中,您甚至不必键入type属性:

<script>
    // ...
</script>

To include PHP, type

要包含 PHP,请键入

<?php
    // ...
?>