简体   繁体   English

将CSV上传到Flask进行后台处理

[英]Uploading CSV to Flask for background processing

I'm looking to use Flask to host a single-page website that would allow users to upload a CSV that would be parsed and put into a database. 我希望使用Flask托管一个单页网站,该网站将允许用户上传CSV文件,该文件将被分析并放入数据库中。 All of the database shenanigans are complete (through SQLalchemy in another Python script) and I've got everything worked out once a script has access to the CSV, I just need help getting it there. 所有数据库的技巧都已完成(通过另一个Python脚本中的SQLalchemy),一旦脚本可以访问CSV,我就可以完成所有工作,我只需要帮助即可。

Here's the scenario: 这是场景:

1. User directs browser at URL (probably something like 
   http://xxx.xxx.xxx.xxx/upload/)
2. User chooses CSV to upload
3. User presses upload
4. File is uploaded and processed, but user is sent to a thank you page while our
   script is still working on the CSV (so that their disconnect doesn't cause the
   script to abort).

It's totally cool if the CSV is left on the server (in fact, it's probably preferred since we'd have a backup in case processing went awry) 如果将CSV留在服务器上,那绝对是一件很酷的事情(事实上,这是首选,因为如果处理出现问题,我们会进行备份)

I think what I want is a daemon that listens on a socket, but I'm not really experienced with this and don't know where to start getting it configured or setting up Flask. 我认为我想要的是在套接字上侦听的守护程序,但是我对此并没有真正的经验,也不知道从哪里开始配置它或设置Flask。

If you think some framework other than Flask would be easier, definitely let me know, I'm not tied to Flask, I've just read that it's pretty easy to set up! 如果您认为Flask以外的其他框架会更简单,那么绝对可以告诉我,我与Flask无关,我只是读到它的设置非常简单!

Thank you very much!! 非常感谢你!!

Here is a (very slightly simplified) example of handling file uploading in web.py based on a cook book example (the Flash example, which I have less experience with, looks even easier): 这是一个基于烹饪书示例 (非常缺乏经验的Flash示例,看起来更简单),在web.py中处理文件上传的一个示例(非常简化):

import web

urls = ('/', 'Upload')

class Upload:
    def GET(self):
        web.header("Content-Type","text/html; charset=utf-8")
        return """
               <form method="POST" enctype="multipart/form-data" action="">
               <input type="file" name="myfile" />
               <br/>
               <input type="submit" />
               """

    def POST(self):
        x = web.input(myfile={})
        filedir = '/uploads' # change this to the directory you want to store the file in.
        if 'myfile' in x: # to check if the file-object is created
            filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
            filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
            fout = open(filedir +'/'+ filename,'wb') # creates the file where the uploaded file should be stored
            fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
            fout.close() # closes the file, upload complete.
        raise web.seeother('/')

if __name__ == "__main__":
   app = web.application(urls, globals()) 
   app.run()

This renders a upload form, and then (on POST) reads the uploaded file and saves it to a designated path. 这将呈现一个上载表单,然后(在POST上)读取上载的文件并将其保存到指定的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM