简体   繁体   English

从python打开html文件

[英]Opening a html file from python

In python 3 code example 在python 3代码示例中

while cond:      
   .... some code...     
   open html page

I want to open local html file(pass parameters to the file) within while condition. 我想在while条件内打开本地html文件(将参数传递给文件)。 So as long as the condition is true the file should keep opening. 因此,只要条件为真,文件就应继续打开。

These are the following things that I tried: 这些是我尝试过的以下事情:

webbrowser.open('path of file')

=> with this am not sure of how to pass parameters to the html page =>与此并不确定如何将参数传递到html页面

return render_template('file.html',params=params)

=> the file opens the first time the condition is true but later the loop is not repeated(well I think that the return statement terminates the loop) =>该文件在条件为真时第一次打开,但以后不再重复循环(嗯,我认为return语句终止循环)

Please let me know if I have not posted my question clearly. 如果我没有清楚地发布我的问题,请告诉我。 Awaiting you guidance on this. 等待您的指导。

Using the render_template() function, you pass your parameters as a dictionary file mapping from your python variables to the html variables, something like this: 使用render_template()函数,您可以将参数作为字典文件传递,从python变量映射到html变量,如下所示:

name = 'Tony Stark'
workplace = 'Stark Industries'
return render_template('file.html', {name:'name', workplace:'workplace'})

The name and workplace in single quotations marks are the variables in your html document usually declared by using the name tag. 名称和工作场所中的单引号是通常使用name标记声明的html文档中的变量。

To use a while loop: 要使用while循环:

while True:
    webbrowser.open('path to file') 

work fine. 工作正常。

PS: This render_template is used in development frameworks like django and flask... so i'm not sure if I have answered your question succintly... PS:此render_template用于django和flask之类的开发框架中...因此,我不确定我是否已经简洁地回答了您的问题...

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

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