Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python script online (django) [closed]

I'm new to Python, and programming in general, so anything explained should be in Layman terms.

I've created a simple script that reads CSV files and outputs results. I'd like to upload that script and have it run online as a simple web interface or web app.

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

Thanks.

like image 850
user1947154 Avatar asked Jan 04 '13 00:01

user1947154


2 Answers

I maybe be biased but I'd say sure, you're on the right track!

It sounds like you want people to be able to upload a csv, then your web app will process it and output the results? If so, check out the Django docs:

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

Nothing too 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 then put it into your web page or template, including the correct enctype:

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

Finally, you deal with it in your view that handles the post (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's good practice to use a redirect after any POST request:
        return HttpResponseRedirect('/show_results/%d/' % db_entry.id)

Aside from that, no particular special config is necessary on PythonAnywhere. The file is saved (temporarily) to /tmp, which will work fine. If you want to save the file for later, you'll have to add some code to do that.

Hope that helps. We're here if you have any more questions!

like image 153
hwjp Avatar answered Nov 16 '22 22:11

hwjp


As a PythonAnywhere dev I would say that you've started in the right place. We do try and make things as simple as possible.

You could start with a simpler application by using the flask web framework. There is a quickstart for that as well. Below is a very simple flask app that would return some output when visited. This code would go into the \var\www\your_username_pythonanywhere_com_wsgi.py file.

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()

That is the simplest, single file, way to serve a bit of data as a web service. I would say start by getting that to work and then you can start expanding your knowledge and adding features.

like image 31
aychedee Avatar answered Nov 16 '22 21:11

aychedee