My question about implementing a script into a view in a django web app.
I have a .py
script that I want to run inside this django web app using the user-supplied data, the configfile
, the modelfile
and the choice as command line arguments. The output is then a list with different scores which will then be displayed inside the html (not the part I need help with yet).
So I have 2 forms, one that uploads a ML model and another one (the one I need help with) that uses user-supplied data to run a uploaded model:
class Meta:
model = MlModel
fields = [
'modelname',
'configfile',
'modelfile',
'choice',
]
class TestForm(forms.Form):
testdata = forms.FileField()
model = forms.ModelChoiceField(queryset=MlModel.objects.all())
My views are as follow, where the test view is where the testing happens:
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/mltesting/')
else:
form = UploadForm()
context = {'form':form}
return render(request,'mltesting/upload.html', context=context)
def test_model(request):
submitbutton = request.POST.get("submit")
testdata = ''
modfile = ''
confile = ''
choice = ''
form = TestForm(request.POST, request.FILES)
if form.is_valid():
testdata = request.FILES['testdata']
model = form.cleaned_data.get('model')
model = MlModel.objects.get(modelname=model)
modfile = model.modelfile.path
confile = model.configfile.path
choice = model.choice
else:
form = TestForm()
context = {'form': form, 'testdata': testdata, 'submitbutton': submitbutton, 'modelfile':modfile, 'configfile': confile,'choice': choice}
return render(request,'mltesting/test.html', context=context)
And this is the HTML template where the output of the testing is supposed to be:
{% block content %}
<p>Compound Protein Interaction Tool</p>
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_table }}
<input type="Submit" name="submit" value="Submit"/>
</form>
{% if submitbutton == "Submit" %}
{% endif %}
{% endblock %}
I am fairly new to Django in general so I apologise for the (probable) bad formulation of my question, but all help is greatly appreciated.