1

I would like to create a website-app to run a bash script located in a server. Basically I want this website for:

  • Upload a file
  • select some parameters
  • Run a bash script taking the input file and the parameters
  • Download the results

I know you can do this with php, javascript... but I have never program in these languages. However I can program in python. I have used pyQT library in python for similar purposes.

Can this be done with django? or should I start learning php & javascript? I cannot find any tutorial for this specific task in Django.

1
  • Welcome to stackoverflow. What have yout ried so far? You are supposed to show some own effort before getting answers from others. Commented Sep 17, 2016 at 13:30

1 Answer 1

2

This can be done in Python using the Django framework.

First create a form including a FileField and the fields for the other parameters:

from django import forms

class UploadFileForm(forms.Form):
    my_parameter = forms.CharField(max_length=50)
    file = forms.FileField()

Include the UploadFileForm in your view and call your function for handling the uploaded file:

from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm

# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            my_parameter = form.cleaned_data['my_parameter']
            # Handle the uploaded file
            results = handle_uploaded_file(request.FILES['file'], title)
            # Clear the form and parse the results
            form = UploadFileForm()
            return render(request, 'upload.html', {'form': form, 'results': results})
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

Create the function to handle the uploaded file and call your bash script:

import subprocess
import os

def handle_uploaded_file(f, my_parameter):
    file_path = os.path.join('/path/to/destination/', f.name)
    # Save the file 
    with open(file_path, 'wb+') as destination:
        for chunk in f.chunks():
             destination.write(chunk)
    # Call your bash script with the
    output = subprocess.check_output(['./my_script.sh',str(file_path),str(my_parameter)], shell=True)
    return output

Check out https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/ for more examples and instructions on how the handle file uploads in Django.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.