Run python script online (django)

I am new to Python and generally program, so the whole explanation should be in terms of Layman.

I created a simple script that reads CSV files and outputs the results. I would like to download this script and run it on the web as a simple web interface or web application.

I signed up for pythonanywhere.com using the Django framework. Am I on the right track here?

Thanks.

+7
source share
2 answers

I may be biased, but I would say that you are on the right track!

It looks like you want people to be able to download csv, then your web application will process it and give the results? If so, check out the Django docs:

https://docs.djangoproject.com/en/1.3/topics/http/file-uploads/

Nothing complicated if you create a Django Form object with a FileField as per the example.

 from django import forms class UploadFileForm(forms.Form): file = forms.FileField() 

You will then post it on your web page or template, including the correct enctype :

 <form enctype="multipart/form-data" method="post" action="/foo/"> {{form.as_p}} </form> 

Finally, you are dealing with this in your view, which processes the message (with the URL from the form action):

 def handle_csv_upload(request): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): results = do_my_csv_magic(request.FILES['file']) # now eg save the results to the database, and show them to the user db_entry = MyCSVResults(results=results) db_entry.save() # it good practice to use a redirect after any POST request: return HttpResponseRedirect('/show_results/%d/' % db_entry.id) 

In addition, PythonAnywhere does not require much configuration. The file is saved (temporarily) in / tmp, which will work fine. If you want to save the file for later, you will have to add code for this.

Hope this helps. We are here if you have more questions!

+8
source

Like PythonAnywhere dev I would say that you started in the right place. We try to make everything as simple as possible.

You can start with a simpler application using the flask web infrastructure . There is a quick start for this. Below is a very simple flash application that will return some result when visiting. This code will go into the \var\www\your_username_pythonanywhere_com_wsgi.py .

 import os import sys from flask import Flask app = Flask(__name__) app.debug = True path = '/home/your_username/' if path not in sys.path: sys.path.append(path) from my_script import function_that_parses_csv @app.route('/') def root(): return function_that_parses_csv() 

This is the simplest, only file capable of serving some data as a web service. I would say, I’ll start with the fact that this will work, and then you can begin to expand your knowledge and add functions.

+4
source

All Articles