我正在使用Flask编写Web应用程序,并希望在Brython中使用browser.ajax功能,但找不到可行的示例.如果有人演示如何在Brython中使用ajax,那将是非常好的.更具体地说,如何通过单击提交按钮将用户输入的数据传递到文本域中.任何帮助都非常感谢!
(我在发布上述问题几周后就写了这篇文章).我按照本教程关于如何在Flask中实现ajax(http://runnable.com/UiPhLHanceFYAAAP/how-to-perform-ajax-in-flask-for-python)并尝试用Brython替换jquery.ajax.不幸的是,我仍然无法让它发挥作用.这是我的代码:
烧瓶的部分:
@app.route('/')
def index():
return render_template('index.html')
@app.route('/_add_numbers')
def add_numbers():
a = request.args.get('a', 0, type=int)
b = request.args.get('b', 0, type=int)
return jsonify(result=a + b)
Run Code Online (Sandbox Code Playgroud)
Brython/HTML:
<body onload="brython()">
<script type="text/python">
from browser import document as doc
from browser import ajax
def on_complete(req):
if req.status==200 or req.status==0:
doc["txt_area"].html = req.text
else:
doc["txt_area"].html = "error "+req.text
def get(url):
req = ajax.ajax()
a = doc['A'].value
b = doc['B'].value
req.bind('complete',on_complete)
req.open('GET',url,True)
req.set_header('content-type','application/x-www-form-urlencoded')
req.send({"a": a, "b":b})
doc['calculate'].bind('click',lambda ev:get('/_add_numbers'))
</script>
<div class="container">
<div class="header"> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PDFMiner从PDF文件中提取文本(在Python中使用PDFMiner从PDF文件中提取文本时找到的代码?).除了path/to/pdf之外,我没有更改代码.令人惊讶的是,代码返回同一文档的多个副本.我得到了与其他pdf文件相同的结果.我是否需要传递其他论点或者我错过了什么?任何帮助都非常感谢.为了以防万一,我提供了代码:
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
fstr = ''
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
str = …Run Code Online (Sandbox Code Playgroud)