python - 我如何使用 python 将输出打印到 html 页面?

标签 python html

我希望用户输入一个句子,然后将该句子分解成一个列表。我下载了 html 页面,但我无法将该句子传递给 python。

如何正确发送用户输入以供 python 处理并将其输出到新页面?

最佳答案

有很多Python web frameworks .例如,要使用 bottle 来分解一个句子:

break-sentence.py :

#!/usr/bin/env python
from bottle import request, route, run, view

@route('/', method=['GET', 'POST'])
@view('form_template')
def index():
    return dict(parts=request.forms.sentence.split(), # split on whitespace
                show_form=request.method=='GET') # show form for get requests

run(host='localhost', port=8080)

和模板文件form_template.tpl在 Python 中处理后用于显示形式和句子部分(参见上面的 index() 函数):

<!DOCTYPE html>
<title>Break up sentence</title>
%if show_form:
<form action="/" method="post">
  <label for="sentence">Input a sentence to break up</label>
  <input type="text" name="sentence" />
</form>
%else:
Sentence parts:<ol>
%for part in parts:
     <li> {{ part }}
%end
</ol>
%end

request.forms.sentence在 Python 中用于访问来自 <input name="sentence"/> 的用户输入字段。

要尝试它,您可以下载 bottle.py并运行:

$ python break-sentence.py 
Bottle server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

现在您可以访问http://localhost:8080/ .

关于python - 我如何使用 python 将输出打印到 html 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9540302/

相关文章:

javascript - (集成 P5.js 和 Three.js) --- 使用 P5.js 库中的动画创建一个 Three JS 场景?

javascript - 无法更改滚动条值

javascript - 在图像标签之前插入 anchor 标签 jQuery

python - 使用输入作为数组和字符串

python - 如何在没有空格的情况下连接一个字符串中的两个单词

html - 右文本对齐 mat-expansion-panel Material 组件

javascript - 使用 JQuery 收集表单数据并删除或隐藏表单字段

python - Xvfb 多显示器并行处理?

Python:将值列表拆分为两个值列表。列表的总和应尽可能相等

python - AppEngine 服务器无法导入 atom 模块