python django run bash script in server
<p>I would like to create a website-app to run a bash script located in a server. Basically I want this website for:</p>

<ul>
<li>Upload a file</li>
<li>select some parameters</li>
<li>Run a bash script taking the input file and the parameters</li>
<li>Download the results</li>
</ul>

<p>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. </p>

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

<p>This can be done in Python using the Django framework. </p>

<p>First create a form including a <code>FileField</code> and the fields for the other parameters:</p>

<pre><code>from django import forms

class UploadFileForm(forms.Form):
    my_parameter = forms.CharField(max_length=50)
    file = forms.FileField()
</code></pre>

<p>Include the <code>UploadFileForm</code> in your view and call your function for handling the uploaded file:</p>

<pre><code>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})
</code></pre>

<p>Create the function to handle the uploaded file and call your bash script:</p>

<pre><code>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
</code></pre>

<p>Check out <a href="https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/</a> for more examples and instructions on how the handle file uploads in Django.</p>

python django