简体   繁体   English

如何在 HTML 中嵌入 python 文件或代码?

[英]How can I embed a python file or code in HTML?

I am working on an assignment and am stuck with the following problem:我正在处理一项任务,但遇到以下问题:

I have to connect to an oracle database in Python to get information about a table, and display this information for each row in an .html-file.我必须连接到 Python 中的 oracle 数据库以获取有关表的信息,并为 .html 文件中的每一行显示此信息。 Hence, I have created a python file with doctype HTML and many many "print" statements, but am unable to embed this to my main html file.因此,我创建了一个带有 doctype HTML 和许多“打印”语句的 python 文件,但无法将其嵌入到我的主 html 文件中。 In the next step, I have created a jinja2 template, however this passes the html template data (incl. "{{ to be printed }}") to python and not the other way round.在下一步中,我创建了一个 jinja2 模板,但是这会将 html 模板数据(包括“{{ 要打印的}}”)传递给 python 而不是相反。 I want to have the code, which is executed in python, to be implemented on my main .html file.我想让在 python 中执行的代码在我的主 .html 文件中实现。

I can't display my code here since it is an active assignment.我无法在此处显示我的代码,因为它是一项活动分配。 I am just interested in general opinions on how to pass my statements from python (or the python file) into an html file.我只是对如何将我的语句从 python(或 python 文件)传递到 html 文件的一般意见感兴趣。 I can't find any information about this, only how to escape html with jinja.我找不到有关此的任何信息,只能找到如何使用 jinja 转义 html。

Any ideas how to achieve this?任何想法如何实现这一目标?

Many thanks.非常感谢。

You can't find information because that won't work.你无法找到信息,因为那是行不通的。 Browser cannot run python, meaning that they won't be able to run your code if you embed it into an html file.浏览器无法运行 python,这意味着如果您将其嵌入到 html 文件中,它们将无法运行您的代码。 The setup that you need is a backend server that is running python (flask is a good framework for that) that will do some processing depending on the request that is being sent to it.您需要的设置是一个运行 python(flask 是一个很好的框架)的后端服务器,它将根据发送给它的请求进行一些处理。 It will then send some data to a template processor (jinja in this case work well with flask).然后它将一些数据发送到模板处理器(在这种情况下,jinja 与flask 配合良好)。 This will in turn put the data right into the html page you want to generate.这将依次将数据放入您要生成的 html 页面中。 Then this html page will be returned to the client making the request, which is something the browser will understand and will show to the user.然后这个 html 页面将返回给发出请求的客户端,浏览器会理解并显示给用户。 If you want to do some computation dynamically on the browser you will need to use javascript instead which is something a browser can run (since its in a sandbox mode).如果你想在浏览器上动态地做一些计算,你需要使用javascript而不是浏览器可以运行的东西(因为它处于沙箱模式)。

Hope it helps!希望能帮助到你!

Given name of calling program, a url and a string to wrap, output string in html body with basic metadata.给定调用程序的名称,一个 url 和一个要包装的字符串,在带有基本元数据的 html 正文中输出字符串。

 def wrapStringInHTML(program, url, body):
    import datetime
    from webbrowser import open_new_tab

    now = datetime.datetime.today().strftime("%Y%m%d-%H%M%S")

    filename = program + '.html'
    f = open(filename,'w')

    wrapper = """<html>
    <head>
    <title>%s output - %s</title>
    </head>
    <body><p>URL: <a href=\"%s\">%s</a></p><p>%s</p></body>
    </html>"""

    whole = wrapper % (program, now, url, url, body)
    f.write(whole)
    f.close()

    open_new_tab(filename)

Thanks for the suggestions.感谢您的建议。 What I have right now is a perfectly working python file containing jinja2 and the html output I want, but as a python file.我现在拥有的是一个完美运行的 python 文件,其中包含 jinja2 和我想要的 html 输出,但作为一个 python 文件。 When executing the corresponding html template, the curly expressions {{name}} are displayed like this, and not as the functions executed within the python file.执行相应的html模板时,卷曲表达式{{name}}是这样显示的,而不是python文件中执行的函数。 Hence, I still have to somehow tell my main html file to execute this python script on my webpage, which I cannot manage so far.因此,我仍然必须以某种方式告诉我的主 html 文件在我的网页上执行这个 python 脚本,到目前为止我无法管理。

Unfortunately, it seems that we are not allowed to use flask, only jinja and django.不幸的是,我们似乎不允许使用flask,只允许使用jinja 和django。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM