Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload and Processing using Python

I was approached by a friend a few days ago - who has very little programming experience, and he has a project that he asked for some help with.

Basically - this is what he is attempting to accomplish:

1.) Create a website that can accept text files as input.
2.) Read said file and pass the parameters contained in the 
    file to a python script.
3.) Output these results of the script on the same webpage upon completion.

He knows a small amount of Python (enough to write the processing script), but he has no idea where to go from here. I made a quick example for him using an ASP page that read in a file, and used IronPython to pass the parameters into a script file and output the results, which worked just as I had expected it.

However - for him I was hoping to guide him in the right direction of developing a much simpler application to perform this and was hoping to find some suggestions or hear some thoughts on different approaches. I figure due to his lack of experience the simpler, the better.

Thanks guys.

like image 286
Rion Williams Avatar asked Feb 06 '11 21:02

Rion Williams


People also ask

How do you upload a file in Python?

Method 1: Using the Python's os Module: Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want. Lastly, we need the input tag with the filename attribute to upload the file we want.

How do you process a file in Python?

Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read r , write w or append a to the file.

How do I make an upload button in Python?

To upload a file, the HTML form must have the enctype attribute set to multipart/form-data. The input tag with the file type creates a "Browse" button.


2 Answers

Flask is dead-simple, extremely powerful, and intuitive. I prefer it over Django for smaller projects, as Django uses way too many folders (just follow the introduction tutorial). Here's what I mean by simple and intuitive. I can't really explain it in words, so here's an example script:

File: script.py

app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/')
def index():
  return render_template('index.html', message = 'Hello')

if __name__ == '__main__':
  app.run(host = '0.0.0.0')

File: index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  <head>
    <title>Test</title>
  </head>

  <body>
  {% if message != 'nope' %}
    {{ message }}
  {% endif %}
  </body>
</html>

Just my thoughts, so good luck.

like image 169
Blender Avatar answered Sep 28 '22 06:09

Blender


Maybe he see to Flask? http://flask.pocoo.org/ Very simple web-framework in Python for fast creation a small web-site.

like image 32
gigimon Avatar answered Sep 28 '22 04:09

gigimon