簡體   English   中英

從python打開html文件

[英]Opening a html file from python

在python 3代碼示例中

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

我想在while條件內打開本地html文件(將參數傳遞給文件)。 因此,只要條件為真,文件就應繼續打開。

這些是我嘗試過的以下事情:

webbrowser.open('path of file')

=>與此並不確定如何將參數傳遞到html頁面

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

=>該文件在條件為真時第一次打開,但以后不再重復循環(嗯,我認為return語句終止循環)

如果我沒有清楚地發布我的問題,請告訴我。 等待您的指導。

使用render_template()函數,您可以將參數作為字典文件傳遞,從python變量映射到html變量,如下所示:

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

名稱和工作場所中的單引號是通常使用name標記聲明的html文檔中的變量。

要使用while循環:

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

工作正常。

PS:此render_template用於django和flask之類的開發框架中...因此,我不確定我是否已經簡潔地回答了您的問題...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM