我希望用户输入一个句子,然后将该句子分解成一个列表。我下载了 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/