Flask 中的动态文件解析
最近,我收到了一些关于一个问题的建议,该问题涉及一个易于使用的网络框架,用于一个简单的项目,我正在帮助一个朋友,并建议使用Flask。
到目前为止,一切都很顺利——但是我试图弄清楚如何(或者如果可能)动态读取文件,并将文件的内容传递到我拥有的函数中。
例如,我想使用如下内容:
HTML Side:
<form action="process_file" method=post enctype=multipart/form-data>
<input type='file' name='file'>
<input type='submit' value="Upload and Process Selected File">
</form>
我认为这就是我在使用 HTML 的实际页面上所需的全部内容,因为这将允许我获取文件的路径我需要的,所以希望我能够读取所述文件。
我不确定 Flask/Python 方面该去哪里 - 我只是在寻找正确方向的一步,也许读取两个数字或字母(在文件中)并将它们输出在同一页面上?
Flask/Python 方面:
@app.route('/process_file', methods=['GET', 'POST'])
def process_file():
if request.method == 'POST':
file = request.files.get('file')
if file:
"Read file and parse the values into an array?"
"Pass arguments to a Processing function and outputs result into x)"
return render_template('index.html',answer = x)
else:
return render_template('index.html',error=1)
我不确定我的方向是否正确 - 我只是认为对 Flask / Python 有更多经验的人可以引导我到达那里。
编辑:我还注意到 Flask 似乎与 jQuery 配合得很好,结合使用它们会使处理/文件解析变得更简单吗?
谢谢大家。
I recently received some advice on a question regarding an easy to use web-framework to use for a simple project that I am helping a friend with and was suggested to use Flask.
Everything has been working out so far - however I am trying to figure out how to (or if it is possible) to read a file on the fly, and pass the contents of the file into a function that I have.
For instance, I would want to use something like the following:
HTML Side:
<form action="process_file" method=post enctype=multipart/form-data>
<input type='file' name='file'>
<input type='submit' value="Upload and Process Selected File">
</form>
I figure that is all I would need on the actual page using HTML, as this would allow me to get the path of the file that I need, so hopefully I would be able to read said-file.
I am unsure as to where to go on the Flask/Python side of things - I'm just looking for a step in the right direction, perhaps reading in two numbers or letters (in the file) and outputting them on the same page?
Flask/Python Side:
@app.route('/process_file', methods=['GET', 'POST'])
def process_file():
if request.method == 'POST':
file = request.files.get('file')
if file:
"Read file and parse the values into an array?"
"Pass arguments to a Processing function and outputs result into x)"
return render_template('index.html',answer = x)
else:
return render_template('index.html',error=1)
I'm not sure if I am headed in the right direction - I just thought someone with more experience with Flask / Python could lead me there.
Edit: I also noticed that Flask seems to play well with jQuery, would using them in combination make processing / file-parsing any simpler?
Thanks everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Flask 网站上的文档 (http://flask.pocoo.org/docs/patterns/ fileuploads/)演示了如何正确、安全地处理文件上传,我将从这里开始。如果您希望在/而不是保存之前解析文件,您应该能够使用 stream 您可以通过 request.files 访问的 FileStorage 对象上的属性/属性。
The documentation on the flask site (http://flask.pocoo.org/docs/patterns/fileuploads/) demonstrates how to properly and safely handle file uploads, I would start there. If you wish to parse the file before/instead of saving it, you should be able to use the stream property/attribute on the FileStorage object you're given access to via request.files.
我假设你的代码从烧瓶的角度来看是正确的。我的猜测是该文件是一个 python 类似文件的对象。该文档告诉您有关读取和写入文件所需了解的所有信息。
就解析而言,这取决于格式。我的建议是编写一些代码,以您期望的格式读取文件,并使您的解析例程更加可靠。然后将其放入 process_file 函数中。
就 jquery 而言,它是一个 JavaScript 库。如果您打算将 Flask 用于 ajax,那么只要 Flask 能够使用 http,它并不关心知道 Flask 是什么。但这并不会让文件解析变得更简单。
I am assuming your code is right from flask's perspective. My guess would be that file is a python file-like object. The docs for this tell you everything you need to know about reading and writing files.
As far as parsing goes, well, that depends on the format. My suggestion would be is to write some code up that reads in the file in the format you expect and get your parsing routine solid. then drop then work that into your process_file function.
As far as jquery goes, it's a javascript library. it doesn't care to know what flask is as long as flask speaks http if you are planning on using it for ajax. It wouldn't make the file-parsing any simpler though.